-
Notifications
You must be signed in to change notification settings - Fork 341
/
hardwareprofile.cpp
301 lines (250 loc) · 7.52 KB
/
hardwareprofile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "hardwareprofile.h"
// qt
#include <QStringList>
#include <QDir>
#include <QTextStream>
// libmythbase
#include "mythcorecontext.h"
#include "mythdirs.h"
#include "mythlogging.h"
#include "mythsystem.h"
#include "exitcodes.h"
#include "mythdate.h"
const QString SMOLT_SERVER_LOCATION =
QString("http://smolt.mythtv.org/");
const QString SMOLT_TOKEN =
QString("smolt_token-smolt.mythtv.org");
HardwareProfile::HardwareProfile() :
m_enabled(false),
m_uuid(QString()), m_publicuuid(QString()),
m_lastUpdate(QDateTime()), m_hardwareProfile(QString())
{
m_enabled = (gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 1);
m_uuid = gCoreContext->GetSetting("HardwareProfileUUID");
m_publicuuid = gCoreContext->GetSetting("HardwareProfilePublicUUID");
if (m_enabled)
{
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("SELECT lastrun FROM housekeeping"
" WHERE tag = 'HardwareProfiler'"
" AND hostname = :HOST");
query.bindValue(":HOST", gCoreContext->GetHostName());
if (query.exec() && query.next())
m_lastUpdate = MythDate::as_utc(query.value(0).toDateTime());
}
}
HardwareProfile::~HardwareProfile()
{
}
void HardwareProfile::Enable(void)
{
if (m_uuid.isEmpty())
return;
gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "1", "");
}
void HardwareProfile::Disable(void)
{
gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "0", "");
}
void HardwareProfile::GenerateUUIDs(void)
{
QString fileprefix = GetConfDir() + "/HardwareProfile";
QDir dir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);
// Generate the Private Hardware UUID (or recover them from the DB or file)
QString fileUUID = GetPrivateUUIDFromFile();
if (fileUUID.isEmpty() && m_uuid.isEmpty())
{
LOG(VB_GENERAL, LOG_INFO,
"No UUID in DB or File, generating new UUID...");
QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
QStringList args;
args << "-p";
MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
system.Run();
system.Wait();
m_hardwareProfile = system.ReadAll();
m_uuid = GetPrivateUUIDFromFile();
}
else if (fileUUID.isEmpty())
{
LOG(VB_GENERAL, LOG_INFO,
QString("Writing Database UUID to local file: %1")
.arg(m_uuid));
WritePrivateUUIDToFile(m_uuid);
}
else if (m_uuid.isEmpty())
{
LOG(VB_GENERAL, LOG_INFO,
QString("Profile UUID found in local file: %1")
.arg(fileUUID));
m_uuid = fileUUID;
}
// Get the Public UUID from file
m_publicuuid = GetPublicUUIDFromFile();
}
QString HardwareProfile::GetPrivateUUIDFromFile() const
{
QString ret;
QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
QFile file(hwuuid_file);
if (file.open( QIODevice::ReadOnly ))
{
QTextStream stream(&file);
ret = stream.readLine();
file.close();
}
return ret;
}
QString HardwareProfile::GetPublicUUIDFromFile() const
{
QString ret;
QString pubuuid_file = GetConfDir() + "/HardwareProfile/uuiddb.cfg";
QString pubuuid;
QFile pubfile(pubuuid_file);
if (pubfile.open( QIODevice::ReadOnly ))
{
QString s;
QTextStream stream(&pubfile);
while ( !stream.atEnd() )
{
s = stream.readLine();
if (s.contains(m_uuid))
{
ret = s.section("=",1,1);
ret = ret.trimmed();
}
}
pubfile.close();
}
return ret;
}
QString HardwareProfile::GetAdminPasswordFromFile() const
{
QString ret;
if (gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
return ret;
QString token_file = GetConfDir() + "/HardwareProfile/" + SMOLT_TOKEN;
QFile file(token_file);
if (file.open( QIODevice::ReadOnly ))
{
QTextStream stream(&file);
ret = stream.readLine();
file.close();
}
return ret;
}
bool HardwareProfile::WritePrivateUUIDToFile(QString uuid)
{
QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
QFile file(hwuuid_file);
if (file.open(QIODevice::WriteOnly))
{
QTextStream stream(&file);
stream << uuid;
file.close();
return true;
}
else
return false;
}
bool HardwareProfile::NeedsUpdate(void) const
{
if (!m_lastUpdate.isNull() &&
(m_lastUpdate.addMonths(1) < MythDate::current()) &&
!m_uuid.isEmpty())
{
LOG(VB_GENERAL, LOG_INFO,
"Last hardware profile update was > 30 days ago, update "
"required...");
return true;
}
return false;
}
bool HardwareProfile::SubmitProfile(bool updateTime)
{
if (m_uuid.isEmpty())
return false;
if (!m_enabled)
Enable();
if (!m_hardwareProfile.isEmpty())
LOG(VB_GENERAL, LOG_INFO,
QString("Submitting the following hardware profile: %1")
.arg(m_hardwareProfile));
QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
QStringList args;
args << "--submitOnly";
args << "-a";
MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
system.Run();
if (system.Wait() == GENERIC_EXIT_OK)
{
GenerateUUIDs();
gCoreContext->SaveSetting("HardwareProfileUUID", GetPrivateUUID());
gCoreContext->SaveSetting("HardwareProfilePublicUUID", GetPublicUUID());
if (updateTime)
{
HardwareProfileTask task;
task.UpdateLastRun(MythDate::current());
}
return true;
}
else
return false;
return false;
}
bool HardwareProfile::DeleteProfile(void)
{
if (m_uuid.isEmpty())
return false;
LOG(VB_GENERAL, LOG_INFO,
QString("Deleting the following hardware profile: %1")
.arg(m_uuid));
QString cmd = GetShareDir() + "hardwareprofile/deleteProfile.py";
QStringList args;
MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
system.Run();
if (system.Wait() == GENERIC_EXIT_OK)
{
gCoreContext->SaveSetting("HardwareProfileUUID", "");
gCoreContext->SaveSetting("HardwareProfilePublicUUID", "");
gCoreContext->SaveSetting("HardwareProfileLastUpdated",
MythDate::current_iso_string());
return true;
}
else
return false;
return false;
}
QString HardwareProfile::GetProfileURL() const
{
QString ret;
if (!gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
{
ret = SMOLT_SERVER_LOCATION + "client/show/?uuid=" + m_publicuuid;
}
return ret;
}
QString HardwareProfile::GetHardwareProfile() const
{
QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
QStringList args;
args << "-p";
MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
system.Run();
system.Wait();
return system.ReadAll();
}
bool HardwareProfileTask::DoCheckRun(QDateTime now)
{
if (GetLastRun().secsTo(now) > 2592000 && // 60*60*24*30
(gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 1))
return true;
return false;
}
bool HardwareProfileTask::DoRun(void)
{
HardwareProfile hp;
return hp.SubmitProfile(false);
}