Split actuator into 2
[khatus.git] / bin / khatus_controller
CommitLineData
f37162a4
SK
1#! /usr/bin/awk -f
2
688fb20e 3/^OK/ { debug("OK line", $0) }
e16fe0ea 4
688fb20e
SK
5/^ERROR/ {
6 debug("ERROR line", $0)
7 shift()
8 msg_head = $1
9 shift()
10 msg_body = $0
11 alert_trigger_hi(msg_head, "KhatusSensorError", msg_body)
12}
13
14/^OK in:ENERGY battery/\
f37162a4 15{
ab99b556 16 debug("ENERGY battery", $0)
688fb20e 17 sub("%$", "", $5)
7851644f 18 db["energy_state_prev"] = db["energy_state_curr"]
688fb20e
SK
19 db["energy_state_curr"] = $4
20 db["energy_percentage"] = ensure_numeric($5)
f03e88c6 21 alert_check_energy_battery()
ab99b556
SK
22}
23
688fb20e 24/^OK in:ENERGY line_power/\
ab99b556
SK
25{
26 debug("ENERGY line_power", $0)
27 db["energy_line_power_prev"] = db["energy_line_power_curr"]
688fb20e 28 db["energy_line_power_curr"] = $4
f03e88c6 29 alert_check_energy_line_power()
f37162a4
SK
30}
31
688fb20e 32/^OK in:MEMORY/\
f37162a4 33{
688fb20e 34 shift()
ab99b556 35 shift()
f37162a4
SK
36 db["memory_total"] = $1
37 db["memory_used"] = $2
38}
39
688fb20e 40/^OK in:FAN +status:/\
f37162a4 41{
688fb20e 42 shift()
ab99b556 43 shift()
f37162a4
SK
44 db["fan_status"] = $2
45}
46
688fb20e 47/^OK in:FAN +speed:/\
f37162a4 48{
688fb20e 49 shift()
ab99b556 50 shift()
f37162a4
SK
51 db["fan_speed"] = $2
52}
53
688fb20e 54/^OK in:FAN +level:/\
f37162a4 55{
688fb20e 56 shift()
ab99b556 57 shift()
f37162a4
SK
58 db["fan_level"] = $2
59}
60
688fb20e 61/^OK in:TEMPERATURE/\
f37162a4 62{
688fb20e 63 shift()
ab99b556 64 shift()
f37162a4
SK
65 db["temperature"] = $1
66}
67
688fb20e 68/^OK in:LOAD_AVG/\
f37162a4 69{
688fb20e 70 shift()
ab99b556 71 shift()
f37162a4
SK
72 set_load_avg()
73}
74
688fb20e 75/^OK in:DISK_IO/\
f37162a4 76{
688fb20e 77 shift()
ab99b556 78 shift()
f37162a4
SK
79 set_disk_io()
80}
81
688fb20e 82/^OK in:DISK_SPACE/\
f37162a4 83{
688fb20e 84 shift()
ab99b556 85 shift()
9419890b 86 db["disk_space_used"] = $0
f37162a4
SK
87}
88
688fb20e 89/^OK in:NET_ADDR_IO/\
f37162a4 90{
688fb20e 91 shift()
ab99b556 92 shift()
f37162a4
SK
93 set_net_addr_io()
94}
95
688fb20e 96/^OK in:NET_WIFI_STATUS/\
f37162a4 97{
688fb20e 98 shift()
ab99b556 99 shift()
9419890b 100 db["net_wifi_status"] = $0
f37162a4
SK
101}
102
688fb20e 103/^OK in:BLUETOOTH_POWER/\
f37162a4 104{
688fb20e 105 shift()
ab99b556 106 shift()
9419890b 107 db["bluetooth_power"] = $0
f37162a4
SK
108}
109
688fb20e 110/^OK in:SCREEN_BRIGHTNESS/\
f37162a4 111{
688fb20e 112 shift()
ab99b556 113 shift()
f37162a4
SK
114 set_screen_brightness()
115}
116
688fb20e 117/^OK in:VOLUME/\
f37162a4 118{
688fb20e 119 shift()
d06e7260 120 set_volume()
f37162a4
SK
121}
122
688fb20e
SK
123/^OK in:MPD_SONG OK +MPD/ { delete db_mpd_song; next }
124/^OK in:MPD_SONG OK$/ { set_mpd_playing() ; next }
125/^OK in:MPD_SONG / { set_mpd_song() ; next }
f5dfebd2 126
688fb20e 127/^OK in:MPD_STATE /\
a3bb1cff 128{
688fb20e 129 shift()
ab99b556 130 shift()
a3bb1cff
SK
131 db["mpd_status_state"] = $1
132 db["mpd_status_time"] = $2
133 db["mpd_status_percent"] = $3
f37162a4
SK
134}
135
688fb20e 136/^OK in:WEATHER/\
f37162a4 137{
688fb20e 138 shift()
ab99b556 139 shift()
9419890b 140 db["weather_temperature"] = $0
f37162a4
SK
141}
142
688fb20e 143/^OK in:DATE_TIME/\
f37162a4 144{
688fb20e 145 shift()
ab99b556 146 shift()
9419890b 147 db["datetime"] = $0
cae87802
SK
148 output_msg_status_bar(make_status_bar())
149}
150
d06e7260
SK
151function set_volume( mute, left, right) {
152 # 0 RUNNING no 75% 75%
153 #msg_head = $1
154 #sink = $2
155 #state = $3
156 mute = $4
157 left = $5
158 right = $6
159
160 if (mute == "no") {
161 db["volume"] = sprintf("%s %s", left, right)
162 } else if (mute == "yes") {
163 db["volume"] = "X"
164 } else {
0684c786 165 error("set_volume", "Unexpected value for 'mute' field: " mute)
d06e7260
SK
166 }
167}
168
30b9a4e1 169function set_mpd_song( key, val) {
688fb20e 170 shift()
30b9a4e1 171 key = $2
ab99b556
SK
172 shift()
173 shift()
30b9a4e1
SK
174 val = $0
175 db_mpd_song[key] = val
bb1e4fee 176 debug("set_mpd_song", "", db_mpd_song)
30b9a4e1
SK
177}
178
f5dfebd2
SK
179function set_mpd_playing( \
180 currently_playing, name, title, file, last, parts\
181) {
bb1e4fee 182 debug("set_mpd_playing", "", db_mpd_song)
30b9a4e1
SK
183 name = db_mpd_song["Name:"]
184 title = db_mpd_song["Title:"]
185 file = db_mpd_song["file:"]
f5dfebd2
SK
186
187 if (name) {
188 currently_playing = name
189 } else if (title) {
190 currently_playing = title
191 } else if (file) {
192 last = split(file, parts, "/")
193 currently_playing = parts[last]
194 } else {
195 currently_playing = ""
196 }
197 db["mpd_playing_prev"] = db["mpd_playing_curr"]
198 db["mpd_playing_curr"] = currently_playing
199
200 alert_check_mpd()
201}
202
30b9a4e1 203function alert_check_mpd( curr, prev, name, body) {
f5dfebd2
SK
204 prev = db["mpd_playing_prev"]
205 curr = db["mpd_playing_curr"]
e8c1404e 206 if (curr && curr != prev) {
30b9a4e1
SK
207 name = db_mpd_song["Name:"]
208 if (name) {
209 body = name
210 } else {
211 body = \
212 db_mpd_song["Artist:"] \
213 " - " db_mpd_song["Album:"] \
214 " - " db_mpd_song["Title:"]
215 }
43e49903 216 alert_trigger_low("alert_check_mpd", "MpdNowPlaying", body)
f5dfebd2
SK
217 }
218}
219
cae87802
SK
220# TODO: Generalize alert spec lang
221# - trigger threshold
222# - above/bellow/equal to threshold value
223# - priority
224# - snooze time (if already alerted, when to re-alert?)
225# - text: subject/body
f03e88c6
SK
226function alert_check_energy_battery( \
227 from, dbg, state_curr, state_prev, remaining, subj, body\
7851644f 228) {
f03e88c6 229 from = "alert_check_energy_battery"
d4c26c51 230
7851644f
SK
231 state_curr = db["energy_state_curr"]
232 state_prev = db["energy_state_prev"]
233 remaining = db["energy_percentage"]
e16fe0ea 234
7851644f 235 dbg["state_curr"] = state_curr
e16fe0ea 236 dbg["remaining"] = remaining
bb1e4fee 237 debug(from, "", dbg)
e16fe0ea 238
ab99b556 239 if (state_curr == "discharging") {
cae87802
SK
240 if (remaining < 5) {
241 subj = "Energy_CRITICALLY_Low"
242 body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining)
d4c26c51 243 alert_trigger_hi(from, subj, body)
cae87802
SK
244 } else if (remaining < 10) {
245 subj = "Energy_Very_Low"
246 body = sprintf("%d%% Plug it in ASAP.", remaining)
d4c26c51 247 alert_trigger_hi(from, subj, body)
cae87802
SK
248 } else if (remaining < 15) {
249 subj = "Energy_Low"
250 body = sprintf("%d%% Get the charger.", remaining)
d4c26c51 251 alert_trigger_hi(from, subj, body)
361523eb
SK
252 } else if (remaining < 20) {
253 subj = "Energy_Low"
254 body = sprintf("%d%% Get the charger.", remaining)
255 alert_trigger_med(from, subj, body)
cae87802
SK
256 } else if (remaining < 50) {
257 if (!state__alerts__energy__notified_bellow_half) {
258 state__alerts__energy__notified_bellow_half = 1
259 subj = "Energy_Bellow_Half"
260 body = sprintf("%d%% Where is the charger?", remaining)
d4c26c51 261 alert_trigger_med(from, subj, body)
cae87802
SK
262 }
263 }
264 } else {
265 # TODO: Reconsider the competing global-state organizing-conventions
266 state__alerts__energy__notified_bellow_half = 0
267 }
268}
269
f03e88c6
SK
270function alert_check_energy_line_power( \
271 from, dbg, line_power_curr, line_power_prev, subj, body \
272) {
273 from = "alert_check_energy_line_power"
274
275 dbg["energy_line_power_prev"] = db["energy_line_power_prev"]
276 dbg["energy_line_power_curr"] = db["energy_line_power_curr"]
277 debug(from, "", dbg)
278
279 line_power_curr = db["energy_line_power_curr"]
280 line_power_prev = db["energy_line_power_prev"]
281
282 if (line_power_curr == "no" && line_power_prev != "no") {
283 alert_trigger_low(from, "PowerUnplugged", "")
284 }
285}
286
d4c26c51
SK
287function alert_trigger_low(from, subject, body) {
288 alert_trigger("low", from, subject, body)
cae87802
SK
289}
290
d4c26c51
SK
291function alert_trigger_med(from, subject, body) {
292 alert_trigger("med", from, subject, body)
cae87802
SK
293}
294
d4c26c51
SK
295function alert_trigger_hi(from, subject, body) {
296 alert_trigger("hi", from, subject, body)
cae87802
SK
297}
298
d4c26c51 299function alert_trigger(priority, from, subject, body, msg) {
cae87802
SK
300 # priority : "low" | "med" | "hi"
301 # subject : no spaces
302 # body : anything
d4c26c51 303 msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body)
cae87802
SK
304 output_msg_alert(msg)
305}
306
307function output_msg_alert(msg) {
308 # TODO: Should alerts go into a dedicated channel?
309 output_msg("ALERT", msg, "/dev/stdout")
310}
311
312function output_msg_status_bar(msg) {
313 output_msg("STATUS_BAR", msg, "/dev/stdout")
314}
315
316function output_msg(type, content, channel) {
317 print(type, content) > channel
f37162a4
SK
318}
319
320function set_load_avg( sched) {
321 split($4, sched, "/")
322 db["load_avg_1min"] = $1
323 db["load_avg_5min"] = $2
324 db["load_avg_15min"] = $3
325 db["kern_sched_queue_runnable"] = sched[1]
326 db["kern_sched_queue_total"] = sched[2]
327 db["kern_sched_latest_pid"] = $5
328}
329
330function set_disk_io( curr_w, curr_r, prev_w, prev_r) {
331 curr_w = $1
332 curr_r = $2
333 prev_w = db["disk_io_curr_w"]
334 prev_r = db["disk_io_curr_r"]
335 db["disk_io_curr_w"] = curr_w
336 db["disk_io_curr_r"] = curr_r
337 db["disk_io_diff_w"] = curr_w - prev_w
338 db["disk_io_diff_r"] = curr_r - prev_r
339}
340
341function set_net_addr_io( \
342 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
343) {
344 interface = $1
345 address = $2
346 io_curr_w = $3
347 io_curr_r = $4
348 if (interface) {
349 if (address && io_curr_w && io_curr_r) {
350 # recalculate
351 io_prev_w = net_io_curr_w[interface]
352 io_prev_r = net_io_curr_r[interface]
353
354 net_addr[interface] = address
355 net_io_curr_w[interface] = io_curr_w
356 net_io_curr_r[interface] = io_curr_r
357 net_io_diff_w[interface] = io_curr_w - io_prev_w
358 net_io_diff_r[interface] = io_curr_r - io_prev_r
359 } else {
360 # clear
361 net_addr[interface] = ""
362 net_io_curr_w[interface] = 0
363 net_io_curr_r[interface] = 0
364 net_io_diff_w[interface] = 0
365 net_io_diff_r[interface] = 0
366 }
367 }
368}
369
370function set_screen_brightness( max, cur) {
371 max = $1
372 cur = $2
373 db["screen_brightness"] = (cur / max) * 100
374}
375
ab99b556
SK
376# TODO: Revise overuse of shift() where it is not really needed
377function shift() {
9419890b 378 sub("^" $1 " +", "")
f37162a4
SK
379}
380
cae87802 381function make_status_bar( position, bar, sep, i, j) {
f37162a4
SK
382 position[++i] = make_status_energy()
383 position[++i] = make_status_mem()
384 position[++i] = make_status_cpu()
385 position[++i] = make_status_disk()
386 position[++i] = make_status_net()
387 position[++i] = sprintf("B=%s", db["bluetooth_power"])
388 position[++i] = sprintf("*%d%%", db["screen_brightness"])
389 position[++i] = sprintf("(%s)", db["volume"])
390 position[++i] = make_status_mpd()
391 position[++i] = db["weather_temperature"]
392 position[++i] = db["datetime"]
393 bar = ""
394 sep = ""
395 for (j = 1; j <= i; j++) {
396 bar = bar sep position[j]
397 sep = " "
398 }
399 return bar
400}
401
402function make_status_energy( state, direction_of_change) {
7851644f 403 state = db["energy_state_curr"]
f37162a4
SK
404 if (state == "discharging") {
405 direction_of_change = "<"
406 } else if (state == "charging") {
407 direction_of_change = ">"
408 } else {
409 direction_of_change = "="
410 };
e16fe0ea 411 return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"])
f37162a4
SK
412}
413
414function make_status_mem( total, used, percent, status) {
415 total = db["memory_total"]
416 used = db["memory_used"]
417 # To avoid division by zero when data is missing
418 if (total && used) {
419 percent = round((used / total) * 100)
420 status = sprintf("%d%%", percent)
421 } else {
422 status = "__"
423 }
424 return sprintf("M=%s", status)
425}
426
427function make_status_cpu( load, temp, fan) {
428 load = db["load_avg_1min"]
429 temp = db["temperature"] / 1000
430 fan = db["fan_speed"]
431 return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
432}
433
434function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) {
435 bytes_per_sector = 512
436 bytes_per_mb = 1024 * 1024
437 w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb
438 r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb
439 return \
440 sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r)
441}
442
443function make_status_net( \
444 out,
445 number_of_interfaces_to_show,
446 n,
447 array_of_prefixes_of_interfaces_to_show,
448 prefix,
449 interface,
450 label,
451 count_printed,
452 sep,
453 io_stat,
454 dw, dr,
455 bytes_per_unit\
456) {
457 out = ""
458 number_of_interfaces_to_show = \
459 split(\
460 opt_prefixes_of_net_interfaces_to_show,\
461 array_of_prefixes_of_interfaces_to_show,\
462 ","\
463 )
464 for (n = 1; n <= number_of_interfaces_to_show; n++) {
465 prefix = array_of_prefixes_of_interfaces_to_show[n]
466 for (interface in net_addr) {
467 if (interface ~ ("^" prefix)) {
468 label = substr(interface, 1, 1)
469 if (net_addr[interface]) {
470 bytes_per_mb = 1024 * 1024 # TODO: option
471 dw = net_io_diff_w[interface] / bytes_per_mb
472 dr = net_io_diff_r[interface] / bytes_per_mb
473 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
474 } else {
475 io_stat = "--"
476 }
477 if (interface ~ "^w") {
478 label = label ":" db["net_wifi_status"]
479 }
480 if (++count_printed > 1) {
481 sep = " "
482 } else {
483 sep = ""
484 }
485 out = out sep label ":" io_stat
486 }
487 }
488 }
489 return sprintf("N[%s]", out)
490}
491
492function make_status_mpd( state, status) {
a3bb1cff 493 state = db["mpd_status_state"]
f37162a4
SK
494
495 if (state == "play") {
496 status = make_status_mpd_state_known("▶")
497 } else if (state == "pause") {
498 status = make_status_mpd_state_known("❚❚")
499 } else if (state == "stop") {
500 status = make_status_mpd_state_known("⬛")
501 } else {
502 status = make_status_mpd_state_unknown("--")
503 }
504
505 return sprintf("[%s]", status)
506}
507
f5dfebd2 508function make_status_mpd_state_known(symbol) {
f37162a4
SK
509 return sprintf(\
510 "%s %s %s %s",
511 symbol,
a3bb1cff
SK
512 db["mpd_status_time"],
513 db["mpd_status_percent"],
f5dfebd2 514 substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\
f37162a4
SK
515 )
516}
517
518function make_status_mpd_state_unknown(symbol) {
519 return sprintf("%s", symbol)
520}
521
522function round(n) {
523 return int(n + 0.5)
524}
525
bb1e4fee 526function debug(location, msg, values, sep, vals, key, payload) {
f37162a4 527 if (opt_debug) {
e16fe0ea
SK
528 sep = ""
529 vals = ""
530 for (key in values) {
531 vals = sprintf("%s%s%s: %s", vals, sep, key, values[key])
532 sep = ", "
533 }
ab99b556
SK
534 payload = \
535 sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location, msg, vals)
bb1e4fee 536 output_msg("DEBUG", payload, "/dev/stderr")
f37162a4
SK
537 }
538}
e16fe0ea 539
0684c786
SK
540function error(location, msg) {
541 # TODO: Reconsider classifying internal errors as alerts
542 # Maybe better to keep the error class distinct and provide a
543 # an optional transformation from error to alert
544 alert_trigger_hi(location, "KhatusControllerError", msg)
7275500c
SK
545}
546
e16fe0ea
SK
547function ensure_numeric(n) {
548 return n + 0
549}
550#-------------------------------
551# Why do we need ensure_numeric?
552#-------------------------------
553# awk appears to be guessing the type of an inputted scalar based on usage, so
554# if we read-in a number, but did not use it in any numeric operations, but did
555# use as a string (even in just a format string!) - it will be treated as a
556# string and can lead to REALLY SURPRISING behavior in conditional statements,
557# where smaller number may compare as greater than the bigger ones, such as.
558#
559# Demo:
560#
561# $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %d, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
562# 75 < 100
563# $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
564# 75 > 100
565
566# However, once used as a number, seems to stay that way even after being
567# used as string:
568#
569# $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
570# 75 < 100
571#
572# $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
573# 75 < 100
574#
575# $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
576# 75 < 100
577# $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}'
578# 75 > 100
This page took 0.068947 seconds and 4 git commands to generate.