forked from Frankkkkk/python-uni-t-ut353-bt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
read.py
95 lines (76 loc) · 2.42 KB
/
read.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
## Frank@Villaro-Dixon.eu - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, etc.
import pexpect
import time
import os
import statistics
import requests
def get_minute_measure(child):
end_time = time.time()+10
measures = []
while time.time() < end_time:
child.sendline("char-write-cmd 0x25 5e")
time.sleep(.1)
child.expect("Notification handle.*", timeout=10)
result = child.after.split(b'\r')[0]
value = result.decode('ascii').split('value: ')[1]
condensed = value.replace(' ', '')
bytemsg = bytes.fromhex(condensed)
# For more information, look at the file
# p004cn/com/unitrend/ienv/android/domain/service/BluetoothLeService.java
# From the decompiled cn-com-unitrend-ienv APK application
assert(bytemsg[4] == 0x3b) # Uni-T UT353BT noise meter
assert(bytemsg[14] == 0x3d) # dB(A) units
value = bytemsg[5:]
value = value.split(b'=')[0]
assert(b'dBA' in value)
raw_value = value.split(b'dBA')[0]
dba_noise = float(raw_value.decode('ascii'))
print(dba_noise)
measures.append(dba_noise)
return {
'min': min(measures),
'max': max(measures),
'median': statistics.median(measures),
'mean': statistics.mean(measures),
}
def send_stats(stats):
print(stats)
now = int(time.time())
for key, value in stats.items():
pt = {
"metric": "noise.outside",
"timestamp": now,
"value": value,
"tags": {
"type": key,
}
}
OPEN_TSDB_HOST = os.environ.get('OPEN_TSDB_HOST')
url = f'http://{OPEN_TSDB_HOST}/api/put'
print(url)
r = requests.post(url, json=pt)
DEVICE = "90:E2:02:8F:76:7D"
print('Launching gatttool -I')
child = pexpect.spawn("gatttool -I")
print(f'Connecting to {DEVICE}')
child.sendline(f'connect {DEVICE}')
child.expect("Connection successful", timeout=5)
print('Connected, Hell yeah ! !')
time.sleep(1)
try:
while True:
try:
stats = get_minute_measure(child)
except Exception as e:
print("Sleep - Error 1")
time.sleep(5)
try:
send_stats(stats)
except Exception as e:
print("Sleep - Error 2")
time.sleep(5)
finally:
child.sendline('disconnect')
# vim: set ts=4 sw=4 et: