From: Siraaj Khandkar Date: Wed, 1 Aug 2018 18:28:04 +0000 (-0400) Subject: Split-out and refactor bluetooth status parser X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=fd22859bbb95b488a17e439e9e64de2eef00a04d;p=khatus.git Split-out and refactor bluetooth status parser --- diff --git a/bin/khatus b/bin/khatus index 00f2d9c..f13fc52 100755 --- a/bin/khatus +++ b/bin/khatus @@ -259,6 +259,8 @@ main() { cmd_sens_fan="khatus_sensor_fan $fan_path" + cmd_sens_bluetooth="khatus_sensor_bluetooth_power $dir_bin" + # TODO: Redirect each worker's stderr to a dedicated log file pipe="$file_pipe" log="$dir_logs"/khatus_sensors.log @@ -271,7 +273,7 @@ main() { fork_poller $interval_inp_mpd_state "$pipe" "$dir_bin" "$log" khatus_sensor_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" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' + fork_poller $interval_inp_bluetooth "$pipe" "$dir_bin" "$log" "$cmd_sens_bluetooth" 'in:BLUETOOTH_POWER' fork_poller $interval_inp_net_wifi "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' fork_poller $interval_inp_net_io "$pipe" "$dir_bin" "$log" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' fork_poller $interval_inp_disk_space "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_space" 'in:DISK_SPACE' diff --git a/bin/khatus_parse_bluetoothctl_show b/bin/khatus_parse_bluetoothctl_show new file mode 100755 index 0000000..08a8708 --- /dev/null +++ b/bin/khatus_parse_bluetoothctl_show @@ -0,0 +1,36 @@ +#! /usr/bin/awk -f + +/^Controller / { + controller = $2 + controllers[++ctrl_count] = controller +} + +/^\t[A-Z][A-Za-z]+:/ { + key = $1 + sub(":$", "", key) + sub("^" $1, "") + val = $0 + data[controller, key] = val +} + +END { + # Using the 1st seen controller. Should we select specific instead? + power_status = data[controllers[1], "Powered"] + if (ctrl_count > 0) { + if (power_status == "no") { + show = "off" + } else if (power_status == "yes") { + show = "on" + } else { + print_error("Unexpected bluetooth power status: " power_status) + show = "ERROR" + } + } else { + show = "off" # TODO: Perhaps use differentiated marker? + } + print(show) +} + +function print_error(msg) { + print(msg) > "/dev/stderr" +} diff --git a/bin/khatus_sensor_bluetooth_power b/bin/khatus_sensor_bluetooth_power index 92c2128..7810f7a 100755 --- a/bin/khatus_sensor_bluetooth_power +++ b/bin/khatus_sensor_bluetooth_power @@ -2,36 +2,6 @@ set -e -echo -e 'show \n quit' \ -| bluetoothctl \ -| awk ' - /^Controller / { - controller = $2; - controllers[++ctrl_count] = controller; - } - /^\t[A-Z][A-Za-z]+:/ { - key = $1; - sub(":$", "", key); - val = $2; - for (i=3; i<=NF; i++) { - val = val " " $i}; - data[controller, key] = val; - } - END { - # Using the 1st seen controller. Should we select specific instead? - power_status = data[controllers[1], "Powered"]; - if (ctrl_count > 0) { - if (power_status == "no") { - power_status = "off" - } else if (power_status == "yes") { - power_status = "on" - } else { - printf("Unexpected bluetooth power status: %s\n", power_status)\ - > "/dev/stderr"; - power_status = "ERROR" - } - } else { - power_status = "off" # TODO: Perhaps use differentiated marker? - } - printf("%s\n", power_status); - }' +dir_bin="$1" + +echo -e 'show \n quit' | bluetoothctl | "$dir_bin"/khatus_parse_bluetoothctl_show