Add msg field to debug
[khatus.git] / bin / khatus_controller
index ae4a8dc..d91d49b 100755 (executable)
     db["volume"] = $0
 }
 
-/^in:MPD_SONG +file:/\
-{
-    fields_shift()
-    fields_shift()
-    db["mpd_song_curr_file"] = $0
-}
-
-/^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 +MPD/ { delete db_mpd_song; next }
+/^in:MPD_SONG OK$/     { set_mpd_playing() ; next }
+/^in:MPD_SONG /        { set_mpd_song()    ; next }
 
 /^in:MPD_STATE /\
 {
     output_msg_status_bar(make_status_bar())
 }
 
+function set_mpd_song(    key, val) {
+    key = $2
+    fields_shift()
+    fields_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
@@ -148,11 +182,11 @@ 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", "")
+            alert_trigger_low(from, "PowerUnplugged", "")
         }
 
         if (remaining < 5) {
@@ -278,7 +312,7 @@ function fields_shift(    dbg) {
     dbg["head"] = $1
     sub("^" $1 " +", "")
     dbg["body"] = $0
-    debug("fields_shift", dbg)
+    debug("fields_shift", "", dbg)
 }
 
 function make_status_bar(    position, bar, sep, i, j) {
@@ -408,29 +442,13 @@ function make_status_mpd(    state, status) {
     return sprintf("[%s]", status)
 }
 
-function make_status_mpd_state_known(symbol,    \
-    currently_playing, name, title, file\
-) {
-    name  = db["mpd_song_curr_name"]
-    title = db["mpd_song_curr_title:"]
-    file  = db["mpd_song_curr_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 = ""
-    }
+function make_status_mpd_state_known(symbol) {
     return sprintf(\
         "%s %s %s %s",
         symbol,
         db["mpd_status_time"],
         db["mpd_status_percent"],
-        substr(currently_playing, 1, opt_mpd_song_max_chars)\
+        substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\
     )
 }
 
@@ -442,7 +460,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 = ""
@@ -450,8 +468,8 @@ 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("[%s] [%s] [%s]", location, msg, vals)
+        output_msg("DEBUG", payload, "/dev/stderr")
     }
 }
 
This page took 0.025321 seconds and 4 git commands to generate.