Split-out and refactor 'pactl list sinks' parser
[khatus.git] / bin / khatus_controller
index 836a915..c2e5d91 100755 (executable)
 #! /usr/bin/awk -f
 
+{ debug("LINE", $0) }
 
-/^in:ENERGY/\
+/^in:ENERGY battery/\
 {
-    fields_shift()
-    sub("%$", "", $2)
+    debug("ENERGY battery", $0)
+    sub("%$", "", $4)
     db["energy_state_prev"] = db["energy_state_curr"]
-    db["energy_state_curr"] = $1
-    db["energy_percentage"] = ensure_numeric($2)
-    alert_check_energy()
+    db["energy_state_curr"] = $3
+    db["energy_percentage"] = ensure_numeric($4)
+    alert_check_energy_battery()
+}
+
+/^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
+    alert_check_energy_line_power()
 }
 
 /^in:MEMORY/\
 {
-    fields_shift()
+    shift()
     db["memory_total"] = $1
     db["memory_used"]  = $2
 }
 
 /^in:FAN +status:/\
 {
-    fields_shift()
+    shift()
     db["fan_status"] = $2
 }
 
 /^in:FAN +speed:/\
 {
-    fields_shift()
+    shift()
     db["fan_speed"] = $2
 }
 
 /^in:FAN +level:/\
 {
-    fields_shift()
+    shift()
     db["fan_level"] = $2
 }
 
 /^in:TEMPERATURE/\
 {
-    fields_shift()
+    shift()
     db["temperature"] = $1
 }
 
 /^in:LOAD_AVG/\
 {
-    fields_shift()
+    shift()
     set_load_avg()
 }
 
 /^in:DISK_IO/\
 {
-    fields_shift()
+    shift()
     set_disk_io()
 }
 
 /^in:DISK_SPACE/\
 {
-    fields_shift()
+    shift()
     db["disk_space_used"] = $0
 }
 
 /^in:NET_ADDR_IO/\
 {
-    fields_shift()
+    shift()
     set_net_addr_io()
 }
 
 /^in:NET_WIFI_STATUS/\
 {
-    fields_shift()
+    shift()
     db["net_wifi_status"] = $0
 }
 
 /^in:BLUETOOTH_POWER/\
 {
-    fields_shift()
+    shift()
     db["bluetooth_power"] = $0
 }
 
 /^in:SCREEN_BRIGHTNESS/\
 {
-    fields_shift()
+    shift()
     set_screen_brightness()
 }
 
 /^in:VOLUME/\
 {
-    fields_shift()
-    db["volume"] = $0
-}
-
-# -----------------------------------------------------------------------------
-# BEGIN MPD_SONG
-/^in:MPD_SONG +OK +/\
-{
-    delete db["mpd_song_curr_file"]
-    delete db["mpd_song_curr_title"]
-    delete db["mpd_song_curr_name"]
-}
-
-/^in:MPD_SONG +file:/\
-{
-    fields_shift()
-    fields_shift()
-    db["mpd_song_curr_file"] = $0
+    set_volume()
 }
 
-/^in:MPD_SONG +Title:/\
-{
-    fields_shift()
-    fields_shift()
-    db["mpd_song_curr_title"] = $0
-}
-
-/^in:MPD_SONG +Name:/\
-{
-    fields_shift()
-    fields_shift()
-    db["mpd_song_curr_name"] = $0
-}
-
-/^in:MPD_SONG +OK$/\
-{
-    set_mpd_playing()
-}
-# END MPD_SONG
-# -----------------------------------------------------------------------------
+/^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_STATE /\
 {
-    fields_shift()
+    shift()
     db["mpd_status_state"]   = $1
     db["mpd_status_time"]    = $2
     db["mpd_status_percent"] = $3
 
 /^in:WEATHER/\
 {
-    fields_shift()
+    shift()
     db["weather_temperature"] = $0
 }
 
 /^in:DATE_TIME/\
 {
-    fields_shift()
+    shift()
     db["datetime"] = $0
     output_msg_status_bar(make_status_bar())
 }
 
+function set_volume(    mute, left, right) {
+    # 0 RUNNING no 75% 75%
+    #msg_head = $1
+    #sink     = $2
+    #state    = $3
+    mute      = $4
+    left      = $5
+    right     = $6
+
+    if (mute == "no") {
+        db["volume"] = sprintf("%s %s", left, right)
+    } else if (mute == "yes") {
+        db["volume"] = "X"
+    } else {
+        # TODO: Log error - unexpected mute value
+    }
+}
+
+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\
 ) {
-    name  = db["mpd_song_curr_name"]
-    title = db["mpd_song_curr_title"]
-    file  = db["mpd_song_curr_file"]
+    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
@@ -171,11 +174,20 @@ function set_mpd_playing(    \
     alert_check_mpd()
 }
 
-function alert_check_mpd(    curr, prev) {
+function alert_check_mpd(    curr, prev, name, body) {
     prev = db["mpd_playing_prev"]
     curr = db["mpd_playing_curr"]
-    if (curr != prev) {
-        alert_trigger_low("alert_check_mpd", "NowPlaying", 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)
     }
 }
 
@@ -185,10 +197,10 @@ function alert_check_mpd(    curr, prev) {
 #       - priority
 #       - snooze time (if already alerted, when to re-alert?)
 #       - text: subject/body
-function alert_check_energy(    \
+function alert_check_energy_battery(    \
     from, dbg, state_curr, state_prev, remaining, subj, body\
 ) {
-    from = "alert_check_energy"
+    from = "alert_check_energy_battery"
 
     state_curr = db["energy_state_curr"]
     state_prev = db["energy_state_prev"]
@@ -196,13 +208,9 @@ function alert_check_energy(    \
 
     dbg["state_curr"] = state_curr
     dbg["remaining"] = remaining
-    debug(from, dbg)
+    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)
@@ -233,6 +241,23 @@ function alert_check_energy(    \
     }
 }
 
+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)
 }
@@ -322,11 +347,9 @@ function set_screen_brightness(    max, cur) {
     db["screen_brightness"] = (cur / max) * 100
 }
 
-function fields_shift(    dbg) {
-    dbg["head"] = $1
+# TODO: Revise overuse of shift() where it is not really needed
+function shift() {
     sub("^" $1 " +", "")
-    dbg["body"] = $0
-    debug("fields_shift", dbg)
 }
 
 function make_status_bar(    position, bar, sep, i, j) {
@@ -474,7 +497,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 = ""
@@ -482,8 +505,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.043375 seconds and 4 git commands to generate.