Split-out and refactor MPD status parser
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 19:16:32 +0000 (15:16 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 19:16:32 +0000 (15:16 -0400)
bin/khatus
bin/khatus_parse_mpd_status [new file with mode: 0755]
bin/khatus_sensor_mpd_state

index 4f7b120..ead9dbd 100755 (executable)
@@ -255,6 +255,7 @@ main() {
     cmd_sens_temperature="khatus_sensor_temperature $thermal_zone"
     cmd_sens_fan="khatus_sensor_fan $fan_path"
     cmd_sens_bluetooth="khatus_sensor_bluetooth_power $dir_bin"
+    cmd_sens_mpd_state="khatus_sensor_mpd_state $dir_bin"
 
     # TODO: Redirect each worker's stderr to a dedicated log file
     pipe="$file_pipe"
@@ -265,7 +266,7 @@ main() {
     fork_poller $interval_inp_datetime   "$pipe" "$dir_bin" "$log" khatus_sensor_datetime          'in:DATE_TIME'
     fork_poller $interval_inp_brightness "$pipe" "$dir_bin" "$log" "$cmd_sens_screen_brightness"   'in:SCREEN_BRIGHTNESS'
     fork_poller $interval_inp_weather    "$pipe" "$dir_bin" "$log" "$cmd_sens_weather"             'in:WEATHER'
-    fork_poller $interval_inp_mpd_state  "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_state         'in:MPD_STATE'
+    fork_poller $interval_inp_mpd_state  "$pipe" "$dir_bin" "$log" "$cmd_sens_mpd_state"           'in:MPD_STATE'
     fork_poller $interval_inp_mpd_song   "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_song          'in:MPD_SONG'
     fork_poller $interval_inp_volume     "$pipe" "$dir_bin" "$log" khatus_sensor_volume            'in:VOLUME'
     fork_poller $interval_inp_bluetooth  "$pipe" "$dir_bin" "$log" "$cmd_sens_bluetooth"           'in:BLUETOOTH_POWER'
diff --git a/bin/khatus_parse_mpd_status b/bin/khatus_parse_mpd_status
new file mode 100755 (executable)
index 0000000..1088b4e
--- /dev/null
@@ -0,0 +1,39 @@
+#! /usr/bin/awk -f
+
+# Msg BEGINs
+/^OK MPD / { delete status; next}
+
+# Msg ENDs
+/^OK$/ {
+    split(status["time"], time, ":")
+    seconds_current = time[1]
+    seconds_total   = time[2]
+
+    hours = int(seconds_current / 60 / 60);
+    secs_beyond_hours = seconds_current - (hours * 60 * 60);
+    mins = int(secs_beyond_hours / 60);
+    secs = secs_beyond_hours - (mins * 60);
+
+    if (hours > 0) {
+        current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
+    } else {
+        current_time = sprintf("%.2d:%.2d", mins, secs)
+    }
+
+    if (seconds_total > 0) {
+        time_percentage = (seconds_current / seconds_total) * 100
+        current_percentage = sprintf("%d%%", time_percentage)
+    } else {
+        current_percentage = "~"
+    }
+
+    printf("%s %s %s\n", status["state"], current_time, current_percentage)
+    next
+}
+
+# Msg content
+/^[a-z]+: / {
+    sub(":$", "", $1)
+    status[$1] = $2
+    next
+}
index b99ab73..20cc49a 100755 (executable)
@@ -2,40 +2,8 @@
 
 set -e
 
+dir_bin="$1"
+
 echo 'status' \
 | nc 127.0.0.1 6600 \
-| awk '
-    {
-        status[$1] = $2
-    }
-
-    /^time: +[0-9]+:[0-9]+$/ {
-        split($2, time, ":")
-        seconds_current = time[1]
-        seconds_total   = time[2]
-
-        hours = int(seconds_current / 60 / 60);
-        secs_beyond_hours = seconds_current - (hours * 60 * 60);
-        mins = int(secs_beyond_hours / 60);
-        secs = secs_beyond_hours - (mins * 60);
-        if (hours > 0) {
-            current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
-        } else {
-            current_time = sprintf("%.2d:%.2d", mins, secs)
-        }
-
-        if (seconds_total > 0) {
-            time_percentage = (seconds_current / seconds_total) * 100
-            current_percentage = sprintf("%d%%", time_percentage)
-        } else {
-            current_percentage = "~"
-        }
-    }
-
-    END {
-        printf(\
-            "%s %s %s\n",
-            status["state:"], current_time, current_percentage\
-        )
-    }
-    '
+| "$dir_bin"/khatus_parse_mpd_status
This page took 0.033105 seconds and 4 git commands to generate.