Added polling based on mqtt configuration
This commit is contained in:
@@ -3,6 +3,11 @@ from btlewrap.bluepy import BluepyBackend
|
|||||||
from miflora import miflora_scanner
|
from miflora import miflora_scanner
|
||||||
from miflora.miflora_poller import (MI_BATTERY, MI_CONDUCTIVITY, MI_LIGHT, MI_MOISTURE, MI_TEMPERATURE)
|
from miflora.miflora_poller import (MI_BATTERY, MI_CONDUCTIVITY, MI_LIGHT, MI_MOISTURE, MI_TEMPERATURE)
|
||||||
import time
|
import time
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import json
|
||||||
|
|
||||||
|
devices = []
|
||||||
|
this_client = "test_client"
|
||||||
|
|
||||||
def poll_miflora(address):
|
def poll_miflora(address):
|
||||||
poller = MiFloraPoller(address, BluepyBackend)
|
poller = MiFloraPoller(address, BluepyBackend)
|
||||||
@@ -17,16 +22,45 @@ def poll_miflora(address):
|
|||||||
|
|
||||||
return miflora_device
|
return miflora_device
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
print("Connected with result code "+str(rc))
|
||||||
|
|
||||||
|
client.subscribe("dev/mqtt-miflora/device-configuration")
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
devices_all = json.loads(msg.payload.decode("utf-8"))
|
||||||
|
try:
|
||||||
|
for device in devices_all["devices"]:
|
||||||
|
try:
|
||||||
|
if device["client"] == this_client:
|
||||||
|
devices.append(device)
|
||||||
|
except KeyError as err:
|
||||||
|
print("Skipping device. Key not found: '{0}'".format(err))
|
||||||
|
except Exception as err:
|
||||||
|
print("Error decoding json configuration: {0}".format(err))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sleep_minutes = 1
|
sleep_minutes = 1
|
||||||
backend = BluepyBackend
|
backend = BluepyBackend
|
||||||
|
|
||||||
#devices = miflora_scanner.scan(BluepyBackend, 10)
|
#devices = miflora_scanner.scan(BluepyBackend, 10)
|
||||||
|
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
|
client.connect("chimaera", 1883, 60)
|
||||||
|
|
||||||
|
client.loop_start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
device_1 = poll_miflora("C4:7C:8D:67:2E:1C")
|
if len(devices) == 0:
|
||||||
device_2 = poll_miflora("C4:7C:8D:67:2E:4A")
|
time.sleep(1)
|
||||||
print(device_1)
|
else:
|
||||||
print(device_2)
|
for device in devices:
|
||||||
time.sleep(sleep_minutes * 60)
|
polled_dev = poll_miflora(device["mac"])
|
||||||
|
print("MIFlora {0}, -- {1}".format(device["name"], polled_dev))
|
||||||
|
time.sleep(sleep_minutes * 60)
|
||||||
|
|
||||||
|
client.loop_stop()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
bluepy
|
bluepy
|
||||||
miflora
|
miflora
|
||||||
|
paho-mqtt
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import json
|
|||||||
device_1 = {}
|
device_1 = {}
|
||||||
device_1["mac"] = "C4:7C:8D:67:2E:1C"
|
device_1["mac"] = "C4:7C:8D:67:2E:1C"
|
||||||
device_1["name"] = "Pflanze 1"
|
device_1["name"] = "Pflanze 1"
|
||||||
device_1["client"] = "Controller_id_1"
|
device_1["client"] = "test_client"
|
||||||
|
|
||||||
device_2 = {}
|
device_2 = {}
|
||||||
device_2["max"] = "C4:7C:8D:67:2E:4A"
|
device_2["mac"] = "C4:7C:8D:67:2E:4A"
|
||||||
device_2["name"] = "Pflanze 2"
|
device_2["name"] = "Pflanze 2"
|
||||||
device_2["client"] = "Controller_id_1"
|
device_2["client"] = "test_client"
|
||||||
|
|
||||||
devices = {}
|
devices = {}
|
||||||
devices["devices"] = [device_1, device_2]
|
devices["devices"] = [device_1, device_2]
|
||||||
|
|||||||
Reference in New Issue
Block a user