Commit b6b3458e authored by JackV's avatar JackV

Fix MQTT status handling

parent 869d0d09
......@@ -24,6 +24,7 @@ from . import notifier
def on_connect(client, userdata, flags, rc):
if not rc:
client.subscribe('sede/status', qos=2)
client.message_callback_add('sede/status', hooks.handle_status)
notifier.mqtt_instance = client
LOG.info('Connected!')
......
......@@ -10,12 +10,8 @@
Hooks called by `.handlers` to handle specific commands.
"""
# NOTE: don't forget to register your handler in RemoteListener.ACTIONS
# : and in __all__ below!!
import json
from bitsd.listener import notifier
from bitsd.persistence.engine import session_scope
from bitsd.persistence.models import Status
import bitsd.persistence.query as query
......@@ -31,9 +27,6 @@ __all__ = [
'handle_status',
]
status_map = {'open': 1,
'closed': 0}
def handle_temperature(client, userdata, message):
"""Receives and log data received from remote sensor."""
......@@ -60,15 +53,12 @@ def handle_status(client, userdata, message):
if status_msg['id'] == 'bits_server':
return
LOG.info('Received status: %r', status_msg)
status = status_map(status_msg['status'])
try:
status = int(status)
except ValueError:
LOG.error('Wrong parameter in status command')
return
if status not in (0, 1):
LOG.error('Non existent status %r, ignoring.', status)
return
if status_msg['status'] == 'open':
status = 1
elif status_msg['status'] == 'closed':
status = 0
else:
LOG.error('Received unknown status!')
textstatus = Status.OPEN if status == 1 else Status.CLOSED
with session_scope() as session:
......@@ -76,6 +66,5 @@ def handle_status(client, userdata, message):
if curstatus is None or curstatus.value != textstatus:
status = query.log_status(session, textstatus, 'BITS')
broadcast(status.jsondict())
notifier.send_status(textstatus)
else:
LOG.error('BITS already open/closed! Ignoring.')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment