ソースを参照

add influxdb post to bleak test

Thomas Buck 4ヶ月前
コミット
921ac24390
1個のファイルの変更21行の追加0行の削除
  1. 21
    0
      test_bleak/poll.py

+ 21
- 0
test_bleak/poll.py ファイルの表示

@@ -4,6 +4,23 @@ import sys
4 4
 import asyncio
5 5
 from bleak import BleakClient, BleakScanner
6 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 25
 async def ble_conn(address):
9 26
     attempts = 10
@@ -31,6 +48,7 @@ async def ble_conn(address):
31 48
 async def get_current_temp(client):
32 49
     val = await client.read_gatt_char("10110001-5354-4f52-5a26-4249434b454c")
33 50
     num = int.from_bytes(val, byteorder="little")
51
+    await influx("current", num)
34 52
     return num / 10.0
35 53
 
36 54
 async def get_target_temp(client):
@@ -40,6 +58,7 @@ async def get_target_temp(client):
40 58
 
41 59
 async def set_target_temp(client, temp):
42 60
     val = int(temp * 10.0)
61
+    await influx("target", val)
43 62
     d = val.to_bytes(4, byteorder="little")
44 63
     await client.write_gatt_char("10110003-5354-4f52-5a26-4249434b454c", d)
45 64
 
@@ -57,6 +76,8 @@ async def get_state(client):
57 76
 
58 77
 async def set_state(client, state):
59 78
     heater, pump = state
79
+    await influx("heater", heater)
80
+    await influx("pump", pump)
60 81
     if heater:
61 82
         await client.write_gatt_char("1011000f-5354-4f52-5a26-4249434b454c", 0)
62 83
     else:

読み込み中…
キャンセル
保存