Browse Source

add influxdb post to bleak test

Thomas Buck 2 months ago
parent
commit
921ac24390
1 changed files with 21 additions and 0 deletions
  1. 21
    0
      test_bleak/poll.py

+ 21
- 0
test_bleak/poll.py View File

4
 import asyncio
4
 import asyncio
5
 from bleak import BleakClient, BleakScanner
5
 from bleak import BleakClient, BleakScanner
6
 from bleak.exc import BleakDBusError
6
 from bleak.exc import BleakDBusError
7
+import aiohttp
8
+import asyncio
9
+
10
+influx_host = 'http://INFLUX_DB_IP_HERE:8086'
11
+influx_path = '/write?db=INFLUX_DB_NAME_HERE'
12
+
13
+cache = {}
14
+
15
+async def influx(name, value):
16
+    if not name in cache:
17
+        cache[name] = value
18
+    elif cache[name] == value:
19
+        return
20
+
21
+    data = "volcano,device=bleak " + name + "=" + str(float(value))
22
+    async with aiohttp.ClientSession(influx_host) as session:
23
+        await session.post(influx_path, data=data)
7
 
24
 
8
 async def ble_conn(address):
25
 async def ble_conn(address):
9
     attempts = 10
26
     attempts = 10
31
 async def get_current_temp(client):
48
 async def get_current_temp(client):
32
     val = await client.read_gatt_char("10110001-5354-4f52-5a26-4249434b454c")
49
     val = await client.read_gatt_char("10110001-5354-4f52-5a26-4249434b454c")
33
     num = int.from_bytes(val, byteorder="little")
50
     num = int.from_bytes(val, byteorder="little")
51
+    await influx("current", num)
34
     return num / 10.0
52
     return num / 10.0
35
 
53
 
36
 async def get_target_temp(client):
54
 async def get_target_temp(client):
40
 
58
 
41
 async def set_target_temp(client, temp):
59
 async def set_target_temp(client, temp):
42
     val = int(temp * 10.0)
60
     val = int(temp * 10.0)
61
+    await influx("target", val)
43
     d = val.to_bytes(4, byteorder="little")
62
     d = val.to_bytes(4, byteorder="little")
44
     await client.write_gatt_char("10110003-5354-4f52-5a26-4249434b454c", d)
63
     await client.write_gatt_char("10110003-5354-4f52-5a26-4249434b454c", d)
45
 
64
 
57
 
76
 
58
 async def set_state(client, state):
77
 async def set_state(client, state):
59
     heater, pump = state
78
     heater, pump = state
79
+    await influx("heater", heater)
80
+    await influx("pump", pump)
60
     if heater:
81
     if heater:
61
         await client.write_gatt_char("1011000f-5354-4f52-5a26-4249434b454c", 0)
82
         await client.write_gatt_char("1011000f-5354-4f52-5a26-4249434b454c", 0)
62
     else:
83
     else:

Loading…
Cancel
Save