MythTV master
hardwareprofile.cpp
Go to the documentation of this file.
1#include "hardwareprofile.h"
2
3// qt
4#include <QStringList>
5#include <QDir>
6#include <QTextStream>
7
8// libmythbase
9#include "mythcorecontext.h"
10#include "mythdirs.h"
11#include "mythlogging.h"
12#include "mythsystemlegacy.h"
13#include "exitcodes.h"
14#include "mythdate.h"
15
16const QString SMOLT_SERVER_LOCATION =
17 QString("http://smolt.mythtv.org/");
18const QString SMOLT_TOKEN =
19 QString("smolt_token-smolt.mythtv.org");
20
22{
23 m_enabled = (gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 1);
24 m_uuid = gCoreContext->GetSetting("HardwareProfileUUID");
25 m_publicuuid = gCoreContext->GetSetting("HardwareProfilePublicUUID");
26
27 if (m_enabled)
28 {
30
31 query.prepare("SELECT lastrun FROM housekeeping"
32 " WHERE tag = 'HardwareProfiler'"
33 " AND hostname = :HOST");
34 query.bindValue(":HOST", gCoreContext->GetHostName());
35 if (query.exec() && query.next())
36 m_lastUpdate = MythDate::as_utc(query.value(0).toDateTime());
37 }
38}
39
41{
42 if (m_uuid.isEmpty())
43 return;
44
45 gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "1", "");
46}
47
49{
50 gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "0", "");
51}
52
54{
55 QString fileprefix = GetConfDir() + "/HardwareProfile";
56 QDir dir(fileprefix);
57 if (!dir.exists())
58 dir.mkdir(fileprefix);
59
60 // Generate the Private Hardware UUID (or recover them from the DB or file)
61
62 QString fileUUID = GetPrivateUUIDFromFile();
63
64 if (fileUUID.isEmpty() && m_uuid.isEmpty())
65 {
66 LOG(VB_GENERAL, LOG_INFO,
67 "No UUID in DB or File, generating new UUID...");
68
69 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
70 QStringList args;
71 args << "-p";
73
74 system.Run();
75 system.Wait();
76 m_hardwareProfile = system.ReadAll();
78 }
79 else if (fileUUID.isEmpty())
80 {
81 LOG(VB_GENERAL, LOG_INFO,
82 QString("Writing Database UUID to local file: %1")
83 .arg(m_uuid));
85 }
86 else if (m_uuid.isEmpty())
87 {
88 LOG(VB_GENERAL, LOG_INFO,
89 QString("Profile UUID found in local file: %1")
90 .arg(fileUUID));
91 m_uuid = fileUUID;
92 }
93
94 // Get the Public UUID from file
95
97}
98
100{
101 QString ret;
102
103 QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
104 QFile file(hwuuid_file);
105 if (file.open( QIODevice::ReadOnly ))
106 {
107 QTextStream stream(&file);
108 ret = stream.readLine();
109 file.close();
110 }
111
112 return ret;
113}
114
116{
117 QString ret;
118
119 QString pubuuid_file = GetConfDir() + "/HardwareProfile/uuiddb.cfg";
120 QFile pubfile(pubuuid_file);
121 if (pubfile.open( QIODevice::ReadOnly ))
122 {
123 QString s;
124 QTextStream stream(&pubfile);
125 while ( !stream.atEnd() )
126 {
127 s = stream.readLine();
128 if (s.contains(m_uuid))
129 {
130 ret = s.section("=",1,1);
131 ret = ret.trimmed();
132 }
133 }
134 pubfile.close();
135 }
136
137 return ret;
138}
139
141{
142 QString ret;
143
144 if (gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
145 return ret;
146
147 QString token_file = GetConfDir() + "/HardwareProfile/" + SMOLT_TOKEN;
148 QFile file(token_file);
149 if (file.open( QIODevice::ReadOnly ))
150 {
151 QTextStream stream(&file);
152 ret = stream.readLine();
153 file.close();
154 }
155
156 return ret;
157}
158
160{
161 QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
162 QFile file(hwuuid_file);
163 if (file.open(QIODevice::WriteOnly))
164 {
165 QTextStream stream(&file);
166 stream << uuid;
167 file.close();
168 return true;
169 }
170 return false;
171}
172
174{
175 if (!m_lastUpdate.isNull() &&
176 (m_lastUpdate.addMonths(1) < MythDate::current()) &&
177 !m_uuid.isEmpty())
178 {
179 LOG(VB_GENERAL, LOG_INFO,
180 "Last hardware profile update was > 30 days ago, update "
181 "required...");
182 return true;
183 }
184
185 return false;
186}
187
189{
190 if (m_uuid.isEmpty())
191 return false;
192
193 if (!m_enabled)
194 Enable();
195
196 if (!m_hardwareProfile.isEmpty())
197 {
198 LOG(VB_GENERAL, LOG_INFO,
199 QString("Submitting the following hardware profile: %1")
200 .arg(m_hardwareProfile));
201 }
202
203 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
204 QStringList args;
205 args << "--submitOnly";
206 args << "-a";
208
209 system.Run();
210 if (system.Wait() == GENERIC_EXIT_OK)
211 {
213 gCoreContext->SaveSetting("HardwareProfileUUID", GetPrivateUUID());
214 gCoreContext->SaveSetting("HardwareProfilePublicUUID", GetPublicUUID());
215
216 if (updateTime)
217 {
220 }
221
222 return true;
223 }
224 return false;
225}
226
228{
229 if (m_uuid.isEmpty())
230 return false;
231
232 LOG(VB_GENERAL, LOG_INFO,
233 QString("Deleting the following hardware profile: %1")
234 .arg(m_uuid));
235
236 QString cmd = GetShareDir() + "hardwareprofile/deleteProfile.py";
237 QStringList args;
239
240 system.Run();
241 if (system.Wait() == GENERIC_EXIT_OK)
242 {
243 gCoreContext->SaveSetting("HardwareProfileUUID", "");
244 gCoreContext->SaveSetting("HardwareProfilePublicUUID", "");
245 Disable();
246 return true;
247 }
248 return false;
249}
250
252{
253 QString ret;
254
255 if (!gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
256 {
257 ret = SMOLT_SERVER_LOCATION + "client/show/?uuid=" + m_publicuuid;
258 }
259
260 return ret;
261}
262
264{
265 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
266 QStringList args;
267 args << "-p";
269
270 system.Run();
271 system.Wait();
272 return system.ReadAll();
273}
274
275bool HardwareProfileTask::DoCheckRun(const QDateTime& now)
276{
277 if (gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 0)
278 // global disable, we don't want to run
279 return false;
280
281 // leave it up to the standard periodic calculation, 30 +-1 days
283}
284
286{
288 return hp.SubmitProfile(false);
289}
bool DoCheckRun(const QDateTime &now) override
bool DoRun(void) override
void GenerateUUIDs(void)
static bool WritePrivateUUIDToFile(const QString &uuid)
bool NeedsUpdate(void) const
QString GetPublicUUIDFromFile(void) const
QString m_hardwareProfile
QString GetProfileURL(void) const
QDateTime m_lastUpdate
QString GetPublicUUID(void) const
bool DeleteProfile(void)
static QString GetAdminPasswordFromFile(void)
QString GetPrivateUUID(void) const
static QString GetPrivateUUIDFromFile(void)
static QString GetHardwareProfile(void)
static void Disable(void)
bool SubmitProfile(bool updateTime=true)
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:837
QVariant value(int i) const
Definition: mythdbcon.h:204
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:812
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:550
QString GetHostName(void)
void SaveSetting(const QString &key, int newValue)
QString GetSetting(const QString &key, const QString &defaultval="")
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)
int GetNumSetting(const QString &key, int defaultval=0)
uint Wait(std::chrono::seconds timeout=0s)
void Run(std::chrono::seconds timeout=0s)
Runs a command inside the /bin/sh shell. Returns immediately.
QByteArray & ReadAll()
bool DoCheckRun(const QDateTime &now) override
QDateTime UpdateLastRun(const QDateTime &last, bool successful=true) override
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
const QString SMOLT_TOKEN
const QString SMOLT_SERVER_LOCATION
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString GetShareDir(void)
Definition: mythdirs.cpp:261
QString GetConfDir(void)
Definition: mythdirs.cpp:263
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
@ kMSStdOut
allow access to stdout
Definition: mythsystem.h:41
@ kMSRunShell
run process through shell
Definition: mythsystem.h:43
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:28
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15