Remove unused variable
[khatus.git] / bin / khatus_controller
index c4b9224..cbc6992 100755 (executable)
@@ -1,10 +1,13 @@
 #! /usr/bin/awk -f
 
+
 /^in:ENERGY/\
 {
     split_msg_parts()
+    sub("%$", "", $2)
     db["energy_state"]      = $1
-    db["energy_percentage"] = $2
+    db["energy_percentage"] = ensure_numeric($2)
+    alert_check_energy()
 }
 
 /^in:MEMORY/\
 {
     split_msg_parts()
     db["datetime"] = msg_body
-    print make_bar()
+    output_msg_status_bar(make_status_bar())
+}
+
+# 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(    dbg, state, remaining, subj, body) {
+    state     = db["energy_state"]
+    remaining = db["energy_percentage"]
+
+    dbg["state"] = state
+    dbg["remaining"] = remaining
+    debug("alert_check_energy", dbg)
+
+    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_med(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
 }
 
 function set_load_avg(    sched) {
@@ -169,14 +246,16 @@ function set_screen_brightness(    max, cur) {
     db["screen_brightness"] = (cur / max) * 100
 }
 
-function split_msg_parts() {
+function split_msg_parts(    dbg) {
     msg_head = $1
     sub("^" msg_head " +", "")
     msg_body = $0
-    debug(msg_head, msg_body)
+    dbg["msg_head"] = msg_head
+    dbg["msg_body"] = msg_body
+    debug("split_msg_parts", dbg)
 }
 
-function make_bar(    position, bar, sep, i, j) {
+function make_status_bar(    position, bar, sep, i, j) {
     position[++i] = make_status_energy()
     position[++i] = make_status_mem()
     position[++i] = make_status_cpu()
@@ -206,7 +285,7 @@ function make_status_energy(    state, direction_of_change) {
     } else {
         direction_of_change = "="
     };
-    printf("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) {
@@ -321,12 +400,48 @@ function round(n) {
     return int(n + 0.5)
 }
 
-function debug(location, msg) {
+function debug(location, values,    sep, vals, key, msg) {
     if (opt_debug) {
-        print_error(location, msg)
+        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 print_error(location, msg) {
-    print(location " ==> " 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.030584 seconds and 4 git commands to generate.