From: Siraaj Khandkar Date: Wed, 1 Aug 2018 19:16:32 +0000 (-0400) Subject: Split-out and refactor MPD status parser X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=e7a3c6ab3cc5fd8c89a483789cde8d1ca623aaba;p=khatus.git Split-out and refactor MPD status parser --- diff --git a/bin/khatus b/bin/khatus index 4f7b120..ead9dbd 100755 --- a/bin/khatus +++ b/bin/khatus @@ -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 index 0000000..1088b4e --- /dev/null +++ b/bin/khatus_parse_mpd_status @@ -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 +} diff --git a/bin/khatus_sensor_mpd_state b/bin/khatus_sensor_mpd_state index b99ab73..20cc49a 100755 --- a/bin/khatus_sensor_mpd_state +++ b/bin/khatus_sensor_mpd_state @@ -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