From aaadf0f1b6695bd7f31ccc82848b01318a0192fb Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 3 Jun 2023 11:55:14 +0200 Subject: [PATCH] Initial commit --- .gitignore | 17 ++++++++++++ config.json | 6 +++++ dub.json | 14 ++++++++++ source/loadshedd/app.d | 56 +++++++++++++++++++++++++++++++++++++++ source/loadshedd/config.d | 39 +++++++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 dub.json create mode 100644 source/loadshedd/app.d create mode 100644 source/loadshedd/config.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9ec918 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.dub +docs.json +__dummy.html +docs/ +/loadshedd +loadshedd.so +loadshedd.dylib +loadshedd.dll +loadshedd.a +loadshedd.lib +loadshedd-test-* +*.exe +*.pdb +*.o +*.obj +*.lst +dub.selections.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..9055f90 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "server": "https://eskom-calendar-api.shuttleapp.rs/v0.0.1/", + "area": "western-cape-worscester", + "warnBefore": 1, + "refreshInterval" : 1 +} \ No newline at end of file diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..70fa88c --- /dev/null +++ b/dub.json @@ -0,0 +1,14 @@ +{ + "authors": [ + "Tristan B. Velloza Kildaire" + ], + "copyright": "Copyright © 2023, Tristan B. Velloza Kildaire", + "dependencies": { + "dlog": "~>0.0.6", + "eskomcalendar4d": "~>0.1.1", + "jstruct": "~>0.1.3" + }, + "description": "The loadshedding daemon for Saffers!", + "license": "GPL v3", + "name": "loadshedd" +} \ No newline at end of file diff --git a/source/loadshedd/app.d b/source/loadshedd/app.d new file mode 100644 index 0000000..08014ae --- /dev/null +++ b/source/loadshedd/app.d @@ -0,0 +1,56 @@ +module loadshedd.app; + +import loadshedd.config; +import std.stdio; +import eskomcalendar; +import core.thread : Thread, dur, Duration; +import std.datetime.systime : Clock, SysTime; + +void main() +{ + Config config = Config.fromJSON2(getJSON("config.json")); + EskomCalendar calendar = new EskomCalendar(config.server); + + while(true) + { + // Get schedules for your area today + Schedule[] todayShedding = calendar.getTodaySchedules(config.area); + writeln("Schedules for today:\n"); + + Schedule[] careAbouts; + foreach(Schedule schedule; todayShedding) + { + writeln(schedule); + + SysTime currentTime = Clock.currTime(); + currentTime += dur!("minutes")(config.warnBefore); + + // TODO: Figure out the algo + + + } + + writeln("End of schedules"); + + writeln("Care abouts: ", careAbouts); + + + Thread.sleep(dur!("minutes")(config.refreshInterval)); + } +} + +import std.json : parseJSON, JSONValue; +private JSONValue getJSON(string path) +{ + // JSONValue jsonConfig; + + File configFile; + configFile.open(path); + + byte[] data; + data.length = configFile.size(); + configFile.rawRead(data); + configFile.close(); + + return parseJSON(cast(string)data); +} diff --git a/source/loadshedd/config.d b/source/loadshedd/config.d new file mode 100644 index 0000000..3672e13 --- /dev/null +++ b/source/loadshedd/config.d @@ -0,0 +1,39 @@ +module loadshedd.config; + +import std.json : JSONValue, JSONException; +import jstruct; + +public struct Config +{ + public string server; + public string area; + public long warnBefore; + public long refreshInterval; + + public static fromJSON2(JSONValue json) + { + return fromJSON!(Config)(json); + } +} + +version(unittest) +{ + import std.json : parseJSON; + import std.stdio; +} + +unittest +{ + JSONValue testConfig = parseJSON(` + { + "server": "https://eskom-calendar-api.shuttleapp.rs/v0.0.1/", + "area": "western-cape-worscester", + "warnBefore": 1, + "refreshInterval" : 1 + } + `); + + Config config = Config.fromJSON2(testConfig); + writeln(config); + +} \ No newline at end of file