Split-out and refactor bluetooth status parser
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 18:28:04 +0000 (14:28 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 18:28:04 +0000 (14:28 -0400)
bin/khatus
bin/khatus_parse_bluetoothctl_show [new file with mode: 0755]
bin/khatus_sensor_bluetooth_power

index 00f2d9c..f13fc52 100755 (executable)
@@ -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 (executable)
index 0000000..08a8708
--- /dev/null
@@ -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"
+}
index 92c2128..7810f7a 100755 (executable)
@@ -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
This page took 0.03267 seconds and 4 git commands to generate.