From: Siraaj Khandkar Date: Wed, 1 Aug 2018 16:54:51 +0000 (-0400) Subject: React-to, rather than poll-for, energy events X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=ab99b55675644c1ec18bb6ecbd9479e001f604a1;p=khatus.git React-to, rather than poll-for, energy events --- diff --git a/bin/khatus b/bin/khatus index b58d2a2..7205aa9 100755 --- a/bin/khatus +++ b/bin/khatus @@ -14,7 +14,20 @@ consume() { -v opt_prefixes_of_net_interfaces_to_show="$prefixes_of_net_interfaces_to_show" } -spawn() { +spawn_reactor() { + pipe="$1" + dir_bin="$2" + log="$3" + cmd="$4" + msg_head="$5" + "$dir_bin"/$cmd | while read line; do + echo "${msg_head} $line" > "$pipe" + done \ + 2> "$log" \ + & +} + +spawn_poller() { pipe="$1" dir_bin="$2" log="$3" @@ -59,7 +72,6 @@ main() { interval_inp_temp=1 interval_inp_fan=1 interval_inp_mem=1 - interval_inp_energy=1 # User-overrides # --------------------------------------------------------------------- @@ -94,7 +106,6 @@ main() { long_options+=',interval_inp_temp:' long_options+=',interval_inp_fan:' long_options+=',interval_inp_mem:' - long_options+=',interval_inp_energy:' OPTS=$( getopt \ -o 'd' \ @@ -209,10 +220,6 @@ main() { interval_inp_mem="$2" shift 2 ;; - --interval_inp_energy) - interval_inp_energy="$2" - shift 2 - ;; --) shift break @@ -257,22 +264,24 @@ main() { # TODO: Redirect each worker's stderr to a dedicated log file pipe="$file_pipe" log="$dir_logs"/khatus_sensors.log - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_datetime 'in:DATE_TIME' $interval_inp_datetime - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' $interval_inp_brightness - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_weather" 'in:WEATHER' $interval_inp_weather - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_state 'in:MPD_STATE' $interval_inp_mpd_state - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_song 'in:MPD_SONG' $interval_inp_mpd_song - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_volume 'in:VOLUME' $interval_inp_volume - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' $interval_inp_bluetooth - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' $interval_inp_net_wifi - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' $interval_inp_net_io - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_space" 'in:DISK_SPACE' $interval_inp_disk_space - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_io" 'in:DISK_IO' $interval_inp_disk_io - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_loadavg 'in:LOAD_AVG' $interval_inp_loadavg - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_temperature" 'in:TEMPERATURE' $interval_inp_temp - spawn "$pipe" "$dir_bin" "$log" "$cmd_sens_fan" 'in:FAN' $interval_inp_fan - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_memory 'in:MEMORY' $interval_inp_mem - spawn "$pipe" "$dir_bin" "$log" khatus_sensor_energy 'in:ENERGY' $interval_inp_energy + + spawn_reactor "$pipe" "$dir_bin" "$log" "khatus_sensor_energy $dir_bin" 'in:ENERGY' + + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_datetime 'in:DATE_TIME' $interval_inp_datetime + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_screen_brightness" 'in:SCREEN_BRIGHTNESS' $interval_inp_brightness + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_weather" 'in:WEATHER' $interval_inp_weather + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_state 'in:MPD_STATE' $interval_inp_mpd_state + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_song 'in:MPD_SONG' $interval_inp_mpd_song + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_volume 'in:VOLUME' $interval_inp_volume + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_bluetooth_power 'in:BLUETOOTH_POWER' $interval_inp_bluetooth + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' $interval_inp_net_wifi + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_net_addr_io 'in:NET_ADDR_IO' $interval_inp_net_io + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_space" 'in:DISK_SPACE' $interval_inp_disk_space + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_io" 'in:DISK_IO' $interval_inp_disk_io + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_loadavg 'in:LOAD_AVG' $interval_inp_loadavg + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_temperature" 'in:TEMPERATURE' $interval_inp_temp + spawn_poller "$pipe" "$dir_bin" "$log" "$cmd_sens_fan" 'in:FAN' $interval_inp_fan + spawn_poller "$pipe" "$dir_bin" "$log" khatus_sensor_memory 'in:MEMORY' $interval_inp_mem consume \ "$pipe" \ diff --git a/bin/khatus_controller b/bin/khatus_controller index d91d49b..fd60153 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -1,92 +1,105 @@ #! /usr/bin/awk -f +{ debug("LINE", $0) } -/^in:ENERGY/\ +/^in:ENERGY battery/\ { - fields_shift() - sub("%$", "", $2) + debug("ENERGY battery", $0) + sub("%$", "", $4) db["energy_state_prev"] = db["energy_state_curr"] - db["energy_state_curr"] = $1 - db["energy_percentage"] = ensure_numeric($2) + db["energy_state_curr"] = $3 + db["energy_percentage"] = ensure_numeric($4) +} + +/^in:ENERGY line_power/\ +{ + debug("ENERGY line_power", $0) + db["energy_line_power_prev"] = db["energy_line_power_curr"] + db["energy_line_power_curr"] = $3 +} + +/^in:ENERGY/\ +{ + debug("ENERGY ALL", $0) alert_check_energy() } /^in:MEMORY/\ { - fields_shift() + shift() db["memory_total"] = $1 db["memory_used"] = $2 } /^in:FAN +status:/\ { - fields_shift() + shift() db["fan_status"] = $2 } /^in:FAN +speed:/\ { - fields_shift() + shift() db["fan_speed"] = $2 } /^in:FAN +level:/\ { - fields_shift() + shift() db["fan_level"] = $2 } /^in:TEMPERATURE/\ { - fields_shift() + shift() db["temperature"] = $1 } /^in:LOAD_AVG/\ { - fields_shift() + shift() set_load_avg() } /^in:DISK_IO/\ { - fields_shift() + shift() set_disk_io() } /^in:DISK_SPACE/\ { - fields_shift() + shift() db["disk_space_used"] = $0 } /^in:NET_ADDR_IO/\ { - fields_shift() + shift() set_net_addr_io() } /^in:NET_WIFI_STATUS/\ { - fields_shift() + shift() db["net_wifi_status"] = $0 } /^in:BLUETOOTH_POWER/\ { - fields_shift() + shift() db["bluetooth_power"] = $0 } /^in:SCREEN_BRIGHTNESS/\ { - fields_shift() + shift() set_screen_brightness() } /^in:VOLUME/\ { - fields_shift() + shift() db["volume"] = $0 } @@ -96,7 +109,7 @@ /^in:MPD_STATE /\ { - fields_shift() + shift() db["mpd_status_state"] = $1 db["mpd_status_time"] = $2 db["mpd_status_percent"] = $3 @@ -104,21 +117,21 @@ /^in:WEATHER/\ { - fields_shift() + shift() db["weather_temperature"] = $0 } /^in:DATE_TIME/\ { - fields_shift() + shift() db["datetime"] = $0 output_msg_status_bar(make_status_bar()) } function set_mpd_song( key, val) { key = $2 - fields_shift() - fields_shift() + shift() + shift() val = $0 db_mpd_song[key] = val debug("set_mpd_song", "", db_mpd_song) @@ -172,7 +185,8 @@ function alert_check_mpd( curr, prev, name, body) { # - snooze time (if already alerted, when to re-alert?) # - text: subject/body function alert_check_energy( \ - from, dbg, state_curr, state_prev, remaining, subj, body\ + from, dbg, state_curr, state_prev, line_power_curr, line_power_prev, \ + remaining, subj, body\ ) { from = "alert_check_energy" @@ -182,13 +196,18 @@ function alert_check_energy( \ dbg["state_curr"] = state_curr dbg["remaining"] = remaining + dbg["energy_line_power_prev"] = db["energy_line_power_prev"] + dbg["energy_line_power_curr"] = db["energy_line_power_curr"] debug(from, "", dbg) - if (state_curr == "discharging") { - if (state_prev == "charging") { - alert_trigger_low(from, "PowerUnplugged", "") - } + line_power_curr = db["energy_line_power_curr"] + line_power_prev = db["energy_line_power_prev"] + if (line_power_curr == "no" && line_power_prev != "no") { + alert_trigger_low(from, "PowerUnplugged", "") + } + + if (state_curr == "discharging") { if (remaining < 5) { subj = "Energy_CRITICALLY_Low" body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining) @@ -308,11 +327,9 @@ function set_screen_brightness( max, cur) { db["screen_brightness"] = (cur / max) * 100 } -function fields_shift( dbg) { - dbg["head"] = $1 +# TODO: Revise overuse of shift() where it is not really needed +function shift() { sub("^" $1 " +", "") - dbg["body"] = $0 - debug("fields_shift", "", dbg) } function make_status_bar( position, bar, sep, i, j) { @@ -468,7 +485,8 @@ function debug(location, msg, values, sep, vals, key, payload) { vals = sprintf("%s%s%s: %s", vals, sep, key, values[key]) sep = ", " } - payload = sprintf("[%s] [%s] [%s]", location, msg, vals) + payload = \ + sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location, msg, vals) output_msg("DEBUG", payload, "/dev/stderr") } } diff --git a/bin/khatus_parse_upower b/bin/khatus_parse_upower new file mode 100755 index 0000000..6761508 --- /dev/null +++ b/bin/khatus_parse_upower @@ -0,0 +1,55 @@ +#! /usr/bin/awk -f + +# When parsing 'upower --dump' +/^Device:[ \t]+/ { + device["path"] = $2 + next +} + +# When parsing 'upower --monitor-detail' +/^\[[0-9]+:[0-9]+:[0-9]+\.[0-9]+\][ \t]+device changed:[ \t]+/ { + device["path"] = $4 + next +} + +# BEGIN battery +/ battery/ && device["path"] { + device["is_battery"] = 1 + next +} + +/ state:/ && device["is_battery"] { + device["battery_state"] = $2 + next +} + +/ percentage:/ && device["is_battery"] { + device["battery_percentage"] = $2 + next +} + +/^$/ && device["is_battery"] { + printf("battery %s %s\n", device["battery_state"], device["battery_percentage"]) +} +# END battery + +# BEGIN line-power +/ line-power/ && device["path"] { + device["is_line_power"] = 1 + next +} + +/ online:/ && device["is_line_power"] { + device["line_power_online"] = $2 + next +} + +/^$/ && device["is_line_power"] { + printf("line_power %s\n", device["line_power_online"]) +} +# END line-power + +/^$/ { + delete device + next +} diff --git a/bin/khatus_sensor_energy b/bin/khatus_sensor_energy index f91155f..068ab81 100755 --- a/bin/khatus_sensor_energy +++ b/bin/khatus_sensor_energy @@ -2,11 +2,7 @@ set -e -upower -e \ -| grep battery \ -| xargs upower -i \ -| awk ' - /^ +percentage: +/ { percentage=$2 } - /^ +state: +/ { state=$2 } - END { print(state, percentage) } - ' +dir_bin="$1" + +upower --dump | stdbuf -o L "$dir_bin"/khatus_parse_upower +upower --monitor-detail | stdbuf -o L "$dir_bin"/khatus_parse_upower