Browse Source

fetch alarms in heartbeat

main
Stefan Pranger 3 years ago
parent
commit
7ded95e8e2
  1. 15
      calendar-daemon.cpp
  2. 30
      db/fetch_alarms.h

15
calendar-daemon.cpp

@ -16,13 +16,14 @@
#include <db/db.h> #include <db/db.h>
#include <db/create.h> #include <db/create.h>
#include <db/update.h> #include <db/update.h>
#include <db/fetch_alarms.h>
#include <db/FSWatcher.h> #include <db/FSWatcher.h>
#include <util/notifications.h> #include <util/notifications.h>
#include <util/calendar_parsing.h> #include <util/calendar_parsing.h>
#include <ical/IcalObject.h> #include <ical/IcalObject.h>
#include <io/debug.h> #include <io/debug.h>
const int MINUTE = 1000000;
const int MINUTE = 60; const int MINUTE = 60;
const int SLEEP_INTERVAL = 60 * MINUTE; const int SLEEP_INTERVAL = 60 * MINUTE;
@ -34,7 +35,7 @@ void update_database(std::string directory, sql::Database *db) {
std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory); std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory);
for(auto const& obj : objects) { for(auto const& obj : objects) {
if(obj->empty()) continue; 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); db::FSWatcher fswatcher(db, directory);
fswatcher.run(); fswatcher.run();
} }
void do_heartbeat(std::string directory, sql::Database *db) { 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 // 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 // Daemon-specific intialization should go here
const int SLEEP_INTERVAL = 5 * MINUTE;
std::string calendar = "dummy"; std::string calendar = "dummy";
std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/"; std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/";
sql::Database* db = init_database(calendar); sql::Database* db = init_database(calendar);
update_database(directory, db);
std::thread thread([&](){ init_fswatcher(directory, db); }); std::thread thread([&](){ init_fswatcher(directory, db); });
// Enter daemon loop // Enter daemon loop
while(1) while(1)
{ {
// Execute daemon heartbeat, where your recurring activity occurs
do_heartbeat(directory, db); do_heartbeat(directory, db);
exit(0);
// Sleep for a period of time
sleep(SLEEP_INTERVAL); sleep(SLEEP_INTERVAL);
} }

30
db/fetch_alarms.h

@ -0,0 +1,30 @@
#include <ctime>
#include <db/db.h>
#include <db/statements/builder/SelectStatementBuilder.h>
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<? AND 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;
}
}
}
Loading…
Cancel
Save