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