React-to, rather than poll-for, energy events
[khatus.git] / bin / khatus_controller
index ed98c34..fd60153 100755 (executable)
 #! /usr/bin/awk -f
 
+{ debug("LINE", $0) }
+
+/^in:ENERGY battery/\
+{
+    debug("ENERGY battery", $0)
+    sub("%$", "", $4)
+    db["energy_state_prev"] = db["energy_state_curr"]
+    db["energy_state_curr"] = $3
+    db["energy_percentage"] = ensure_numeric($4)
+}
+
+/^in:ENERGY line_power/\
+{
+    debug("ENERGY line_power", $0)
+    db["energy_line_power_prev"] = db["energy_line_power_curr"]
+    db["energy_line_power_curr"] = $3
+}
 
 /^in:ENERGY/\
 {
-    split_msg_parts()
-    sub("%$", "", $2)
-    db["energy_state"]      = $1
-    db["energy_percentage"] = ensure_numeric($2)
+    debug("ENERGY ALL", $0)
     alert_check_energy()
 }
 
 /^in:MEMORY/\
 {
-    split_msg_parts()
+    shift()
     db["memory_total"] = $1
     db["memory_used"]  = $2
 }
 
 /^in:FAN +status:/\
 {
-    split_msg_parts()
+    shift()
     db["fan_status"] = $2
 }
 
 /^in:FAN +speed:/\
 {
-    split_msg_parts()
+    shift()
     db["fan_speed"] = $2
 }
 
 /^in:FAN +level:/\
 {
-    split_msg_parts()
+    shift()
     db["fan_level"] = $2
 }
 
 /^in:TEMPERATURE/\
 {
-    split_msg_parts()
+    shift()
     db["temperature"] = $1
 }
 
 /^in:LOAD_AVG/\
 {
-    split_msg_parts()
+    shift()
     set_load_avg()
 }
 
 /^in:DISK_IO/\
 {
-    split_msg_parts()
+    shift()
     set_disk_io()
 }
 
 /^in:DISK_SPACE/\
 {
-    split_msg_parts()
-    db["disk_space_used"] = msg_body
+    shift()
+    db["disk_space_used"] = $0
 }
 
 /^in:NET_ADDR_IO/\
 {
-    split_msg_parts()
+    shift()
     set_net_addr_io()
 }
 
 /^in:NET_WIFI_STATUS/\
 {
-    split_msg_parts()
-    db["net_wifi_status"] = msg_body
+    shift()
+    db["net_wifi_status"] = $0
 }
 
 /^in:BLUETOOTH_POWER/\
 {
-    split_msg_parts()
-    db["bluetooth_power"] = msg_body
+    shift()
+    db["bluetooth_power"] = $0
 }
 
 /^in:SCREEN_BRIGHTNESS/\
 {
-    split_msg_parts()
+    shift()
     set_screen_brightness()
 }
 
 /^in:VOLUME/\
 {
-    split_msg_parts()
-    db["volume"] = msg_body
+    shift()
+    db["volume"] = $0
 }
 
-/^in:MPD_STATE/\
-{
-    split_msg_parts()
-    db["mpd_state"]             = $1
-    db["mpd_curr_song_time"]    = $2
-    db["mpd_curr_song_percent"] = $3
-}
+/^in:MPD_SONG OK +MPD/ { delete db_mpd_song; next }
+/^in:MPD_SONG OK$/     { set_mpd_playing() ; next }
+/^in:MPD_SONG /        { set_mpd_song()    ; next }
 
-/^in:MPD_SONG/\
+/^in:MPD_STATE /\
 {
-    split_msg_parts()
-    db["mpd_curr_song_name"] = msg_body
+    shift()
+    db["mpd_status_state"]   = $1
+    db["mpd_status_time"]    = $2
+    db["mpd_status_percent"] = $3
 }
 
 /^in:WEATHER/\
 {
-    split_msg_parts()
-    db["weather_temperature"] = msg_body
+    shift()
+    db["weather_temperature"] = $0
 }
 
 /^in:DATE_TIME/\
 {
-    split_msg_parts()
-    db["datetime"] = msg_body
+    shift()
+    db["datetime"] = $0
     output_msg_status_bar(make_status_bar())
 }
 
+function set_mpd_song(    key, val) {
+    key = $2
+    shift()
+    shift()
+    val = $0
+    db_mpd_song[key] = val
+    debug("set_mpd_song", "", db_mpd_song)
+}
+
+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(    threshold, dbg, state, remaining, subj, body) {
-    state     = db["energy_state"]
-    remaining = db["energy_percentage"]
+function alert_check_energy(    \
+    from, dbg, state_curr, state_prev, line_power_curr, line_power_prev, \
+    remaining, subj, body\
+) {
+    from = "alert_check_energy"
 
-    dbg["state"] = state
+    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("alert_check_energy", dbg)
+    dbg["energy_line_power_prev"] = db["energy_line_power_prev"]
+    dbg["energy_line_power_curr"] = db["energy_line_power_curr"]
+    debug(from, "", dbg)
 
-    if (state == "discharging") {
+    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", "")
+    }
+
+    if (state_curr == "discharging") {
         if (remaining < 5) {
             subj = "Energy_CRITICALLY_Low"
             body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
-            alert_trigger_hi(subj, body)
+            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(subj, body)
+            alert_trigger_hi(from, subj, body)
         } else if (remaining < 15) {
             subj = "Energy_Low"
             body = sprintf("%d%% Get the charger.", remaining)
-            alert_trigger_hi(subj, body)
+            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_hi(subj, body)
+                alert_trigger_med(from, subj, body)
             }
         }
     } else {
@@ -157,23 +238,23 @@ function alert_check_energy(    threshold, dbg, state, remaining, subj, body) {
     }
 }
 
-function alert_trigger_low(subject, body) {
-    alert_trigger("low", subject, body)
+function alert_trigger_low(from, subject, body) {
+    alert_trigger("low", from, subject, body)
 }
 
-function alert_trigger_med(subject, body) {
-    alert_trigger("med", subject, body)
+function alert_trigger_med(from, subject, body) {
+    alert_trigger("med", from, subject, body)
 }
 
-function alert_trigger_hi(subject, body) {
-    alert_trigger("hi", subject, body)
+function alert_trigger_hi(from, subject, body) {
+    alert_trigger("hi", from, subject, body)
 }
 
-function alert_trigger(priority, subject, body,    msg) {
+function alert_trigger(priority, from, subject, body,    msg) {
     # priority : "low" | "med" | "hi"
     # subject  : no spaces
     # body     : anything
-    msg = sprintf("%s %s %s", priority, subject, body)
+    msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body)
     output_msg_alert(msg)
 }
 
@@ -246,13 +327,9 @@ function set_screen_brightness(    max, cur) {
     db["screen_brightness"] = (cur / max) * 100
 }
 
-function split_msg_parts(    dbg) {
-    msg_head = $1
-    sub("^" msg_head " +", "")
-    msg_body = $0
-    dbg["msg_head"] = msg_head
-    dbg["msg_body"] = msg_body
-    debug("split_msg_parts", dbg)
+# TODO: Revise overuse of shift() where it is not really needed
+function shift() {
+    sub("^" $1 " +", "")
 }
 
 function make_status_bar(    position, bar, sep, i, j) {
@@ -277,7 +354,7 @@ function make_status_bar(    position, bar, sep, i, j) {
 }
 
 function make_status_energy(    state, direction_of_change) {
-    state = db["energy_state"]
+    state = db["energy_state_curr"]
     if (state == "discharging") {
         direction_of_change = "<"
     } else if (state == "charging") {
@@ -367,7 +444,7 @@ function make_status_net(    \
 }
 
 function make_status_mpd(    state, status) {
-    state = db["mpd_state"]
+    state = db["mpd_status_state"]
 
     if (state == "play") {
         status = make_status_mpd_state_known("▶")
@@ -386,9 +463,9 @@ function make_status_mpd_state_known(symbol) {
     return sprintf(\
         "%s %s %s %s",
         symbol,
-        db["mpd_curr_song_time"],
-        db["mpd_curr_song_percent"],
-        substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
+        db["mpd_status_time"],
+        db["mpd_status_percent"],
+        substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\
     )
 }
 
@@ -400,7 +477,7 @@ function round(n) {
     return int(n + 0.5)
 }
 
-function debug(location, values,    sep, vals, key, msg) {
+function debug(location, msg, values,    sep, vals, key, payload) {
     if (opt_debug) {
         sep = ""
         vals = ""
@@ -408,8 +485,9 @@ function debug(location, values,    sep, vals, key, msg) {
             vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
             sep = ", "
         }
-        msg = location " ==> " vals "."
-        output_msg("DEBUG", msg, "/dev/stderr")
+        payload = \
+            sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location, msg, vals)
+        output_msg("DEBUG", payload, "/dev/stderr")
     }
 }
 
This page took 0.04379 seconds and 4 git commands to generate.