Re-factor MPD currentsong parsing
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 03:47:57 +0000 (23:47 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 03:47:57 +0000 (23:47 -0400)
bin/khatus_controller
bin/khatus_sensor_mpd_song

index 1ff6e83..ae4a8dc 100755 (executable)
     db["volume"] = $0
 }
 
-/^in:MPD_STATE/\
+/^in:MPD_SONG +file:/\
 {
     fields_shift()
-    db["mpd_state"]             = $1
-    db["mpd_curr_song_time"]    = $2
-    db["mpd_curr_song_percent"] = $3
+    fields_shift()
+    db["mpd_song_curr_file"] = $0
 }
 
-/^in:MPD_SONG/\
+/^in:MPD_SONG +Title:/\
 {
     fields_shift()
-    db["mpd_curr_song_name"] = $0
+    fields_shift()
+    db["mpd_song_curr_title"] = $0
+}
+/^in:MPD_SONG +Name:/\
+{
+    fields_shift()
+    fields_shift()
+    db["mpd_song_curr_name"] = $0
+}
+
+/^in:MPD_STATE /\
+{
+    fields_shift()
+    db["mpd_status_state"]   = $1
+    db["mpd_status_time"]    = $2
+    db["mpd_status_percent"] = $3
 }
 
 /^in:WEATHER/\
@@ -379,7 +393,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("▶")
@@ -394,13 +408,29 @@ function make_status_mpd(    state, status) {
     return sprintf("[%s]", status)
 }
 
-function make_status_mpd_state_known(symbol) {
+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 = ""
+    }
     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(currently_playing, 1, opt_mpd_song_max_chars)\
     )
 }
 
index 49f7f15..0d119d1 100755 (executable)
@@ -2,35 +2,4 @@
 
 set -e
 
-echo 'currentsong' \
-| nc 127.0.0.1 6600 \
-| awk '
-    /^OK/ {
-        next
-    }
-
-    {
-        key = $1
-        sub("^" key " +", "")
-        val = $0
-        data[key] = val
-    }
-
-    END {
-        name  = data["Name:"]
-        title = data["Title:"]
-        file  = data["file:"]
-
-        if (name) {
-            out = name
-        } else if (title) {
-            out = title
-        } else if (file) {
-            last = split(file, parts, "/")
-            out = parts[last]
-        } else {
-            out = ""
-        }
-        print out
-    }
-    '
+echo 'currentsong' | nc 127.0.0.1 6600
This page took 0.029951 seconds and 4 git commands to generate.