| 1 | BEGIN { |
| 2 | # TODO: Read spec from a file |
| 3 | bat_alert_spec[100] = "low|Energy_Bellow_Full|Must have perfection!" |
| 4 | bat_alert_spec[50] = "low|Energy_Bellow_Half|Where is the charger?" |
| 5 | bat_alert_spec[20] = "med|Energy_Low|Get the charger." |
| 6 | bat_alert_spec[15] = "med|Energy_Low|Get the charger!" |
| 7 | bat_alert_spec[10] = "hi|Energy_Low|Plug it in, ASAP!" |
| 8 | bat_alert_spec[5] = "hi|Energy_CRITICALLY_Low|CHARGE NOW!!! GO GO GO!!!" |
| 9 | } |
| 10 | |
| 11 | $1 == Node && \ |
| 12 | $2 == "khatus_sensor_energy" && \ |
| 13 | $3 == "data" && \ |
| 14 | $4 == "line_power" { |
| 15 | line_power_prev = line_power_curr |
| 16 | line_power_curr = $5 |
| 17 | if (line_power_curr == "no" && line_power_prev != "no") { |
| 18 | msg_out_alert_low("PowerUnplugged", "") |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | $1 == Node && \ |
| 23 | $2 == "khatus_sensor_energy" && \ |
| 24 | $3 == "data" && \ |
| 25 | $4 == "battery_state" { |
| 26 | battery_state_prev = battery_state_curr |
| 27 | battery_state_curr = $5 |
| 28 | } |
| 29 | |
| 30 | $1 == Node && \ |
| 31 | $2 == "khatus_sensor_energy" && \ |
| 32 | $3 == "data" && \ |
| 33 | $4 == "battery_percentage" { |
| 34 | # TODO: Re-think the spec - can't rely on order of keys |
| 35 | battery_percentage = num_ensure_numeric($5) |
| 36 | if (battery_state_curr == "discharging") { |
| 37 | for (threshold in bat_alert_spec) { |
| 38 | threshold = num_ensure_numeric(threshold) |
| 39 | if (battery_percentage <= threshold && !_alerted[threshold]) { |
| 40 | split(bat_alert_spec[threshold], alert, "|") |
| 41 | priority = alert[1] |
| 42 | subject = alert[2] |
| 43 | body = sprintf("%d%% %s", battery_percentage, alert[3]) |
| 44 | msg_out_alert(priority, subject, body) |
| 45 | _alerted[threshold]++ |
| 46 | } |
| 47 | } |
| 48 | } else { |
| 49 | delete _alerted |
| 50 | } |
| 51 | } |