+function set_mpd_playing( \
+ currently_playing, name, title, file, last, parts\
+) {
+ debug("set_mpd_playing", "", db_mpd_song)
+ name = db_mpd_song["Name:"]
+ title = db_mpd_song["Title:"]
+ file = db_mpd_song["file:"]
+
+ if (name) {
+ currently_playing = name
+ } else if (title) {
+ currently_playing = title
+ } else if (file) {
+ last = split(file, parts, "/")
+ currently_playing = parts[last]
+ } else {
+ currently_playing = ""
+ }
+ db["mpd_playing_prev"] = db["mpd_playing_curr"]
+ db["mpd_playing_curr"] = currently_playing
+
+ alert_check_mpd()
+}
+
+function alert_check_mpd( curr, prev, name, body) {
+ prev = db["mpd_playing_prev"]
+ curr = db["mpd_playing_curr"]
+ if (curr && curr != prev) {
+ name = db_mpd_song["Name:"]
+ if (name) {
+ body = name
+ } else {
+ body = \
+ db_mpd_song["Artist:"] \
+ " - " db_mpd_song["Album:"] \
+ " - " db_mpd_song["Title:"]
+ }
+ alert_trigger_low("alert_check_mpd", "NowPlaying", body)
+ }
+}
+
+# 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_battery( \
+ from, dbg, state_curr, state_prev, remaining, subj, body\
+) {
+ from = "alert_check_energy_battery"
+
+ state_curr = db["energy_state_curr"]
+ state_prev = db["energy_state_prev"]
+ remaining = db["energy_percentage"]
+
+ dbg["state_curr"] = state_curr
+ dbg["remaining"] = remaining
+ debug(from, "", dbg)
+
+ if (state_curr == "discharging") {
+ if (remaining < 5) {
+ subj = "Energy_CRITICALLY_Low"
+ body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
+ alert_trigger_hi(from, subj, body)
+ } else if (remaining < 10) {
+ subj = "Energy_Very_Low"
+ body = sprintf("%d%% Plug it in ASAP.", remaining)
+ alert_trigger_hi(from, subj, body)
+ } else if (remaining < 15) {
+ subj = "Energy_Low"
+ body = sprintf("%d%% Get the charger.", remaining)
+ alert_trigger_hi(from, subj, body)
+ } else if (remaining < 20) {
+ subj = "Energy_Low"
+ body = sprintf("%d%% Get the charger.", remaining)
+ alert_trigger_med(from, 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_med(from, subj, body)
+ }
+ }
+ } else {
+ # TODO: Reconsider the competing global-state organizing-conventions
+ state__alerts__energy__notified_bellow_half = 0
+ }
+}
+
+function alert_check_energy_line_power( \
+ from, dbg, line_power_curr, line_power_prev, subj, body \
+) {
+ from = "alert_check_energy_line_power"
+
+ dbg["energy_line_power_prev"] = db["energy_line_power_prev"]
+ dbg["energy_line_power_curr"] = db["energy_line_power_curr"]
+ debug(from, "", dbg)
+
+ line_power_curr = db["energy_line_power_curr"]
+ line_power_prev = db["energy_line_power_prev"]
+
+ if (line_power_curr == "no" && line_power_prev != "no") {
+ alert_trigger_low(from, "PowerUnplugged", "")
+ }
+}
+
+function alert_trigger_low(from, subject, body) {
+ alert_trigger("low", from, subject, body)
+}
+
+function alert_trigger_med(from, subject, body) {
+ alert_trigger("med", from, subject, body)
+}
+
+function alert_trigger_hi(from, subject, body) {
+ alert_trigger("hi", from, subject, body)
+}
+
+function alert_trigger(priority, from, subject, body, msg) {
+ # priority : "low" | "med" | "hi"
+ # subject : no spaces
+ # body : anything
+ msg = sprintf("khatus_%s %s %s %s", from, 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