-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackgroundTasks.py
More file actions
588 lines (535 loc) · 30.8 KB
/
Copy pathBackgroundTasks.py
File metadata and controls
588 lines (535 loc) · 30.8 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import time
import traceback
import subprocess
from battery import Battery
from chipGPIO.hardwareAbstraction import HardwareAbstraction
from datamodel.db_helper import DatabaseHelper
from settings.settings import SettingsClass
import requests
import yaml
import logging
from multiprocessing import Process, Queue
from queue import Full, Empty
from subscriberadapters.sendstatusadapter import SendStatusAdapter
class BackgroundTasks(object):
WiRocLogger: logging.Logger = logging.getLogger('WiRoc')
def __init__(self):
self.lastWiRocDeviceNameSentToServer: str | None = None
self.lastBatteryIsLow: bool | None = None
self.lastBatteryIsLowReceived: bool | None = None
self.webServerUpQueue = Queue()
self.webServerUpQueueCommands = Queue()
self.messageStatQueueCommands = Queue()
self.doInfrequentDatabaseTasksQueueCommands = Queue()
self.doInfrequentHTTPTasksQueueCommands = Queue()
self.messageStatProcess: Process | None = None
self.updateWebServerUpProcess: Process | None = None
self.doInfrequentHTTPTasksBackgroundProcess: Process | None = None
self.doInfrequentDatabaseTasksBackgroundProcess: Process | None = None
self.rocCallHomeBackgroundProcess: Process | None = None
self.rocCallHomeQueueCommands = Queue()
def SendDataToInfrequentHTTPTaskProcess(self):
batteryIsLow = Battery.GetIsBatteryLow()
try:
if batteryIsLow:
self.doInfrequentHTTPTasksQueueCommands.put("BATTERYISLOW", False)
else:
self.doInfrequentHTTPTasksQueueCommands.put("BATTERYISNOTLOW", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::SendDataToInfrequentHTTPTaskProcess() doInfrequentHTTPTasksQueueCommands full")
batteryIsLowReceived = SettingsClass.GetBatteryIsLowReceived()
try:
if batteryIsLowReceived:
self.doInfrequentHTTPTasksQueueCommands.put("BATTERYISLOWRECEIVED", False)
else:
self.doInfrequentHTTPTasksQueueCommands.put("BATTERYISLOWNOTRECEIVED", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::SendDataToInfrequentHTTPTaskProcess() doInfrequentHTTPTasksQueueCommands full 2")
def GetDataFromSubProcesses(self):
try:
while not self.webServerUpQueue.empty():
webServerUp = self.webServerUpQueue.get(False)
BackgroundTasks.WiRocLogger.debug(
f"BackgroundTasks::GetDataFromSubProcesses() webServerUp {webServerUp}")
SettingsClass.SetWebServerUp(webServerUp)
try:
if webServerUp:
self.messageStatQueueCommands.put("WEBSERVERUP", False)
self.doInfrequentHTTPTasksQueueCommands.put("WEBSERVERUP", False)
else:
self.messageStatQueueCommands.put("WEBSERVERDOWN", False)
self.doInfrequentHTTPTasksQueueCommands.put("WEBSERVERDOWN", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::GetDataFromSubProcesses() : messageStatQueueCommands FULL")
pass
except Exception as ex:
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::GetDataFromSubProcesses() exception: {ex}")
def StartInfrequentHTTPTasks(self):
try:
if self.doInfrequentHTTPTasksBackgroundProcess is None:
batteryIsLow = Battery.GetIsBatteryLow()
batteryIsLowReceived = SettingsClass.GetBatteryIsLowReceived()
self.doInfrequentHTTPTasksBackgroundProcess = Process(
target=BackgroundTasks.DoInfrequentHTTPTasksBackground,
args=(self.doInfrequentHTTPTasksQueueCommands,
batteryIsLow,
batteryIsLowReceived,
),
daemon=True)
self.doInfrequentHTTPTasksBackgroundProcess.start()
try:
self.doInfrequentHTTPTasksQueueCommands.put("START", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::StartInfrequentHTTPTasks() doInfrequentHTTPTasksQueueCommands FULL Exception: {fex}")
except Exception as ex:
tb = traceback.format_exc()
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::StartInfrequentHTTPTasks() Exception: {ex} StackTrace: {tb}")
@staticmethod
def DoInfrequentHTTPTasksBackground(doInfrequentHTTPTasksQueueCommands: Queue,
batteryIsLow: bool,
batteryIsLowReceived: bool,
):
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::DoInfrequentHTTPTasksBackground()")
lastWiRocDeviceNameSentToServer: str | None = None
lastBatteryIsLow: bool | None = None
lastBatteryIsLowReceived: bool | None = None
webServerUp = True
while True:
try:
SettingsClass.InvalidateCachesIfSettingsUpdated()
cmd = "START"
if not doInfrequentHTTPTasksQueueCommands.empty():
try:
cmd = doInfrequentHTTPTasksQueueCommands.get(False)
except Empty:
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::DoInfrequentHTTPTasksBackground() doInfrequentDatabaseTasksQueueCommands is empty")
time.sleep(1)
continue
if cmd == "START":
if webServerUp:
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::doInfrequentHTTPTasksBackground()")
btAddress = SettingsClass.GetBTAddress()
webServerUrl = SettingsClass.GetWebServerUrl()
apiKey = SettingsClass.GetAPIKey()
wiRocDeviceName = SettingsClass.GetWiRocDeviceName() if SettingsClass.GetWiRocDeviceName() is not None else "WiRoc Device"
updateWiRocDevice = (btAddress != "NoBTAddress" and (lastWiRocDeviceNameSentToServer != wiRocDeviceName))
if btAddress != "NoBTAddress":
if lastBatteryIsLowReceived is None:
lastBatteryIsLowReceived = not batteryIsLowReceived # so it saves the new value
lastBatteryIsLowReceived = BackgroundTasks.UpdateBatteryIsLowReceivedBackground(webServerUrl,
apiKey,
batteryIsLowReceived,
btAddress,
lastBatteryIsLowReceived)
if lastBatteryIsLow is None:
lastBatteryIsLow = not batteryIsLow # so it saves the new value
lastBatteryIsLow = BackgroundTasks.UpdateBatteryIsLowBackground(batteryIsLow, webServerUrl, apiKey,
btAddress, lastBatteryIsLow)
BackgroundTasks.SendSetConnectedToInternetBackground(webServerUrl, apiKey, btAddress)
if updateWiRocDevice:
lastWiRocDeviceNameSentToServer = wiRocDeviceName
BackgroundTasks.AddDeviceBackground(wiRocDeviceName, btAddress, apiKey, webServerUrl)
time.sleep(40)
elif cmd == "EXIT":
return
elif cmd == "WEBSERVERUP":
webServerUp = True
elif cmd == "WEBSERVERDOWN":
webServerUp = False
elif cmd == "BATTERYISLOW":
batteryIsLow = True
elif cmd == "BATTERYISNOTLOW":
batteryIsLow = False
elif cmd == "BATTERYISLOWRECEIVED":
batteryIsLowReceived = True
elif cmd == "BATTERYISLOWNOTRECEIVED":
batteryIsLowReceived = False
except Exception as ex:
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::DoInfrequentHTTPTasksBackground() exception: {ex}")
time.sleep(40)
@staticmethod
def UpdateBatteryIsLowBackground(batteryIsLow, webServerURL, apiKey, btAddress, lastBatteryIsLow) -> bool:
try:
headers = {'X-Authorization': apiKey}
if batteryIsLow and (lastBatteryIsLow is None or not lastBatteryIsLow):
URL = webServerURL + "/api/v1/Devices/" + btAddress + "/SetBatteryIsLow"
resp = requests.get(url=URL, timeout=1, headers=headers, verify=False)
if resp.status_code == 200:
retDevice = resp.json()
batteryIsLow = retDevice['batteryIsLow'] == '1'
elif not batteryIsLow and (lastBatteryIsLow is None or lastBatteryIsLow):
URL = webServerURL + "/api/v1/Devices/" + btAddress + "/SetBatteryIsNormal"
resp = requests.get(url=URL, timeout=1, headers=headers, verify=False)
if resp.status_code == 200:
retDevice = resp.json()
batteryIsLow = retDevice['batteryIsLow'] == '1'
return batteryIsLow
except Exception as ex:
BackgroundTasks.WiRocLogger.error("BackgroundTasks::UpdateBatteryIsLowBackground() Exception: " + str(ex))
return lastBatteryIsLow
@staticmethod
def UpdateBatteryIsLowReceivedBackground(webServerUrl, apiKey, batteryIsLowReceived, btAddress, lastBatteryIsLowReceived) -> bool:
try:
headers = {'X-Authorization': apiKey}
if batteryIsLowReceived and (lastBatteryIsLowReceived is None or not lastBatteryIsLowReceived):
URL = webServerUrl + "/api/v1/Devices/" + btAddress + "/SetBatteryIsLowReceived"
resp = requests.get(url=URL, timeout=1, headers=headers, verify=False)
if resp.status_code == 200:
retDevice = resp.json()
batteryIsLowReceived = retDevice['batteryIsLowReceived'] == '1'
elif not batteryIsLowReceived and (lastBatteryIsLowReceived is None or lastBatteryIsLowReceived):
URL = webServerUrl + "/api/v1/Devices/" + btAddress + "/SetBatteryIsNormalReceived"
resp = requests.get(url=URL, timeout=1, headers=headers, verify=False)
if resp.status_code == 200:
retDevice = resp.json()
batteryIsLowReceived = retDevice['batteryIsLowReceived'] == '1'
return batteryIsLowReceived
except Exception as ex:
BackgroundTasks.WiRocLogger.error(
"BackgroundTasks::updateBatteryIsLowReceivedBackground() Exception: " + str(ex))
return lastBatteryIsLowReceived
@staticmethod
def SendSetConnectedToInternetBackground(webServerUrl, apiKey, btAddress):
headers = {'X-Authorization': apiKey}
URL = webServerUrl + "/api/v1/Devices/" + btAddress + "/SetConnectedToInternetTime"
try:
resp = requests.post(url=URL, timeout=2, headers=headers, verify=False)
if resp.status_code != 200 and resp.status_code != 303:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::SendSetConnectedToInternetBackground() resp.status_code {resp.status_code}")
except Exception as ex:
BackgroundTasks.WiRocLogger.error("BackgroundTasks::SendSetConnectedToInternetBackground() Exception: " + str(ex))
@staticmethod
def AddDeviceBackground(wiRocDeviceName, btAddress, apiKey, webServerUrl):
try:
with open("../settings.yaml", "r") as f:
settings = yaml.load(f, Loader=yaml.BaseLoader)
wirocPythonVersion = settings['WiRocPythonVersion']
wirocBLEAPIVersion = settings['WiRocBLEAPIVersion']
hardwareVersion = settings["WiRocHWVersion"]
headers = {'X-Authorization': apiKey}
device = {"BTAddress": btAddress, "headBTAddress": btAddress, "name": wiRocDeviceName,
"wirocPythonVersion": wirocPythonVersion, "wirocBLEAPIVersion": wirocBLEAPIVersion,
"hardwareVersion": hardwareVersion}
URL = webServerUrl + "/api/v1/Devices"
resp = requests.post(url=URL, json=device, timeout=1, headers=headers, verify=False)
BackgroundTasks.WiRocLogger.warning(
"BackgroundTasks::AddDeviceBackground resp statuscode btaddress " + btAddress + " " + str(
resp.status_code) + " " + resp.text)
if resp.status_code == 200:
BackgroundTasks.WiRocLogger.info(
f"BackgroundTasks::AddDeviceBackground resp statuscode: {resp.status_code} btaddress: {btAddress} {resp.text}")
retDevice = resp.json()
BackgroundTasks.WiRocLogger.info(
f"BackgroundTasks::AddDeviceBackground returned json: {retDevice}")
else:
BackgroundTasks.WiRocLogger.warning(
f"BackgroundTasks::AddDeviceBackground resp statuscode: {resp.status_code} btaddress: {btAddress} {resp.text}")
except Exception as ex:
BackgroundTasks.WiRocLogger.warning(
"BackgroundTasks::AddDeviceBackground error creating device on webserver")
BackgroundTasks.WiRocLogger.warning("BackgroundTasks::AddDeviceBackground " + str(ex))
# ################## Database tasks ##############
def StartInfrequentDatabaseTasks(self):
try:
if self.doInfrequentDatabaseTasksBackgroundProcess is None:
self.doInfrequentDatabaseTasksBackgroundProcess = Process(
target=BackgroundTasks.DoInfrequentDatabaseTasksBackground,
args=(self.doInfrequentDatabaseTasksQueueCommands,),
daemon=True)
self.doInfrequentDatabaseTasksBackgroundProcess.start()
try:
self.doInfrequentDatabaseTasksQueueCommands.put("START", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::StartInfrequentDatabaseTasks() doInfrequentDatabaseTasksQueueCommands FULL Exception: {fex}")
except Exception as ex:
tb = traceback.format_exc()
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::StartInfrequentDatabaseTasks() Exception: {ex} StackTrace: {tb}")
@staticmethod
def DoInfrequentDatabaseTasksBackground(doInfrequentDatabaseTasksQueueCommands: Queue):
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::DoInfrequentDatabaseTasksBackground()")
while True:
try:
cmd = "START"
if not doInfrequentDatabaseTasksQueueCommands.empty():
try:
cmd = doInfrequentDatabaseTasksQueueCommands.get(False)
except Empty:
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::DoInfrequentDatabaseTasksBackground() doInfrequentDatabaseTasksQueueCommands is empty")
time.sleep(1)
continue
if cmd == "START":
BackgroundTasks.ArchiveFailedMessagesBackground()
BackgroundTasks.ArchiveOldRepeaterMessagesBackground()
elif cmd == "EXIT":
return
time.sleep(20)
except Exception as ex:
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::DoInfrequentDatabaseTasksBackground() exception: {ex}")
time.sleep(40)
@staticmethod
def ArchiveOldRepeaterMessagesBackground():
DatabaseHelper.archive_old_repeater_message()
@staticmethod
def ArchiveFailedMessagesBackground():
msgSubscriptions = DatabaseHelper.get_message_subscriptions_view_to_archive(100)
for msgSub in msgSubscriptions:
BackgroundTasks.WiRocLogger.info(
"BackgroundTasks::archiveFailedMessages() subscription reached max tries: " + msgSub.SubscriberInstanceName + " Transform: " + msgSub.TransformName + " msgSubId: " + str(
msgSub.id))
DatabaseHelper.archive_message_subscription_view_not_sent(msgSub.id)
# ############### ROC CallHome / MiniCallHome #############
ROC_VERSION = "ver7.3"
def StartRocCallHome(self):
try:
if self.rocCallHomeBackgroundProcess is None:
self.rocCallHomeBackgroundProcess = Process(
target=BackgroundTasks.DoRocCallHomeBackground,
args=(self.rocCallHomeQueueCommands,),
daemon=True)
self.rocCallHomeBackgroundProcess.start()
try:
self.rocCallHomeQueueCommands.put("START", False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::StartRocCallHome() rocCallHomeQueueCommands FULL Exception: {fex}")
except Exception as ex:
tb = traceback.format_exc()
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::StartRocCallHome() Exception: {ex} StackTrace: {tb}")
@staticmethod
def _get_local_ip() -> str:
"""Get the local IP address. Priority: ethernet → USB ethernet → WiFi → mesh → mobile USB.
Creates a HardwareAbstraction instance if needed (the subprocess won't have one)."""
try:
if HardwareAbstraction.Instance is None:
HardwareAbstraction.Instance = HardwareAbstraction()
candidates = []
eth = HardwareAbstraction.Instance.GetBuiltinEthernetInterfaceName()
if eth and HardwareAbstraction.Instance.DoesInterfaceExist(eth):
candidates.append(eth)
usbEths = HardwareAbstraction.Instance.GetUSBEthernetInterfaces()
for iface in usbEths:
candidates.append(iface)
wifi = HardwareAbstraction.Instance.GetBuiltinWifiInterfaceName()
if wifi and HardwareAbstraction.Instance.DoesInterfaceExist(wifi):
candidates.append(wifi)
mesh = HardwareAbstraction.Instance.GetMeshInterfaceName()
if mesh and HardwareAbstraction.Instance.DoesInterfaceExist(mesh):
candidates.append(mesh)
# Look for mobile USB stick interfaces (wwan, usb)
try:
result = subprocess.run(["ls", "/sys/class/net/"], capture_output=True, text=True, timeout=1)
all_ifaces = result.stdout.strip().split('\n')
mobile_patterns = ['wwan', 'wwp', 'usb']
for iface in all_ifaces:
if any(p in iface.lower() for p in mobile_patterns) and iface not in candidates:
if HardwareAbstraction.Instance.DoesInterfaceExist(iface):
candidates.append(iface)
except Exception:
pass
for iface in candidates:
ips = HardwareAbstraction.Instance.GetAllIPAddressesOnInterface(iface)
if ips and len(ips) > 0:
return ips[0]
return '0.0.0.0'
except Exception:
return '0.0.0.0'
@staticmethod
def DoRocCallHomeBackground(rocCallHomeQueueCommands: Queue):
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::DoRocCallHomeBackground() begin")
import requests
failedCallHomes = 0
callHomeSent = False
while True:
try:
cmd = "START"
while not rocCallHomeQueueCommands.empty():
try:
cmd = rocCallHomeQueueCommands.get(False)
except Empty:
time.sleep(1)
continue
if cmd == "EXIT":
return
elif cmd != "START":
time.sleep(5)
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::DoRocCallHomeBackground() not start")
continue
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::DoRocCallHomeBackground() START!")
SettingsClass.InvalidateCachesIfSettingsUpdated()
rocEnabled = SettingsClass.GetRocEnabled()
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::DoRocCallHomeBackground() {rocEnabled}!")
if not rocEnabled:
callHomeSent = False
failedCallHomes = 0
time.sleep(SettingsClass.GetRocMiniCallHomeInterval())
continue
rocServerUrl = SettingsClass.GetRocServerUrl()
unitId = SettingsClass.GetBTAddress().replace(':', '').lower()
rocVersion = BackgroundTasks.ROC_VERSION
localIp = BackgroundTasks._get_local_ip()
stationCode = f"{SettingsClass.GetSIStationNumber()}-?"
deviceName = "WiRoc: " + (SettingsClass.GetWiRocDeviceName() or "WiRoc Device")
if not callHomeSent:
# Send CallHome
URL = (f"{rocServerUrl}/{rocVersion}/receivedata.php"
f"?function=callhome&command=set"
f"&computername={deviceName}"
f"&macaddr={unitId}"
f"&signalstrength=0"
f"&rocversion={rocVersion}"
f"&rocrevision=1"
f"&timetoonline=0"
f"&localipaddress={localIp}"
f"&rasphardware=1")
BackgroundTasks.WiRocLogger.debug(
f"BackgroundTasks::DoRocCallHomeBackground() CallHome URL: {URL}")
try:
resp = requests.get(url=URL, timeout=10, verify=False)
if resp.status_code == 200:
BackgroundTasks.WiRocLogger.info(
f"BackgroundTasks::DoRocCallHomeBackground() CallHome OK, unitId: {unitId}")
callHomeSent = True
else:
BackgroundTasks.WiRocLogger.warning(
f"BackgroundTasks::DoRocCallHomeBackground() CallHome failed: {resp.status_code}, response: {resp.text[:500]}")
except Exception as ex:
BackgroundTasks.WiRocLogger.warning(
f"BackgroundTasks::DoRocCallHomeBackground() CallHome exception: {ex}")
else:
# Send MiniCallHome
# https://roc.olresultat.se/ver7.3/mch.php?unitid=<macaddr>&codes=32-Co,99-Co,3-St&totaldatatx=12,4MB&totaldatarx=5,1MB&failedcallhomes=2&localipaddress=192.168.1.50&signaldbm=-73&networktype=19&temperature=52.3&volts=1.20&minfreq=600&maxfreq=1200&freq=900&vpnip=10.255.255.2
URL = (f"{rocServerUrl}/{rocVersion}/mch.php"
f"?unitid={unitId}"
f"&codes={stationCode}"
f"&totaldatatx=0"
f"&totaldatarx=0"
f"&failedcallhomes={failedCallHomes}"
f"&localipaddress={localIp}")
BackgroundTasks.WiRocLogger.debug(
f"BackgroundTasks::DoRocCallHomeBackground() MiniCallHome URL: {URL}")
try:
resp = requests.get(url=URL, timeout=10, verify=False)
if resp.status_code == 200:
failedCallHomes = 0
BackgroundTasks.WiRocLogger.debug(
f"BackgroundTasks::DoRocCallHomeBackground() MiniCallHome OK")
else:
failedCallHomes += 1
BackgroundTasks.WiRocLogger.warning(
f"BackgroundTasks::DoRocCallHomeBackground() MiniCallHome failed: {resp.status_code}, response: {resp.text[:500]}")
except Exception as ex:
failedCallHomes += 1
BackgroundTasks.WiRocLogger.warning(
f"BackgroundTasks::DoRocCallHomeBackground() MiniCallHome exception: {ex}")
time.sleep(SettingsClass.GetRocMiniCallHomeInterval())
except Exception as ex:
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::DoRocCallHomeBackground() exception: {ex}")
time.sleep(5)
# ############### message stats #############
def StartMessageStats(self):
try:
if self.messageStatProcess is None:
self.messageStatProcess = Process(target=BackgroundTasks.SendMessageStatsBackground, args=(self.messageStatQueueCommands, SettingsClass.GetWebServerUp()), daemon=True)
self.messageStatProcess.start()
try:
self.messageStatQueueCommands.put("START",False)
except Full as fex:
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::sendMessageStats() messageStatQueueCommands FULL Exception: {fex}")
except Exception as ex:
tb = traceback.format_exc()
BackgroundTasks.WiRocLogger.error(f"BackgroundTasks::sendMessageStats() Exception: {ex} StackTrace: {tb}")
@staticmethod
def SendMessageStatsBackground(messageStatQueueCommands: Queue, webServerUp: bool):
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::SendMessageStatsBackground() begin")
while True:
try:
SettingsClass.InvalidateCachesIfSettingsUpdated()
cmd = "START"
while not messageStatQueueCommands.empty():
try:
cmd = messageStatQueueCommands.get(False)
except Empty:
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::SendMessageStatsBackground() messageStatQueueCommands is empty")
time.sleep(1)
continue
if cmd == "START":
if webServerUp:
while True:
messageStat = DatabaseHelper.get_message_stat_to_upload()
if messageStat is None:
break
else:
btAddress = SettingsClass.GetBTAddress()
webServerUrl = SettingsClass.GetWebServerUrl()
apiKey = SettingsClass.GetAPIKey()
headers = {'X-Authorization': apiKey}
URL = webServerUrl + "/api/v1/MessageStats"
messageStatToSend = {"adapterInstance": messageStat.AdapterInstanceName,
"BTAddress": btAddress,
"messageType": messageStat.MessageSubTypeName,
"status": messageStat.Status,
"noOfMessages": messageStat.NoOfMessages}
try:
resp = requests.post(url=URL, json=messageStatToSend, timeout=3,
headers=headers,
verify=False)
if resp.status_code == 200 or resp.status_code == 303:
DatabaseHelper.set_message_stat_uploaded(messageStat.id)
except Exception as ex:
tb = traceback.format_exc()
BackgroundTasks.WiRocLogger.error(
f"BackgroundTasks::SendMessageStatsBackground() Exception: {ex} StackTrace: {tb}")
time.sleep(10)
elif cmd == "WEBSERVERUP":
webServerUp = True
elif cmd == "WEBSERVERDOWN":
webServerUp = False
elif cmd == "EXIT":
return
time.sleep(5)
except Exception as ex:
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::SendMessageStatsBackground() exception: {ex}")
time.sleep(1)
# ############ WEB SERVER UP ##############
def StartUpdateWebServerUp(self):
if SettingsClass.GetSendStatusMessages():
if self.updateWebServerUpProcess is None:
webServerUrl = SettingsClass.GetWebServerUrl()
self.updateWebServerUpProcess = Process(target=self.UpdateWebServerUpBackground, args=(self.webServerUpQueueCommands, self.webServerUpQueue, webServerUrl), daemon=True)
self.updateWebServerUpProcess.start()
self.webServerUpQueueCommands.put("CHECK", False)
else:
SettingsClass.SetWebServerUp(False)
@staticmethod
def TestConnection(webServerUrl):
try:
URL = webServerUrl + "/api/v1/ping"
BackgroundTasks.WiRocLogger.debug("BackgroundTasks::TestConnection() " + URL)
r = requests.get(url=URL, timeout=2, headers={}, verify=False)
data = r.json()
return data['code'] == 0
except Exception as ex:
SendStatusAdapter.WiRocLogger.error("BackgroundTasks::TestConnection() " + webServerUrl + " Exception: " + str(ex))
return False
@staticmethod
def UpdateWebServerUpBackground(webServerUpQueueCommands, webServerUpQueue, webServerUrl):
while True:
try:
cmd = "CHECK"
if not webServerUpQueueCommands.empty():
cmd = webServerUpQueueCommands.get(False)
if cmd == "CHECK":
webServerUp: bool = BackgroundTasks.TestConnection(webServerUrl)
webServerUpQueue.put(webServerUp)
elif cmd == "EXIT":
return
time.sleep(15)
except Exception as ex:
BackgroundTasks.WiRocLogger.debug(f"BackgroundTasks::UpdateWebServerUpBackground() exception: {ex}")
webServerUpQueue.put(False)
time.sleep(1)