M changelog.md => changelog.md +3 -1
@@ 5,10 5,12 @@
### Added
* `[gui]` Added GUI timers capability
-* `[fonts]` Added multiplication and division sign to the font files
+* `[fonts]` Added multiplication and division sign to the font files
* `[bluetooth]` Added storing of BT link keys.
* `[audio]` AudioMux tests
* `[audio]` Added vibration logic (effect is visible in logs only)
+* `[settings]` Added DatabaseAgent base class and SettingsAgent.
+
### Changed
M module-services/service-db/CMakeLists.txt => module-services/service-db/CMakeLists.txt +4 -0
@@ 21,6 21,10 @@ target_sources( ${PROJECT_NAME}
"messages/DBSMSTemplateMessage.cpp"
"messages/DBThreadMessage.cpp"
"messages/QueryMessage.cpp"
+
+ "agents/DatabaseAgent.cpp"
+ "agents/settings/SettingsAgent.cpp"
+
)
if (${ENABLE_TESTS})
M module-services/service-db/ServiceDB.cpp => module-services/service-db/ServiceDB.cpp +8 -0
@@ 609,6 609,14 @@ sys::ReturnCodes ServiceDB::InitHandler()
notificationsRecordInterface = std::make_unique<NotificationsRecordInterface>(notificationsDB.get());
eventsRecordInterface = std::make_unique<EventsRecordInterface>(eventsDB.get());
settingsRecordInterface_v2 = std::make_unique<SettingsRecordInterface_v2>(settingsDB.get());
+
+ databaseAgents.emplace(std::make_unique<SettingsAgent>(this));
+
+ for (auto &dbAgent : databaseAgents) {
+ dbAgent->initDb();
+ dbAgent->registerMessages();
+ }
+
return sys::ReturnCodes::Success;
}
M module-services/service-db/ServiceDB.hpp => module-services/service-db/ServiceDB.hpp +3 -1
@@ 15,6 15,8 @@
#include "Service/Message.hpp"
#include "Service/Service.hpp"
#include "messages/DBNotificationMessage.hpp"
+#include "agents/DatabaseAgent.hpp"
+#include "agents/settings/SettingsAgent.hpp"
class ServiceDB : public sys::Service
{
@@ 44,6 46,7 @@ class ServiceDB : public sys::Service
protected:
db::Interface *getInterface(db::Interface::Name interface);
+ std::set<std::unique_ptr<DatabaseAgent>> databaseAgents;
public:
ServiceDB();
@@ 60,4 63,3 @@ class ServiceDB : public sys::Service
bool StoreIntoBackup(const std::string &backupPath);
void sendUpdateNotification(db::Interface::Name interface, db::Query::Type type);
};
-
A module-services/service-db/agents/DatabaseAgent.cpp => module-services/service-db/agents/DatabaseAgent.cpp +4 -0
@@ 0,0 1,4 @@
+#include "DatabaseAgent.hpp"
+
+DatabaseAgent::DatabaseAgent(sys::Service *parentService) : parentService(parentService)
+{}
A module-services/service-db/agents/DatabaseAgent.hpp => module-services/service-db/agents/DatabaseAgent.hpp +26 -0
@@ 0,0 1,26 @@
+#pragma once
+
+#include <Database/Database.hpp>
+#include <Service/Service.hpp>
+
+#include <string>
+
+class Database;
+
+class DatabaseAgent
+{
+ public:
+ DatabaseAgent(sys::Service *parentService);
+
+ virtual void initDb() = 0;
+ virtual void deinitDb() = 0;
+ virtual void registerMessages() = 0;
+ [[nodiscard]] virtual auto getAgentName() -> const std::string = 0;
+
+ protected:
+ [[nodiscard]] virtual auto getDbInitString() -> const std::string = 0;
+ [[nodiscard]] virtual auto getDbFilePath() -> const std::string = 0;
+
+ sys::Service *parentService;
+ std::unique_ptr<Database> database;
+};
A module-services/service-db/agents/settings/SettingsAgent.cpp => module-services/service-db/agents/settings/SettingsAgent.cpp +94 -0
@@ 0,0 1,94 @@
+#include "SettingsAgent.hpp"
+
+#include <module-sys/Service/Bus.hpp>
+#include <module-vfs/vfs.hpp>
+
+#include <log/log.hpp>
+
+#include <memory>
+
+SettingsAgent::SettingsAgent(sys::Service *parentService) : DatabaseAgent(parentService)
+{
+ database = std::make_unique<Database>(getDbFilePath().c_str());
+}
+
+void SettingsAgent::initDb()
+{
+ // LOG_DEBUG("sql:\n--\n%s\n--", getDbInitString().c_str());
+ database->execute(getDbInitString().c_str());
+}
+
+void SettingsAgent::deinitDb()
+{
+ database->deinitialize();
+}
+
+void SettingsAgent::registerMessages()
+{
+ // connect handler & message in parent service
+ using std::placeholders::_1;
+ using std::placeholders::_2;
+
+ parentService->connect(Settings::Messages::GetVariable(),
+ std::bind(&SettingsAgent::handleGetVariable, this, _1, _2));
+ parentService->connect(Settings::Messages::SetVariable(),
+ std::bind(&SettingsAgent::handleSetVariable, this, _1, _2));
+}
+
+auto SettingsAgent::getDbInitString() -> const std::string
+{
+ auto x = 0;
+ const char *sql = (
+#include "settings.sql"
+ );
+ return sql;
+}
+
+auto SettingsAgent::getDbFilePath() -> const std::string
+{
+
+ return USER_PATH("settings_v2.db");
+}
+auto SettingsAgent::getAgentName() -> const std::string
+{
+ return std::string("settingsAgent");
+}
+
+auto SettingsAgent::getValue(Settings::EntryPath path) -> std::optional<std::string>
+{
+ // get value from db from specified path
+ return "";
+}
+
+auto SettingsAgent::setValue(Settings::EntryPath path, std::string value) -> std::string
+{
+ // set value into db
+ // return old value
+ return "";
+}
+
+auto SettingsAgent::handleGetVariable(sys::DataMessage *req, sys::ResponseMessage * /*response*/) -> sys::Message_t
+{
+ if (auto msg = dynamic_cast<Settings::Messages::GetVariable *>(req)) {
+
+ auto path = msg->getPath();
+ auto value = getValue(path);
+
+ return std::make_shared<Settings::Messages::VariableResponse>(path, value);
+ }
+ return std::make_shared<sys::ResponseMessage>();
+};
+
+auto SettingsAgent::handleSetVariable(sys::DataMessage *req, sys::ResponseMessage * /*response*/) -> sys::Message_t
+{
+ if (auto msg = dynamic_cast<Settings::Messages::SetVariable *>(req)) {
+
+ auto path = msg->getPath();
+ auto value = msg->getValue().value_or("");
+ auto oldValue = setValue(path, msg->getValue().value_or(""));
+
+ auto updateMsg = std::make_shared<Settings::Messages::VariableChanged>(path, value, oldValue);
+ sys::Bus::SendUnicast(std::move(updateMsg), "db-worker", parentService);
+ }
+ return std::make_shared<sys::ResponseMessage>();
+};
A module-services/service-db/agents/settings/SettingsAgent.hpp => module-services/service-db/agents/settings/SettingsAgent.hpp +23 -0
@@ 0,0 1,23 @@
+#pragma once
+
+#include "messages/SettingsMessages.hpp"
+#include "agents/DatabaseAgent.hpp"
+
+class SettingsAgent : public DatabaseAgent
+{
+ public:
+ SettingsAgent(sys::Service *parentService);
+
+ void initDb() override;
+ void deinitDb() override;
+ void registerMessages() override;
+ auto getAgentName() -> const std::string override;
+
+ private:
+ auto getValue(Settings::EntryPath path) -> std::optional<std::string>;
+ auto setValue(Settings::EntryPath path, std::string value) -> std::string;
+ auto getDbInitString() -> const std::string override;
+ auto getDbFilePath() -> const std::string override;
+ auto handleGetVariable(sys::DataMessage *req, sys::ResponseMessage *) -> sys::Message_t;
+ auto handleSetVariable(sys::DataMessage *req, sys::ResponseMessage *) -> sys::Message_t;
+};
R module-services/service-db/workers/settings/settings.sql => module-services/service-db/agents/settings/settings.sql +6 -4
@@ 1,3 1,4 @@
+--x, R"dbInitStr(
/*
* Create Settings tables
*/
@@ 38,11 39,12 @@ CREATE TABLE IF NOT EXISTS notifications_tab (
-- ----------- insert default values ----------------------
INSERT OR REPLACE INTO dictionary_tab (path, value) VALUES
- ("system/phone_mode", "online"),
- ("system/phone_mode", "offline"),
- ("system/phone_mode", "dnd");
+ ('system/phone_mode', 'online'),
+ ('system/phone_mode', 'offline'),
+ ('system/phone_mode', 'dnd');
INSERT OR REPLACE INTO settings_tab (path, value) VALUES
- ("system/phone_mode", "online");
+ ('system/phone_mode', 'online');
COMMIT TRANSACTION;
+-- )dbInitStr"