From 7ded95e8e22117bee0b78922cd58466e48a04f8a Mon Sep 17 00:00:00 2001 From: Stefan Pranger Date: Fri, 2 Jul 2021 21:33:00 +0200 Subject: [PATCH] fetch alarms in heartbeat --- calendar-daemon.cpp | 15 ++++++--------- db/fetch_alarms.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 db/fetch_alarms.h diff --git a/calendar-daemon.cpp b/calendar-daemon.cpp index 781af00..18523d2 100644 --- a/calendar-daemon.cpp +++ b/calendar-daemon.cpp @@ -16,13 +16,14 @@ #include #include #include +#include #include #include #include #include #include -const int MINUTE = 1000000; + const int MINUTE = 60; const int SLEEP_INTERVAL = 60 * MINUTE; @@ -34,7 +35,7 @@ void update_database(std::string directory, sql::Database *db) { std::vector objects = util::parse_cal_dir(directory); for(auto const& obj : objects) { if(obj->empty()) continue; - db::insert_object(db, obj); + db::insert_events(db, obj); } } @@ -42,9 +43,9 @@ void init_fswatcher(std::string directory, sql::Database *db) { db::FSWatcher fswatcher(db, directory); fswatcher.run(); } + void do_heartbeat(std::string directory, sql::Database *db) { - util::notify("Updating files from " + directory, ""); - update_database(directory, db); + db::fetch_alarms(db, SLEEP_INTERVAL); } // For security purposes, we don't allow any arguments to be passed into the daemon @@ -106,22 +107,18 @@ int main(void) // Daemon-specific intialization should go here - const int SLEEP_INTERVAL = 5 * MINUTE; std::string calendar = "dummy"; std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/"; sql::Database* db = init_database(calendar); + update_database(directory, db); std::thread thread([&](){ init_fswatcher(directory, db); }); // Enter daemon loop while(1) { - // Execute daemon heartbeat, where your recurring activity occurs do_heartbeat(directory, db); - exit(0); - - // Sleep for a period of time sleep(SLEEP_INTERVAL); } diff --git a/db/fetch_alarms.h b/db/fetch_alarms.h new file mode 100644 index 0000000..6a29d6a --- /dev/null +++ b/db/fetch_alarms.h @@ -0,0 +1,30 @@ +#include + +#include +#include + + +namespace db { + void fetch_alarms(sql::Database *db, uint32_t interval) { + std::time_t now = std::time(nullptr); + stmt::Statement select_alarms = stmt::Statement::select().columns("dtstart, summary, description").from_table("alarms").inner_join("events", "events.rowid = alarms.eventID").where("dtstart?").close(); + DEBUG << select_alarms.str(); + try { + sql::Statement query(*db, select_alarms.str()); + query.bind(1, now + interval); + query.bind(2, now); + DEBUG << query.getExpandedSQL(); + while (query.executeStep()) + { + uint32_t dtstart = query.getColumn(0); + std::string summary = query.getColumn(1); + std::string description = query.getColumn(2); + DEBUG << dtstart << ", " << summary << ", " << description; + } + } + catch (std::exception& e) + { + std::cout << "Exception: " << e.what() << std::endl; + } + } +}