-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
357 lines (300 loc) · 14.6 KB
/
Copy pathapp.py
File metadata and controls
357 lines (300 loc) · 14.6 KB
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
#!/usr/bin/env python3
# A simple Python manager for "Turing Smart Screen" 3.5" IPS USB-C display
# https://github.com/mathoudebine/turing-smart-screen-python
import os
import signal
import sys
import time
import bitmath
from datetime import datetime
# Import only the modules for LCD communication
from library.lcd_comm_rev_a import LcdCommRevA, Orientation
from library.lcd_comm_rev_b import LcdCommRevB
from library.lcd_simulated import LcdSimulated
from library.log import logger
import requests
from requests.packages import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Get Prom Server URL from env variable
PROM_SERVER_URL=os.environ.get('PROM_SERVER_URL')
def get_prom_metric(metricname, label):
query = "last_over_time(" + metricname + "{" + label + "}[1h])"
response = requests.get(f"{PROM_SERVER_URL}/api/v1/query?query={query}")
if response.status_code == 200:
data = response.json().get('data', {}).get('result', [])
if len(data) == 1:
try:
metric = data[0]['value'][1]
return str(round(float(metric)))
except (ValueError, TypeError):
logger.error(f"Error converting metric value to float: {data[0]['value'][1]}")
return 'ERR'
else:
logger.error(f"No data found for metric: {metricname} with label: {label}")
return 'ERR'
else:
logger.error(f"Failed to retrieve metric: {metricname} with label: {label}, status code: {response.status_code}")
return 'ERR'
def get_prom_metric_from_query(query):
response = requests.get(f"{PROM_SERVER_URL}/api/v1/query?query={query}")
if response.status_code == 200:
data = response.json().get('data', {}).get('result', [])
if len(data) == 1:
try:
metric = data[0]['value'][1]
return str(float(metric))
except (ValueError, TypeError):
logger.error(f"Error converting metric value to float: {data[0]['value'][1]}")
return 'ERR'
else:
logger.error(f"No data found for query: {query}")
return 'ERR'
else:
logger.error(f"Failed to retrieve data for query: {query}, status code: {response.status_code}")
return 'ERR'
# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux, etc. or "AUTO" for auto-discovery
# COM_PORT = "/dev/ttyACM0"
# COM_PORT = "COM5"
COM_PORT = "AUTO"
# Display revision: A or B (for "flagship" version, use B) or SIMU for simulated LCD (image written in screencap.png)
# To identify your revision: https://github.com/mathoudebine/turing-smart-screen-python/wiki/Hardware-revisions
REVISION = "A"
stop = False
if __name__ == "__main__":
def sighandler(signum, frame):
global stop
stop = True
# Set the signal handlers, to send a complete frame to the LCD before exit
signal.signal(signal.SIGINT, sighandler)
signal.signal(signal.SIGTERM, sighandler)
is_posix = os.name == 'posix'
if is_posix:
signal.signal(signal.SIGQUIT, sighandler)
# Build your LcdComm object based on the HW revision
lcd_comm = None
if REVISION == "A":
logger.info("Selected Hardware Revision A (Turing Smart Screen)")
lcd_comm = LcdCommRevA(com_port="AUTO",
display_width=320,
display_height=480)
elif REVISION == "B":
print("Selected Hardware Revision B (XuanFang screen version B / flagship)")
lcd_comm = LcdCommRevB(com_port="AUTO",
display_width=320,
display_height=480)
elif REVISION == "SIMU":
print("Selected Simulated LCD")
lcd_comm = LcdSimulated(display_width=320,
display_height=480)
else:
print("ERROR: Unknown revision")
try:
sys.exit(0)
except:
os._exit(0)
# Reset screen in case it was in an unstable state (screen is also cleared)
lcd_comm.Reset()
# Send initialization commands
lcd_comm.InitializeComm()
# Set brightness in % (warning: screen can get very hot at high brightness!)
lcd_comm.SetBrightness(level=15)
# Set backplate RGB LED color (for supported HW only)
#lcd_comm.SetBackplateLedColor(led_color=(255, 255, 255))
# Set orientation (screen starts in Portrait)
lcd_comm.SetOrientation(orientation=Orientation.REVERSE_LANDSCAPE)
font = "roboto-mono/RobotoMono-Medium.ttf"
# Display static text
lcd_comm.DisplayText("Temperature", 10, 10,
font="roboto/Roboto-Bold.ttf",
font_size=30,
font_color=(255, 0, 0),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Office:", 10, 50,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Lounge:", 10, 85,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("L Room:", 10, 120,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Upstairs:", 10, 155,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Bedroom:", 10, 190,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Backyard:", 10, 225,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Garage:", 10, 260,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Power", 240, 10,
font="roboto/Roboto-Bold.ttf",
font_size=30,
font_color=(0, 153, 51),
background_color=(0, 0, 0))
lcd_comm.DisplayText("PC:", 240, 50,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("K PC:", 240, 85,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("TV:", 240, 120,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("W/Fridge:", 240, 155,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText("Grid:", 240, 190,
font,
font_size=25,
font_color=(255, 0, 0),
background_color=(0, 0, 0))
#lcd_comm.DisplayText("Solar:", 240, 225,
# font,
# font_size=25,
# font_color=(255, 255, 255),
# background_color=(0, 0, 0))
# Display the current time and some progress bars as fast as possible
while not stop:
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Office'") + "º", 150, 50,
font=font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Lounge'") + "º", 150, 85,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='LBedroom'") + "º", 150, 120,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Upstairs'") + "º", 150, 155,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Bedroom'") + "º", 150, 190,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Backyard'") + "º", 150, 225,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
lcd_comm.DisplayText(get_prom_metric("thermometer_temperature_celsius","room='Garage'") + "º", 150, 260,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
computer_watts=get_prom_metric("tasmota_energy_power_active_watts","job='Computer'") + "W"
lcd_comm.DisplayText(f'{computer_watts:>5}', 405, 50,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
k_computer_watts=get_prom_metric("tasmota_energy_power_active_watts","job='K Computer'") + "W"
lcd_comm.DisplayText(f'{k_computer_watts:>5}', 405, 85,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
tv_watts=get_prom_metric("tasmota_energy_power_active_watts","job='TV'") + "W"
lcd_comm.DisplayText(f'{tv_watts:>5}', 405, 120,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
wine_fridge_watts = get_prom_metric("tasmota_energy_power_active_watts","job='Wine Fridge'") + "W"
#chia_watts=get_prom_metric("tasmota_energy_power_active_watts","job='Chia'") + "W"
lcd_comm.DisplayText(f'{wine_fridge_watts:>5}', 405, 155,
font,
font_size=25,
font_color=(255, 255, 255),
background_color=(0, 0, 0))
#purchased_energy=round(float(get_prom_metric_from_query("solaredge_api_purchased_energy/1000")),1)
today_date=datetime.today().strftime('%d %b')
value = round(float(get_prom_metric_from_query("last_over_time(solaredge_api_purchased_energy{date='" + today_date + "'}[120m])/1000")),1)
try:
purchased_energy = round(float(value), 1)
except ValueError:
print(purchased_energy)
purchased_energy = 0 # or handle the error appropriately
lcd_comm.DisplayText(f'{purchased_energy:>5}kW', 375, 190,
font,
font_size=25,
font_color=(255, 0, 0),
background_color=(0, 0, 0))
current_solar_power=round(float(get_prom_metric_from_query("AC_Power*(10^AC_Power_SF)/1000")),1)
lcd_comm.DisplayProgressBar(240, 225,
width=140, height=30,
min_value=0, max_value=5, value=current_solar_power,
bar_color=(80,200,120), bar_outline=True,
background_color=(0, 0, 0))
lcd_comm.DisplayText(f'{current_solar_power:>4}kW', 390, 225,
font,
font_size=25,
font_color=(80,200,120),
background_color=(0, 0, 0))
try:
speed_dl_bytes = float(get_prom_metric_from_query("unpoller_site_receive_rate_bytes{subsystem='wan'}"))
except ValueError:
speed_dl_bytes = 0 # Default value or handle the error as needed
logger.error("Failed to convert metric to int, setting speed_dl_bytes to 0")
try:
speed_dl = bitmath.Byte(speed_dl_bytes).Mib.format("{value:.1f} {unit}/s")
except ValueError:
print(f"Debug Speed DL Bytes:{speed_dl_bytes}")
speed_dl = "ERR"
lcd_comm.DisplayText(f'DL: {speed_dl:>12}', 240, 260,
font,
font_size=25,
font_color=(113, 163, 245),
background_color=(0, 0, 0))
try:
speed_ul_bytes=float(get_prom_metric_from_query("unpoller_site_transmit_rate_bytes{subsystem='wan'}"))
except ValueError:
speed_ul_bytes = 0 # Default value or handle the error as needed
logger.error("Failed to convert metric to int, setting speed_ul_bytes to 0")
try:
speed_ul = bitmath.Byte(speed_ul_bytes).Mib.format("{value:.1f} {unit}/s")
except ValueError:
print(f"Debug Speed UL Bytes:{speed_ul_bytes}")
speed_ul = "ERR"
lcd_comm.DisplayText(f'UL: {speed_ul:>12}', 240, 290,
font,
font_size=25,
font_color=(113, 163, 245),
background_color=(0, 0, 0))
time.sleep(5)
# Close serial connection at exit
lcd_comm.closeSerial()