InstanceTable.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. // db
  3. #include <db/DBManager.h>
  4. #include <db/SettingsTable.h>
  5. // qt
  6. #include <QDateTime>
  7. ///
  8. /// @brief Hyperion instance manager specific database interface. prepares also the Hyperion database for all follow up usage (Init QtSqlConnection) along with db name
  9. ///
  10. class InstanceTable : public DBManager
  11. {
  12. public:
  13. InstanceTable(const QString& rootPath, QObject* parent = nullptr, bool readonlyMode = false)
  14. : DBManager(parent)
  15. {
  16. setReadonlyMode(readonlyMode);
  17. // Init Hyperion database usage
  18. setRootPath(rootPath);
  19. setDatabaseName("hyperion");
  20. // Init instance table
  21. setTable("instances");
  22. createTable(QStringList()<<"instance INTEGER"<<"friendly_name TEXT"<<"enabled INTEGER DEFAULT 0"<<"last_use TEXT");
  23. // start/create the first Hyperion instance index 0
  24. createInstance();
  25. };
  26. ///
  27. /// @brief Create a new Hyperion instance entry, the name needs to be unique
  28. /// @param name The name of the instance
  29. /// @param[out] inst The id that has been assigned
  30. /// @return True on success else false
  31. ///
  32. inline bool createInstance(const QString& name, quint8& inst)
  33. {
  34. VectorPair fcond;
  35. fcond.append(CPair("friendly_name",name));
  36. // check duplicate
  37. if(!recordExists(fcond))
  38. {
  39. inst = 0;
  40. VectorPair cond;
  41. cond.append(CPair("instance",inst));
  42. // increment to next avail index
  43. while(recordExists(cond))
  44. {
  45. inst++;
  46. cond.removeFirst();
  47. cond.append(CPair("instance",inst));
  48. }
  49. // create
  50. QVariantMap data;
  51. data["friendly_name"] = name;
  52. data["instance"] = inst;
  53. VectorPair lcond;
  54. return createRecord(lcond, data);
  55. }
  56. return false;
  57. }
  58. ///
  59. /// @brief Delete a Hyperion instance
  60. /// @param inst The id that has been assigned
  61. /// @return True on success else false
  62. ///
  63. inline bool deleteInstance(quint8 inst)
  64. {
  65. VectorPair cond;
  66. cond.append(CPair("instance",inst));
  67. if(deleteRecord(cond))
  68. {
  69. // delete settings entries
  70. SettingsTable settingsTable(inst);
  71. settingsTable.deleteInstance();
  72. return true;
  73. }
  74. return false;
  75. }
  76. ///
  77. /// @brief Assign a new name for the given instance
  78. /// @param inst The instance index
  79. /// @param name The new name of the instance
  80. /// @return True on success else false (instance not found)
  81. ///
  82. inline bool saveName(quint8 inst, const QString& name)
  83. {
  84. VectorPair fcond;
  85. fcond.append(CPair("friendly_name",name));
  86. // check duplicate
  87. if(!recordExists(fcond))
  88. {
  89. if(instanceExist(inst))
  90. {
  91. VectorPair cond;
  92. cond.append(CPair("instance",inst));
  93. QVariantMap data;
  94. data["friendly_name"] = name;
  95. return updateRecord(cond, data);
  96. }
  97. }
  98. return false;
  99. }
  100. ///
  101. /// @brief Get all instances with all columns
  102. /// @param justEnabled return just enabled instances if true
  103. /// @return The found instances
  104. ///
  105. inline QVector<QVariantMap> getAllInstances(bool justEnabled = false)
  106. {
  107. QVector<QVariantMap> results;
  108. getRecords(results, QStringList(), QStringList() << "instance ASC");
  109. if(justEnabled)
  110. {
  111. for (auto it = results.begin(); it != results.end();)
  112. {
  113. if( ! (*it)["enabled"].toBool())
  114. {
  115. it = results.erase(it);
  116. continue;
  117. }
  118. ++it;
  119. }
  120. }
  121. return results;
  122. }
  123. ///
  124. /// @brief Test if instance record exists
  125. /// @param[in] user The user id
  126. /// @return true on success else false
  127. ///
  128. inline bool instanceExist(quint8 inst)
  129. {
  130. VectorPair cond;
  131. cond.append(CPair("instance",inst));
  132. return recordExists(cond);
  133. }
  134. ///
  135. /// @brief Get instance name by instance index
  136. /// @param index The index to search for
  137. /// @return The name of this index, may return NOT FOUND if not found
  138. ///
  139. inline const QString getNamebyIndex(quint8 index)
  140. {
  141. QVariantMap results;
  142. VectorPair cond;
  143. cond.append(CPair("instance", index));
  144. getRecord(cond, results, QStringList("friendly_name"));
  145. QString name = results["friendly_name"].toString();
  146. return name.isEmpty() ? "NOT FOUND" : name;
  147. }
  148. ///
  149. /// @brief Update 'last_use' timestamp
  150. /// @param inst The instance to update
  151. ///
  152. inline void setLastUse(quint8 inst)
  153. {
  154. VectorPair cond;
  155. cond.append(CPair("instance", inst));
  156. QVariantMap map;
  157. map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
  158. updateRecord(cond, map);
  159. }
  160. ///
  161. /// @brief Update 'enabled' column by instance index
  162. /// @param inst The instance to update
  163. /// @param newState True when enabled else false
  164. ///
  165. inline void setEnable(quint8 inst, bool newState)
  166. {
  167. VectorPair cond;
  168. cond.append(CPair("instance", inst));
  169. QVariantMap map;
  170. map["enabled"] = newState;
  171. updateRecord(cond, map);
  172. }
  173. ///
  174. /// @brief Get state of 'enabled' column by instance index
  175. /// @param inst The instance to get
  176. /// @return True when enabled else false
  177. ///
  178. inline bool isEnabled(quint8 inst)
  179. {
  180. VectorPair cond;
  181. cond.append(CPair("instance", inst));
  182. QVariantMap results;
  183. getRecord(cond, results);
  184. return results["enabled"].toBool();
  185. }
  186. private:
  187. ///
  188. /// @brief Create first Hyperion instance entry, if index 0 is not found.
  189. ///
  190. inline void createInstance()
  191. {
  192. if(instanceExist(0))
  193. setEnable(0, true);
  194. else
  195. {
  196. QVariantMap data;
  197. data["friendly_name"] = "First LED Hardware instance";
  198. VectorPair cond;
  199. cond.append(CPair("instance", 0));
  200. if(createRecord(cond, data))
  201. setEnable(0, true);
  202. else
  203. throw std::runtime_error("Failed to create Hyperion root instance in db! This should never be the case...");
  204. }
  205. }
  206. };