Commit | Line | Data |
---|---|---|
75b23ff8 SK |
1 | #! /usr/bin/awk -f |
2 | ||
3 | BEGIN { | |
4 | FS = msg_fs ? msg_fs : "|" | |
5 | OFS = msg_fs ? msg_fs : "|" | |
6 | Kfs = key_fs ? key_fs : ":" | |
7 | } | |
8 | ||
9 | # ----------------------------------------------------------------------------- | |
10 | # Input | |
11 | # ----------------------------------------------------------------------------- | |
12 | $1 == "OK" { | |
13 | Data_update() | |
14 | } | |
15 | ||
16 | $1 == "OK" && \ | |
17 | $2 == "khatus_sensor_datetime" { | |
18 | print_msg_ok("status_bar", make_status_bar()) | |
19 | } | |
20 | ||
21 | # Let everything else through | |
22 | // | |
23 | ||
24 | # ----------------------------------------------------------------------------- | |
25 | # Data | |
26 | # ----------------------------------------------------------------------------- | |
27 | ||
28 | function Data_update( src, key, val, len_line, len_head, len_val, time) { | |
29 | src = $2 | |
30 | key = $3 | |
31 | ||
32 | # Not just using $4 for val - because an unstructured value (like name of a | |
33 | # song) might contain a character identical to FS | |
34 | len_line = length($0) | |
35 | len_head = length($1 FS $2 FS $3 FS) | |
36 | len_val = len_line - len_head | |
37 | val = substr($0, len_head + 1, len_val) | |
38 | ||
39 | Data[src, key] = val | |
40 | time = Data_get_time() | |
41 | M_time[src, key] = time | |
42 | ||
43 | if (time % 3600 == 0) { | |
44 | Data_gc() | |
45 | } | |
46 | } | |
47 | ||
48 | function Data_get(src, key, age_max, time, age, is_expired) { | |
49 | time = Data_get_time() | |
50 | A_time[src, key] = time | |
51 | age = time - M_time[src, key] | |
52 | is_expired = age_max && age > age_max | |
53 | return is_expired ? "" : Data[src, key] | |
54 | } | |
55 | ||
56 | function Data_get_time( src, key, time) { | |
57 | src = "khatus_sensor_datetime" | |
58 | key = "epoch" | |
59 | time = Data[src, key] | |
60 | A_time[src, key] = time | |
61 | return time | |
62 | } | |
63 | ||
64 | function Data_gc( src_and_key, unused_for) { | |
65 | for (src_and_key in Data) { | |
66 | unused_for = Data_get_time() - A_time[src_and_key] | |
67 | if (unused_for > 3600) { | |
68 | print_msg_error(\ | |
69 | "Data_gc", "Deleting unused src_and_key: " src_and_key \ | |
70 | ) | |
71 | delete Data[src_and_key] | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
76 | # ----------------------------------------------------------------------------- | |
77 | # Status bar | |
78 | # ----------------------------------------------------------------------------- | |
79 | ||
80 | function make_status_bar( position, bar, sep, i, j) { | |
81 | position[++i] = "" | |
82 | position[++i] = make_status_energy() | |
83 | position[++i] = make_status_mem() | |
84 | position[++i] = make_status_cpu() | |
85 | position[++i] = make_status_disk() | |
86 | position[++i] = make_status_net() | |
87 | position[++i] = make_status_bluetooth() | |
88 | position[++i] = make_status_screen_brightness() | |
89 | position[++i] = make_status_volume() | |
90 | position[++i] = make_status_mpd() | |
91 | position[++i] = make_status_weather() | |
92 | position[++i] = make_status_datetime() | |
93 | position[++i] = "" | |
94 | bar = "" | |
95 | sep = "" | |
96 | for (j = 1; j <= i; j++) { | |
97 | bar = bar sep position[j] | |
98 | sep = " " | |
99 | } | |
100 | return bar | |
101 | } | |
102 | ||
103 | function make_status_energy( state, charge, direction_of_change) { | |
104 | state = Data_get("khatus_sensor_energy", "battery_state") | |
105 | charge = Data_get("khatus_sensor_energy", "battery_percentage") | |
106 | ||
107 | if (state == "discharging") { | |
108 | direction_of_change = "<" | |
109 | } else if (state == "charging") { | |
110 | direction_of_change = ">" | |
111 | } else { | |
112 | direction_of_change = "=" | |
113 | } | |
114 | ||
115 | return sprintf("E%s%d%%", direction_of_change, charge) | |
116 | } | |
117 | ||
118 | function make_status_mem( total, used, percent, status) { | |
119 | total = Data_get("khatus_sensor_memory", "total", 2) | |
120 | used = Data_get("khatus_sensor_memory", "used" , 2) | |
121 | # To avoid division by zero when data is missing | |
122 | if (total && used) { | |
123 | percent = round((used / total) * 100) | |
124 | status = sprintf("%d%%", percent) | |
125 | } else { | |
126 | status = "__" | |
127 | } | |
128 | return sprintf("M=%s", status) | |
129 | } | |
130 | ||
131 | function make_status_cpu( load, temp, fan) { | |
132 | load = Data_get("khatus_sensor_loadavg" , "load_avg_1min", 2) | |
133 | temp = Data_get("khatus_sensor_temperature", "temp_c" , 2) | |
134 | fan = Data_get("khatus_sensor_fan" , "speed" , 2) | |
135 | ||
136 | load = load ? sprintf("%4.2f", load) : "--" | |
137 | temp = temp ? sprintf("%d" , temp) : "--" | |
138 | fan = fan ? sprintf("%4d" , fan) : "--" | |
139 | ||
140 | return sprintf("C=[%s %s°C %srpm]", load, temp, fan) | |
141 | } | |
142 | ||
143 | function make_status_disk( bytes_per_sector, bytes_per_mb, w, r, u) { | |
144 | bytes_per_sector = 512 | |
145 | bytes_per_mb = 1024 * 1024 | |
146 | ||
147 | w = Data_get("khatus_sensor_disk_io" , "sectors_written" , 2) | |
148 | r = Data_get("khatus_sensor_disk_io" , "sectors_read" , 2) | |
149 | u = Data_get("khatus_sensor_disk_space", "disk_usage_percentage", 10) | |
150 | ||
151 | w = w ? sprintf("%0.3f", (w * bytes_per_sector) / bytes_per_mb) : "--" | |
152 | r = r ? sprintf("%0.3f", (r * bytes_per_sector) / bytes_per_mb) : "--" | |
153 | u = u ? u : "--" | |
154 | ||
155 | return sprintf("D=[%s%% %s▲ %s▼]", u, w, r) | |
156 | } | |
157 | ||
158 | function make_status_net( \ | |
159 | number_of_net_interfaces_to_show, \ | |
160 | net_interfaces_to_show, \ | |
161 | sensor_io, \ | |
162 | sensor_wi, \ | |
163 | out, \ | |
164 | sep, \ | |
165 | i, \ | |
166 | interface, \ | |
167 | label, \ | |
168 | addr, \ | |
169 | w, \ | |
170 | r, \ | |
171 | bytes_per_mb, \ | |
172 | io_stat, \ | |
173 | wifi \ | |
174 | ) { | |
175 | number_of_net_interfaces_to_show = \ | |
176 | split(opt_net_interfaces_to_show, net_interfaces_to_show, ",") | |
177 | ||
178 | sensor_io = "khatus_sensor_net_addr_io" | |
179 | sensor_wi = "khatus_sensor_net_wifi_status" | |
180 | ||
181 | out = "" | |
182 | sep = "" | |
183 | for (i = number_of_net_interfaces_to_show; i > 0; i--) { | |
184 | interface = net_interfaces_to_show[i] | |
185 | label = substr(interface, 1, 1) | |
186 | ||
187 | addr = Data_get(sensor_io, "addr" Kfs interface, 2) | |
188 | w = Data_get(sensor_io, "bytes_written" Kfs interface, 2) | |
189 | r = Data_get(sensor_io, "bytes_read" Kfs interface, 2) | |
190 | ||
191 | if (addr) { | |
192 | bytes_per_mb = 1024 * 1024 | |
193 | w = w ? sprintf("%0.3f", w / bytes_per_mb) : "--" | |
194 | r = r ? sprintf("%0.3f", r / bytes_per_mb) : "--" | |
195 | io_stat = sprintf("%s▲ %s▼", w, r) | |
196 | } else { | |
197 | io_stat = "--" | |
198 | } | |
199 | ||
200 | if (interface ~ "^w") { | |
201 | wifi = Data_get(sensor_wi, "status" Kfs interface, 5) | |
202 | label = label ":" (wifi ? wifi : "--") | |
203 | } | |
204 | ||
205 | out = out sep label ":" io_stat | |
206 | sep = " " | |
207 | } | |
208 | ||
209 | return sprintf("N[%s]", out) | |
210 | } | |
211 | ||
212 | function make_status_bluetooth( status) { | |
213 | status = Data_get("khatus_sensor_bluetooth_power", "power_status", 5) | |
214 | return sprintf("B=%s", status ? status : "--") | |
215 | } | |
216 | ||
217 | function make_status_screen_brightness( percentage) { | |
218 | percentage = Data_get("khatus_sensor_screen_brightness", "percentage", 5) | |
219 | percentage = percentage ? sprintf("%d", percentage) : "--" | |
220 | return sprintf("*%s%%", percentage) | |
221 | } | |
222 | ||
223 | function make_status_volume( sink, mute, vol_l, vol_r, status) { | |
224 | sink = opt_pulseaudio_sink | |
225 | mute = Data_get("khatus_sensor_volume", "mute" Kfs sink, 2) | |
226 | vol_l = Data_get("khatus_sensor_volume", "vol_left" Kfs sink, 2) | |
227 | vol_r = Data_get("khatus_sensor_volume", "vol_right" Kfs sink, 2) | |
228 | ||
229 | if (mute && vol_l && vol_r) { | |
230 | if (mute == "yes") {status = "X"} | |
231 | else if (mute == "no") {status = sprintf("%s %s", vol_l, vol_r)} | |
232 | else { | |
233 | print_msg_error(\ | |
234 | "make_status_volume", \ | |
235 | "Unexpected value for 'mute' field: " mute \ | |
236 | ) | |
237 | } | |
238 | } else { | |
239 | status = "--" | |
240 | } | |
241 | ||
242 | return sprintf("(%s)", status) | |
243 | } | |
244 | ||
245 | function make_status_mpd( state, status) { | |
246 | if (state = Data_get("khatus_sensor_mpd", "state", 2)) { | |
247 | if (state == "play") { | |
248 | status = make_status_mpd_state_known("▶") | |
249 | } else if (state == "pause") { | |
250 | status = make_status_mpd_state_known("❚❚") | |
251 | } else if (state == "stop") { | |
252 | status = make_status_mpd_state_known("⬛") | |
253 | } else { | |
254 | print_msg_error(\ | |
255 | "make_status_mpd", \ | |
256 | "Unexpected value for 'state' field: " state \ | |
257 | ) | |
258 | status = "--" | |
259 | } | |
260 | } else { | |
261 | status = "--" | |
262 | } | |
263 | ||
264 | return sprintf("[%s]", status) | |
265 | } | |
266 | ||
267 | function make_status_mpd_state_known(symbol, s, song, time, percentage) { | |
268 | s = "khatus_sensor_mpd" | |
269 | song = Data_get(s, "song" , 2) | |
270 | time = Data_get(s, "play_time_minimal_units", 2) | |
271 | percent = Data_get(s, "play_time_percentage" , 2) | |
272 | song = substr(song, 1, opt_mpd_song_max_chars) | |
273 | return sprintf("%s %s %s %s", symbol, time, percent, song) | |
274 | } | |
275 | ||
276 | function make_status_weather( hour, t_f) { | |
277 | hour = 60 * 60 | |
278 | t_f = Data_get("khatus_sensor_weather", "temperature_f", 3 * hour) | |
279 | t_f = t_f ? sprintf("%d", t_f) : "--" | |
280 | return sprintf("%s°F", t_f) | |
281 | } | |
282 | ||
283 | function make_status_datetime( dt) { | |
284 | dt = Data_get("khatus_sensor_datetime", "datetime", 2) | |
285 | return dt ? dt : "--" | |
286 | } | |
287 | ||
288 | # ----------------------------------------------------------------------------- | |
289 | # Output | |
290 | # ----------------------------------------------------------------------------- | |
291 | ||
292 | function print_msg_ok(key, val) { | |
293 | print_msg("OK", key, val, "/dev/stdout") | |
294 | } | |
295 | ||
296 | function print_msg_error(location, msg) { | |
297 | print_msg("ERROR", location, msg, "/dev/stderr") | |
298 | } | |
299 | ||
300 | function print_msg(status, key, val, channel) { | |
301 | print(status, "khatus_bar", key, val) > channel | |
302 | } | |
303 | ||
304 | # ----------------------------------------------------------------------------- | |
305 | # Numbers | |
306 | # ----------------------------------------------------------------------------- | |
307 | ||
308 | function round(n) { | |
309 | return int(n + 0.5) | |
310 | } |