-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrol.py
executable file
·386 lines (309 loc) · 13.4 KB
/
rol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Bobmorton-TX and contributors
# Incorparates demo code by Richard Hull and contributors https://github.com/rm-hull/luma.examples
# Incorparates code from Sebastian @ https://indibit.de/raspberry-pi-cpu-auslastung-als-diagramm-auf-oled-display/
# See LICENSE.rst for details.
import asyncio
import time
from datetime import datetime
import psutil
from psutil._common import bytes2human
from luma.core.render import canvas # Oled rendering
from luma.core.virtual import viewport, snapshot
from PIL import ImageFont
from demo_opts import get_device # easy device selection cmd line
import csv # csv export functions
import config # import config
# Inputs
from gpiozero import Button, RotaryEncoder, MotionSensor
button = Button(14, hold_time=1) # Button
rotor = RotaryEncoder(15, 18, wrap=True, max_steps=30) # Encoder
pir = MotionSensor(22) # Motion Sensor
# second screen
# from luma.core.interface.serial import i2c
# from luma.oled.device import ssd1306, ssd1309, ssd1325, ssd1331, sh1106, ws0010
# serial = i2c(port=3, address=0x3C)
# deviceB = ssd1306(serial, width=128, height=32)
## variables
cpuList = [1] # cpu data list
netList = [1] # network total data list
scaledata = [1] # scale for charts
history_length = 3600 # data collection time
control_output = [0] # control unit outputs
chartlength = 108 # chart display length
offtimer = [0] # display off timer
timeout = 120 # time out in seconds
csvfile = config.csvpath2 # csv file location
writeout_timer = [0] # write to csv when = csvscale
csvscale = [600] # write to csv in seconds
datacollecttimer = [1] # datacollect in seconds
# Fonts
font1 = ImageFont.truetype('FreeSans.ttf', 9)
font2 = ImageFont.truetype('FreeSans.ttf', 12)
# scroll functions
def scroll_right(virtual, pos):
x, y = pos
if virtual.width > device.width:
while x < virtual.width - device.width:
virtual.set_position((x, y))
x += 1
x -= 1
print(x)
return (x, y)
def scroll_left(virtual, pos):
x, y = pos
while x >= 0:
virtual.set_position((x, y))
x -= 1
x = 0
return (x, y)
def move_right(virtual, pos):
x, y = pos
x = virtual.width - device.width
virtual.set_position((x, y))
x -= 1
return (x, y)
def move_left(virtual, pos):
x, y = pos
x = 0
virtual.set_position((x, y))
return (x, y)
# Python program to get average of a list
def avg(data,LEN):
for i in range((len(data) + LEN - 1) // LEN):
sublist = data[i*LEN:(i+1)*LEN]
yield sum(sublist) / len(sublist)
def timer():
offtimer[0] = 0
# print(f'motion detected')
def csvwrite(row):
# open the file in the write mode
# f = open('/home/ubuntu/scripts/OLED/HMON/csvtest.csv', 'a', newline='')
f = open(csvfile, 'a', newline='') # todo via config.file
# create the csv writer
writer = csv.writer(f)
# write a row to the csv file
writer.writerow(row)
# close the file
f.close()
# Button inputs
async def control(delay, signal: asyncio.Event):
while True:
# Encoder input
steps = rotor.steps
stepchk = scaledata[0]
if steps == 0: # avoid div by 0
steps =1
scaledata[0] = abs(steps)
control_output[0] = 0
if button.is_pressed:
control_output[0] = 1
# print(f'knopf pressed')
signal.set()
if button.is_held:
control_output[0] = 2
# print(f'knopf long pressed')
signal.set()
await asyncio.sleep(0.5) # avoid double long press input
else:
await asyncio.sleep(delay)
if stepchk != scaledata[0]:
signal.set()
# Collect system info and store in variable
async def data_collect(delay, values, signal: asyncio.Event):
global uptime, dfpercent, dffree, dftotal, elapsed, nets, netr, mem, mem_free, mem_used, memo, df, cpu_val, tempo
elapsed, nets, netr, mem, mem_free, mem_used, memo, cpu_val, tempo = 1, 1, 1, 1, 1, 1, 1, 1, 1
df = 1
dffree = 1
dftotal = 1
dfpercent = 1
uptime = 1
while True:
# get network data for interval
tot_before = psutil.net_io_counters()
await asyncio.sleep(1)
# time.sleep(delay)
tot_after = psutil.net_io_counters()
nets = (tot_after.bytes_sent - tot_before.bytes_sent)
netr = (tot_after.bytes_recv - tot_before.bytes_recv)
# Uptime
boot_time = datetime.fromtimestamp(psutil.boot_time())
elapsed = datetime.now() - boot_time
uptimestr = time.strftime('%H:%M:%S', time.gmtime(elapsed.total_seconds()))
#disk
df = psutil.disk_usage("/")
dfpercent = str("{0:0.1f}%".format(df.percent))
dffree = bytes2human(df.free,)
dftotal = bytes2human(df.total)
# CPU+MEM Auslastung auslesen
cpu_val = psutil.cpu_percent(interval=0)
mem = psutil.virtual_memory()
mem_used = (mem.total - mem.available) * 100.0 / mem.total
mem_free = mem.available
memo = mem.total
# Temperatur
temp = psutil.sensors_temperatures()["cpu_thermal"][0]
tempo = round(temp.current, 1)
# fill lists
cpuList.append(cpu_val)
if len(cpuList) > history_length:
cpuList.pop(0)
netList.append(nets + netr)
if len(netList) > history_length:
netList.pop(0)
# save to csv
writeout_timer[0] = writeout_timer[0] + 1
if writeout_timer[0] == csvscale[0]:
# csvwrite(list((datetime.now(), list(avg(cpuList[-5:],5),))))
csvwrite(list((datetime.now(), round(list(avg(cpuList[-csvscale[0]:],csvscale[0]))[0],1), tempo, list(avg(netList[-csvscale[0]:],csvscale[0]))[0], mem_free, dffree)))
writeout_timer[0] = 0
# print(f' csv timer {writeout_timer[0]}')
signal.set() # signal that loop is completed
async def ausgabe(delay, btn_input, pos, signal: asyncio.Event):
await signal.wait() # waiting for either data update or button press
with canvas(virtual) as draw:
# debug stuff
# print(f'ausgabe button {control_output[0]}')
# ## change scale on button press
# if btn_input[0] == 1:
# if scaledata[0] == 1:
# scaledata[0] = 5
# elif scaledata[0] == 5:
# scaledata[0] = 1
## offtimer
pir.when_motion = timer
if offtimer[0] >= timeout:
device.hide()
# datacollecttimer[0] = 60 # only collect every minute
# csvscale[0] = 10 # write to csv every 10 min
# deviceB.hide()
else:
device.show()
# datacollecttimer[0] = 1 # collect data every second
# csvscale[0] = 600 # write to csv every 10 min
# deviceB.show()
offtimer[0] = offtimer[0] +1
# print(f'offtimer is {offtimer[0]}')
### CPU Diagram # get last 110 steps of data list
cpuListavg = list(avg(cpuList,scaledata[0]))
if len(cpuListavg) < chartlength:
liststart = 0
else:
liststart = len(cpuListavg) - len(cpuListavg[-chartlength:])
# draw axis
draw.line((25, 43, 25, 63), fill=255) # Y
draw.line((22, 43, 25, 43), fill=255) # mark top of axis
# cpuscale = float(max(cpuList[liststart:len(cpuListavg)]))
cpuscale = max((cpuListavg[liststart:]))
# label axis
# draw.text((0, 43), f'{round(max(cpuList[liststart:len(cpuListavg)]))} %', font=font1, fill=255)
draw.text((0, 43), f'{round(cpuscale)} %', font=font1, fill=255)
draw.text((0, 52), "CPU", font=font1, fill=255)
# CPU chart
X = 26 # start of chartdraw
for n in range(liststart, len(cpuListavg)):
Y = float(20 / cpuscale * cpuListavg[n]) # tbd avoid divion by 0
if len(cpuList) == 0:
draw.line((X, 63 - Y, X, 63 - Y), fill=255)
else:
y2 = float(20 / cpuscale * cpuListavg[n-1])
draw.line((X - 1 , 63 - y2, X, 63 - Y), fill=255)
# draw.line((X, 63, X, 63 - Y), fill=255) # Balken diagram
X = X + 1
# NET STATS output
netListavg = list(avg(netList,scaledata[0]))
if len(netListavg) < chartlength:
liststart = 0
else:
liststart = len(netListavg) - len(netListavg[-chartlength:])
X = 26
# nscale = max(netList[liststart:len(netListavg)])
nscale = max((netListavg[liststart:]))
# print(f'netscale {nscale}')
# Net Diagram
# draw lables
draw.text((0, 21), f'{(bytes2human(round(nscale,0)))}', font=font1, fill=255)
draw.text((0, 29), "NET", font=font1, fill=255)
# draw axis
draw.line((22, 21, 25, 21), fill=255) # mark highest point
draw.line((25, 21, 25, 41), fill=255) # Y
# draw chart
for n in range(liststart, len(netListavg)):
if nscale < 1: # avoid division by 0
nscale = 1
Y = float(20 / nscale * netListavg[n])
draw.line((X, 41, X, 41 - Y), fill=255)
X = X + 1
# draw values to display
draw.text((0, 0), "C: " + str(cpu_val) + " %", font=font1, fill=255)
draw.text((42, 0), "M: " + str(bytes2human(mem_free)), font=font1, fill=255)
draw.text((82, 0), "Tx: " + str(bytes2human(nets)), font=font1, fill=255)
draw.text((82, 8), "Rx: " + str(bytes2human(netr)), font=font1, fill=255)
# draw scale information
draw.rectangle((42, 11, 72, 20), fill="white")
draw.text((44, 11), f'{scaledata[0]} sec', font=font1, fill="black")
# draw cpu temp
draw.text((0, 8), str(tempo) + " °C", font=font1, fill=255)
# render uptime and IP
uptimestr = time.strftime('%d', time.gmtime(elapsed.total_seconds())) + " days " + time.strftime('%H:%M:%S', time.gmtime(elapsed.total_seconds()))
# debug time
# print(uptimestr)
# print('%d:%H:%M:%S',elapsed)
draw.text((135, 0), "Up: " + str(uptimestr), font=font1, fill=255)
draw.text((135, 10), " IP:" + str(psutil.net_if_addrs()['eth0'][0].address), font=font1, fill=255)
# disk usage
draw.text((135, 20), text=f'DISK', font=font2, fill="white")
draw.text((135, 35), text=f'Used {dfpercent}', font=font1, fill="white")
draw.text((135, 45), text=f'Free: {dffree}', font=font1, fill="white")
draw.text((135, 55), text=f'Total: {dftotal}', font=font1, fill="white")
#mem test
draw.text((200, 20), text=f'MEM', font=font2, fill="white")
draw.text((200, 35), text="{0:0.1f}%".format(round(mem_used, 1)), font=font1, fill="white")
draw.text((200, 45), text=f'{bytes2human(mem_free)}', font=font1, fill="white")
draw.text((200, 55), text=f'{bytes2human(memo)}', font=font1, fill="white")
## scroll display on button long press
x, y = pos
if btn_input[0] == 2:
if x == 0:
draw.text((0, 10), text=f'Btn {btn_input}', font=font2, fill="white")
pos = move_right(virtual, pos)
# virtual.set_position((127, 0))
# x = 127
control_output[0] = 0
# print(f'butn pressed {btn_input} x {x}')
elif x > 1:
draw.text((0, 10), text=f'Btn L {btn_input}', font=font2, fill="white")
pos = move_left(virtual, pos)
# virtual.set_position((0, 0))
# x = 0
control_output[0] = 0
if btn_input[0] == 1:
offtimer[0] = 0 # wake up on button press
# second screen output
# with canvas(deviceB) as draw:
# draw.text((0, 0), "Up: " + str(uptimestr), font=font2, fill=255)
# draw.text((0, 10), " IP:" + str(psutil.net_if_addrs()['eth0'][0].address), font=font2, fill=255)
signal.clear()
return pos
async def main():
global btn
# btn = 0 # not needed?
pos = (0, 0)
# h = 0 # not needed?
values = 1
signal = asyncio.Event()
task2 = asyncio.create_task(control(0.10, signal))
task1 = asyncio.create_task(data_collect(1, values, signal))
while True:
task3 = asyncio.create_task(ausgabe(0, control_output, pos, signal))
pos = await task3
# h = h +1 # not needed?
# btn = 0 # not needed?
# print(f'h wert {h} button wert {btn}')
if __name__ == '__main__':
try:
device = get_device() # device selector default to ssd1306
virtual = viewport(device, width=device.width * 2, height=device.height)
asyncio.run(main())
except KeyboardInterrupt:
pass