From 654ea6e2ddf5986f1975cd860575248c114102fe Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Tue, 7 Aug 2018 20:05:00 -0400 Subject: [PATCH] Alert on reported weather phenomena --- bin/khatus | 2 +- bin/khatus_controller | 11 ++++++++- bin/khatus_parse_metar_d_output | 44 +++++++++++++++++++++++++++++++++ bin/khatus_sensor_weather | 22 +++-------------- 4 files changed, 58 insertions(+), 21 deletions(-) create mode 100755 bin/khatus_parse_metar_d_output diff --git a/bin/khatus b/bin/khatus index 6ae08b0..48d3cef 100755 --- a/bin/khatus +++ b/bin/khatus @@ -138,7 +138,7 @@ main() { cmd_sens_screen_brightness='khatus_sensor_screen_brightness' cmd_sens_screen_brightness+=" $screen_brightness_device_path" - cmd_sens_weather="khatus_sensor_weather ${opts['--weather_station_id']}" + cmd_sens_weather="khatus_sensor_weather $bin ${opts['--weather_station_id']}" cmd_sens_disk_space="khatus_sensor_disk_space ${opts['--disk_space_device']}" cmd_sens_disk_io="khatus_sensor_disk_io ${opts['--disk_io_device']}" cmd_sens_temperature="khatus_sensor_temperature ${opts['--thermal_zone']}" diff --git a/bin/khatus_controller b/bin/khatus_controller index c67e640..587e37c 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -133,13 +133,22 @@ db["mpd_status_percent"] = $3 } -/^OK in:WEATHER/\ +/^OK in:WEATHER temperature/\ { + shift() shift() shift() db["weather_temperature"] = $0 } +/^OK in:WEATHER phenomena/\ +{ + shift() + shift() + shift() + alert_trigger_low("weather_phenomena", "WeatherPhenomena", $0) +} + /^OK in:DATE_TIME/\ { shift() diff --git a/bin/khatus_parse_metar_d_output b/bin/khatus_parse_metar_d_output new file mode 100755 index 0000000..49f1067 --- /dev/null +++ b/bin/khatus_parse_metar_d_output @@ -0,0 +1,44 @@ +#! /usr/bin/awk -f +# +# Qualifying the name as "_d_output" lest it be mistaken for parser of actual +# metar format. + +function strip(s) { + sub("^ *", "", s) + sub(" *$", "", s) + return s +} + +/METAR pattern not found in NOAA data/ { + failures++ + exit +} + +/[A-z][a-z]+ *: / { + split($0, line, ":") + key = strip(line[1]) + val = strip(line[2]) + values[NR] = val + first[key] = NR + last[key] = NR +} + +/^ +/ { + values[NR] = strip($0) + last[key] = NR +} + +END { + if (failures) { + print "metar fetch failed" > "/dev/stderr" + } else { + temp_string = values[first["Temperature"]] + split(temp_string, temp_parts, " +") + temp_celsius = temp_parts[1] + temp_fahrenheit = (temp_celsius * (9 / 5)) + 32 + print "temperature " temp_fahrenheit "°F" + for (i=first["Phenomena"]; i<=last["Phenomena"]; i++) { + print "phenomena " values[i] + } + } +} diff --git a/bin/khatus_sensor_weather b/bin/khatus_sensor_weather index 340175d..371010f 100755 --- a/bin/khatus_sensor_weather +++ b/bin/khatus_sensor_weather @@ -2,23 +2,7 @@ set -e -weather_station_id="$1" +dir_bin="$1" +weather_station_id="$2" -metar -d "$weather_station_id" 2>&1 \ -| awk ' - /METAR pattern not found in NOAA data/ { - failures++ - } - - /^Temperature/ { - celsius = $3; - fahrenheit = (celsius * (9 / 5)) + 32; - temperature = fahrenheit - } - - END { - if (failures > 0) { - temperature = "--" - } - print temperature "°F" - }' +metar -d "$weather_station_id" 2>&1 | "$dir_bin"/khatus_parse_metar_d_output -- 2.20.1