Rename split_msg_parts to fields_shift
[khatus.git] / bin / khatus_controller
index d75281e..1ff6e83 100755 (executable)
@@ -1,95 +1,98 @@
 #! /usr/bin/awk -f
 
+
 /^in:ENERGY/\
 {
-    split_msg_parts()
+    fields_shift()
     sub("%$", "", $2)
-    db["energy_state"]      = $1
-    db["energy_percentage"] = $2
+    db["energy_state_prev"] = db["energy_state_curr"]
+    db["energy_state_curr"] = $1
+    db["energy_percentage"] = ensure_numeric($2)
+    alert_check_energy()
 }
 
 /^in:MEMORY/\
 {
-    split_msg_parts()
+    fields_shift()
     db["memory_total"] = $1
     db["memory_used"]  = $2
 }
 
 /^in:FAN +status:/\
 {
-    split_msg_parts()
+    fields_shift()
     db["fan_status"] = $2
 }
 
 /^in:FAN +speed:/\
 {
-    split_msg_parts()
+    fields_shift()
     db["fan_speed"] = $2
 }
 
 /^in:FAN +level:/\
 {
-    split_msg_parts()
+    fields_shift()
     db["fan_level"] = $2
 }
 
 /^in:TEMPERATURE/\
 {
-    split_msg_parts()
+    fields_shift()
     db["temperature"] = $1
 }
 
 /^in:LOAD_AVG/\
 {
-    split_msg_parts()
+    fields_shift()
     set_load_avg()
 }
 
 /^in:DISK_IO/\
 {
-    split_msg_parts()
+    fields_shift()
     set_disk_io()
 }
 
 /^in:DISK_SPACE/\
 {
-    split_msg_parts()
-    db["disk_space_used"] = msg_body
+    fields_shift()
+    db["disk_space_used"] = $0
 }
 
 /^in:NET_ADDR_IO/\
 {
-    split_msg_parts()
+    fields_shift()
     set_net_addr_io()
 }
 
 /^in:NET_WIFI_STATUS/\
 {
-    split_msg_parts()
-    db["net_wifi_status"] = msg_body
+    fields_shift()
+    db["net_wifi_status"] = $0
 }
 
 /^in:BLUETOOTH_POWER/\
 {
-    split_msg_parts()
-    db["bluetooth_power"] = msg_body
+    fields_shift()
+    db["bluetooth_power"] = $0
 }
 
 /^in:SCREEN_BRIGHTNESS/\
 {
-    split_msg_parts()
+    fields_shift()
     set_screen_brightness()
 }
 
 /^in:VOLUME/\
 {
-    split_msg_parts()
-    db["volume"] = msg_body
+    fields_shift()
+    db["volume"] = $0
 }
 
 /^in:MPD_STATE/\
 {
-    split_msg_parts()
+    fields_shift()
     db["mpd_state"]             = $1
     db["mpd_curr_song_time"]    = $2
     db["mpd_curr_song_percent"] = $3
 
 /^in:MPD_SONG/\
 {
-    split_msg_parts()
-    db["mpd_curr_song_name"] = msg_body
+    fields_shift()
+    db["mpd_curr_song_name"] = $0
 }
 
 /^in:WEATHER/\
 {
-    split_msg_parts()
-    db["weather_temperature"] = msg_body
+    fields_shift()
+    db["weather_temperature"] = $0
 }
 
 /^in:DATE_TIME/\
 {
-    split_msg_parts()
-    db["datetime"] = msg_body
+    fields_shift()
+    db["datetime"] = $0
     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") {
+function alert_check_energy(    \
+    from, dbg, state_curr, state_prev, remaining, subj, body\
+) {
+    from = "alert_check_energy"
+
+    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 (state_prev == "charging") {
+            alert_trigger_low(from, "Unplugged", "")
+        }
+
         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 {
@@ -158,23 +171,23 @@ function alert_check_energy(    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)
 }
 
@@ -247,11 +260,11 @@ function set_screen_brightness(    max, cur) {
     db["screen_brightness"] = (cur / max) * 100
 }
 
-function split_msg_parts() {
-    msg_head = $1
-    sub("^" msg_head " +", "")
-    msg_body = $0
-    debug(msg_head, msg_body)
+function fields_shift(    dbg) {
+    dbg["head"] = $1
+    sub("^" $1 " +", "")
+    dbg["body"] = $0
+    debug("fields_shift", dbg)
 }
 
 function make_status_bar(    position, bar, sep, i, j) {
@@ -276,7 +289,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") {
@@ -284,7 +297,7 @@ function make_status_energy(    state, direction_of_change) {
     } else {
         direction_of_change = "="
     };
-    return sprintf("E%s%s%%", direction_of_change, db["energy_percentage"])
+    return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
 }
 
 function make_status_mem(    total, used, percent, status) {
@@ -399,8 +412,48 @@ function round(n) {
     return int(n + 0.5)
 }
 
-function debug(location, msg) {
+function debug(location, values,    sep, vals, key, msg) {
     if (opt_debug) {
-        output_msg("DEBUG", location " ==> " msg, "/dev/stderr")
+        sep = ""
+        vals = ""
+        for (key in values) {
+            vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
+            sep = ", "
+        }
+        msg = location " ==> " vals "."
+        output_msg("DEBUG", msg, "/dev/stderr")
     }
 }
+
+function ensure_numeric(n) {
+    return n + 0
+}
+#-------------------------------
+# Why do we need ensure_numeric?
+#-------------------------------
+# awk appears to be guessing the type of an inputted scalar based on usage, so
+# if we read-in a number, but did not use it in any numeric operations, but did
+# use as a string (even in just a format string!) - it will be treated as a
+# string and can lead to REALLY SURPRISING behavior in conditional statements,
+# where smaller number may compare as greater than the bigger ones, such as.
+#
+# Demo:
+#
+# $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %d, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 < 100
+# $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 > 100
+
+# However, once used as a number, seems to stay that way even after being
+# used as string:
+#
+# $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 < 100
+# 
+# $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 < 100
+# 
+# $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 < 100
+# $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
+# 75 > 100
This page took 0.043636 seconds and 4 git commands to generate.