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 
16 const QString SMOLT_SERVER_LOCATION =
17  QString("http://smolt.mythtv.org/");
18 const 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 
188 bool HardwareProfile::SubmitProfile(bool updateTime)
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";
207  MythSystemLegacy system(cmd, args, kMSRunShell | kMSStdOut);
208 
209  system.Run();
210  if (system.Wait() == GENERIC_EXIT_OK)
211  {
212  GenerateUUIDs();
213  gCoreContext->SaveSetting("HardwareProfileUUID", GetPrivateUUID());
214  gCoreContext->SaveSetting("HardwareProfilePublicUUID", GetPublicUUID());
215 
216  if (updateTime)
217  {
218  HardwareProfileTask task;
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;
238  MythSystemLegacy system(cmd, args, kMSRunShell | kMSStdOut);
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";
268  MythSystemLegacy system(cmd, args, kMSRunShell | kMSStdOut);
269 
270  system.Run();
271  system.Wait();
272  return system.ReadAll();
273 }
274 
275 bool 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 {
287  HardwareProfile hp;
288  return hp.SubmitProfile(false);
289 }
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:813
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
build_compdb.args
args
Definition: build_compdb.py:11
hardwareprofile.h
HardwareProfile::m_enabled
bool m_enabled
Definition: hardwareprofile.h:45
HardwareProfile::m_publicuuid
QString m_publicuuid
Definition: hardwareprofile.h:47
MythSystemLegacy
Definition: mythsystemlegacy.h:67
HardwareProfile::m_hardwareProfile
QString m_hardwareProfile
Definition: hardwareprofile.h:49
MythDate::as_utc
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:27
HardwareProfile::GetPrivateUUIDFromFile
static QString GetPrivateUUIDFromFile(void)
Definition: hardwareprofile.cpp:99
HardwareProfile::WritePrivateUUIDToFile
static bool WritePrivateUUIDToFile(const QString &uuid)
Definition: hardwareprofile.cpp:159
HardwareProfile::m_lastUpdate
QDateTime m_lastUpdate
Definition: hardwareprofile.h:48
SMOLT_SERVER_LOCATION
const QString SMOLT_SERVER_LOCATION
Definition: hardwareprofile.cpp:16
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:619
PeriodicHouseKeeperTask::DoCheckRun
bool DoCheckRun(const QDateTime &now) override
Definition: housekeeper.cpp:379
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythSystemLegacy::ReadAll
QByteArray & ReadAll()
Definition: mythsystemlegacy.cpp:402
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
HardwareProfile::GetPublicUUIDFromFile
QString GetPublicUUIDFromFile(void) const
Definition: hardwareprofile.cpp:115
mythsystemlegacy.h
mythdate.h
mythlogging.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
HardwareProfile::HardwareProfile
HardwareProfile()
Definition: hardwareprofile.cpp:21
HardwareProfile
Definition: hardwareprofile.h:16
HardwareProfileTask::DoRun
bool DoRun(void) override
Definition: hardwareprofile.cpp:285
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:551
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:254
HardwareProfile::DeleteProfile
bool DeleteProfile(void)
Definition: hardwareprofile.cpp:227
HardwareProfile::GetHardwareProfile
static QString GetHardwareProfile(void)
Definition: hardwareprofile.cpp:263
MythSystemLegacy::Wait
uint Wait(std::chrono::seconds timeout=0s)
Definition: mythsystemlegacy.cpp:243
HardwareProfileTask
Definition: hardwareprofile.h:52
HardwareProfileTask::DoCheckRun
bool DoCheckRun(const QDateTime &now) override
Definition: hardwareprofile.cpp:275
HardwareProfile::GetProfileURL
QString GetProfileURL(void) const
Definition: hardwareprofile.cpp:251
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
PeriodicHouseKeeperTask::UpdateLastRun
QDateTime UpdateLastRun(const QDateTime &last, bool successful=true) override
Definition: housekeeper.cpp:363
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:912
HardwareProfile::GetPublicUUID
QString GetPublicUUID(void) const
Definition: hardwareprofile.h:38
kMSRunShell
@ kMSRunShell
run process through shell
Definition: mythsystem.h:43
HardwareProfile::GetPrivateUUID
QString GetPrivateUUID(void) const
Definition: hardwareprofile.h:39
HardwareProfile::SubmitProfile
bool SubmitProfile(bool updateTime=true)
Definition: hardwareprofile.cpp:188
mythcorecontext.h
SMOLT_TOKEN
const QString SMOLT_TOKEN
Definition: hardwareprofile.cpp:18
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:889
HardwareProfile::NeedsUpdate
bool NeedsUpdate(void) const
Definition: hardwareprofile.cpp:173
HardwareProfile::m_uuid
QString m_uuid
Definition: hardwareprofile.h:46
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:838
MythSystemLegacy::Run
void Run(std::chrono::seconds timeout=0s)
Runs a command inside the /bin/sh shell. Returns immediately.
Definition: mythsystemlegacy.cpp:213
HardwareProfile::GenerateUUIDs
void GenerateUUIDs(void)
Definition: hardwareprofile.cpp:53
MythCoreContext::SaveSetting
void SaveSetting(const QString &key, int newValue)
Definition: mythcorecontext.cpp:881
HardwareProfile::Disable
static void Disable(void)
Definition: hardwareprofile.cpp:48
exitcodes.h
kMSStdOut
@ kMSStdOut
allow access to stdout
Definition: mythsystem.h:41
MythCoreContext::SaveSettingOnHost
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)
Definition: mythcorecontext.cpp:891
HardwareProfile::GetAdminPasswordFromFile
static QString GetAdminPasswordFromFile(void)
Definition: hardwareprofile.cpp:140
HardwareProfile::Enable
void Enable(void)
Definition: hardwareprofile.cpp:40
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:898
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:838