SettingsTable.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. // hyperion
  3. #include <db/DBManager.h>
  4. // qt
  5. #include <QDateTime>
  6. #include <QJsonDocument>
  7. ///
  8. /// @brief settings table db interface
  9. ///
  10. class SettingsTable : public DBManager
  11. {
  12. public:
  13. /// construct wrapper with settings table
  14. SettingsTable(quint8 instance, QObject* parent = nullptr)
  15. : DBManager(parent)
  16. , _hyperion_inst(instance)
  17. {
  18. setTable("settings");
  19. // create table columns
  20. createTable(QStringList()<<"type TEXT"<<"config TEXT"<<"hyperion_inst INTEGER"<<"updated_at TEXT");
  21. };
  22. ///
  23. /// @brief Create or update a settings record
  24. /// @param[in] type type of setting
  25. /// @param[in] config The configuration data
  26. /// @return true on success else false
  27. ///
  28. inline bool createSettingsRecord(const QString& type, const QString& config) const
  29. {
  30. QVariantMap map;
  31. map["config"] = config;
  32. map["updated_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
  33. VectorPair cond;
  34. cond.append(CPair("type",type));
  35. // when a setting is not global we are searching also for the instance
  36. if(!isSettingGlobal(type))
  37. cond.append(CPair("AND hyperion_inst",_hyperion_inst));
  38. return createRecord(cond, map);
  39. }
  40. ///
  41. /// @brief Test if record exist, type can be global setting or local (instance)
  42. /// @param[in] type type of setting
  43. /// @param[in] hyperion_inst The instance of hyperion assigned (might be empty)
  44. /// @return true on success else false
  45. ///
  46. inline bool recordExist(const QString& type) const
  47. {
  48. VectorPair cond;
  49. cond.append(CPair("type",type));
  50. // when a setting is not global we are searching also for the instance
  51. if(!isSettingGlobal(type))
  52. cond.append(CPair("AND hyperion_inst",_hyperion_inst));
  53. return recordExists(cond);
  54. }
  55. ///
  56. /// @brief Get 'config' column of settings entry as QJsonDocument
  57. /// @param[in] type The settings type
  58. /// @return The QJsonDocument
  59. ///
  60. inline QJsonDocument getSettingsRecord(const QString& type) const
  61. {
  62. QVariantMap results;
  63. VectorPair cond;
  64. cond.append(CPair("type",type));
  65. // when a setting is not global we are searching also for the instance
  66. if(!isSettingGlobal(type))
  67. cond.append(CPair("AND hyperion_inst",_hyperion_inst));
  68. getRecord(cond, results, QStringList("config"));
  69. return QJsonDocument::fromJson(results["config"].toByteArray());
  70. }
  71. ///
  72. /// @brief Get 'config' column of settings entry as QString
  73. /// @param[in] type The settings type
  74. /// @return The QString
  75. ///
  76. inline QString getSettingsRecordString(const QString& type) const
  77. {
  78. QVariantMap results;
  79. VectorPair cond;
  80. cond.append(CPair("type",type));
  81. // when a setting is not global we are searching also for the instance
  82. if(!isSettingGlobal(type))
  83. cond.append(CPair("AND hyperion_inst",_hyperion_inst));
  84. getRecord(cond, results, QStringList("config"));
  85. return results["config"].toString();
  86. }
  87. ///
  88. /// @brief Delete all settings entries associated with this instance, called from InstanceTable of HyperionIManager
  89. ///
  90. inline void deleteInstance() const
  91. {
  92. VectorPair cond;
  93. cond.append(CPair("hyperion_inst",_hyperion_inst));
  94. deleteRecord(cond);
  95. }
  96. inline bool isSettingGlobal(const QString& type) const
  97. {
  98. // list of global settings
  99. QStringList list;
  100. // server port services
  101. list << "jsonServer" << "protoServer" << "flatbufServer" << "forwarder" << "webConfig" << "network"
  102. // capture
  103. << "framegrabber" << "grabberV4L2" << "grabberAudio"
  104. // other
  105. << "logger" << "general";
  106. return list.contains(type);
  107. }
  108. private:
  109. const quint8 _hyperion_inst;
  110. };