+ output_msg_status_bar(make_status_bar())
+}
+
+{
+ alert_check_all()
+}
+
+function alert_check_all() {
+ alert_check_energy()
+}
+
+# TODO: Generalize alert spec lang
+# - trigger threshold
+# - above/bellow/equal to threshold value
+# - priority
+# - snooze time (if already alerted, when to re-alert?)
+# - text: subject/body
+function alert_check_energy( state, remaining, subj, body) {
+ state = db["energy_state"]
+ remaining = db["energy_percentage"]
+ if (state == "discharging") {
+ if (remaining < 5) {
+ subj = "Energy_CRITICALLY_Low"
+ body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
+ alert_trigger_hi(subj, body)
+ } else if (remaining < 10) {
+ subj = "Energy_Very_Low"
+ body = sprintf("%d%% Plug it in ASAP.", remaining)
+ alert_trigger_hi(subj, body)
+ } else if (remaining < 15) {
+ subj = "Energy_Low"
+ body = sprintf("%d%% Get the charger.", remaining)
+ alert_trigger_hi(subj, body)
+ } else if (remaining < 50) {
+ if (!state__alerts__energy__notified_bellow_half) {
+ state__alerts__energy__notified_bellow_half = 1
+ subj = "Energy_Bellow_Half"
+ body = sprintf("%d%% Where is the charger?", remaining)
+ alert_trigger_hi(subj, body)
+ }
+ }
+ } else {
+ # TODO: Reconsider the competing global-state organizing-conventions
+ state__alerts__energy__notified_bellow_half = 0
+ }
+}
+
+function alert_trigger_low(subject, body) {
+ alert_trigger("low", subject, body)
+}
+
+function alert_trigger_med(subject, body) {
+ alert_trigger("med", subject, body)
+}
+
+function alert_trigger_hi(subject, body) {
+ alert_trigger("hi", subject, body)
+}
+
+function alert_trigger(priority, subject, body, msg) {
+ # priority : "low" | "med" | "hi"
+ # subject : no spaces
+ # body : anything
+ msg = sprintf("%s %s %s", priority, subject, body)
+ output_msg_alert(msg)
+}
+
+function output_msg_alert(msg) {
+ # TODO: Should alerts go into a dedicated channel?
+ output_msg("ALERT", msg, "/dev/stdout")
+}
+
+function output_msg_status_bar(msg) {
+ output_msg("STATUS_BAR", msg, "/dev/stdout")
+}
+
+function output_msg(type, content, channel) {
+ print(type, content) > channel