Move MQTT settings to settings.py
This commit is contained in:
+12
-14
@@ -3,34 +3,32 @@ import logging
|
||||
import datetime
|
||||
from collections import deque
|
||||
|
||||
MQTT_HOST = 'mqtt.sik.party'
|
||||
|
||||
TOPIC_BREW_TIME = 'sik/kiltahuone/kahvivaaka/brewtime'
|
||||
TOPIC_WEIGHT = 'sik/kiltahuone/kahvivaaka/weight'
|
||||
TOPIC_BREWING = 'sik/kiltahuone/kahvivaaka/brewing'
|
||||
TOPIC_CUPS = 'sik/kiltahuone/kahvivaaka/cups'
|
||||
from django.conf import settings
|
||||
|
||||
HOST = settings.MQTT_SETTINGS['HOST']
|
||||
PORT = settings.MQTT_SETTINGS['PORT']
|
||||
TOPICS = settings.MQTT_SETTINGS['TOPICS']
|
||||
latest = {}
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
logging.info('Connected successfully to MQTT.')
|
||||
logging.info('Subscribing to all topics on {}.'.format(MQTT_HOST))
|
||||
logging.info('Subscribing to all topics on {}.'.format(HOST))
|
||||
client.subscribe('sik/kiltahuone/kahvivaaka/#')
|
||||
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
payload = msg.payload.decode('utf-8')
|
||||
if msg.topic == TOPIC_WEIGHT:
|
||||
if msg.topic == TOPICS['WEIGHT']:
|
||||
weight = float(payload)
|
||||
latest['weight'] = weight
|
||||
elif msg.topic == TOPIC_CUPS:
|
||||
elif msg.topic == TOPICS['CUPS']:
|
||||
cups = float(payload)
|
||||
latest['cups'] = cups
|
||||
elif msg.topic == TOPIC_BREWING:
|
||||
elif msg.topic == TOPICS['BREWING']:
|
||||
brewing = bool(int(payload))
|
||||
latest['brewing'] = brewing
|
||||
elif msg.topic == TOPIC_BREW_TIME:
|
||||
elif msg.topic == TOPICS['BREW_TIME']:
|
||||
brew_time = datetime.datetime.fromtimestamp(float(payload))
|
||||
latest['brew_time'] = brew_time
|
||||
|
||||
@@ -54,9 +52,9 @@ client.on_disconnect = on_disconnect
|
||||
|
||||
|
||||
try:
|
||||
logging.info('Connecting to MQTT at {}...'.format(MQTT_HOST))
|
||||
logging.info('Connecting to MQTT at {}...'.format(HOST))
|
||||
logging.info('If there is no confirmation, the MQTT connection has probably failed.')
|
||||
client.connect_async(MQTT_HOST, 1883, 60)
|
||||
client.connect_async(HOST, PORT, 60)
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
logging.error('Failed to connect to MQTT at {}'.format(MQTT_HOST))
|
||||
logging.error('Failed to connect to MQTT at {}'.format(HOST))
|
||||
|
||||
@@ -274,3 +274,14 @@ MEDIA_URL = '/media/'
|
||||
HSL_USERHASH = 'YOUR HSL USERHASH HERE'
|
||||
HSL_DEPARTURE_THRESHOLD = 8
|
||||
HSL_HURRY_THRESHOLD = 13
|
||||
|
||||
MQTT_SETTINGS = {
|
||||
'HOST': 'mqtt.sik.party',
|
||||
'PORT': 1883,
|
||||
'TOPICS': {
|
||||
'BREW_TIME': 'sik/kiltahuone/kahvivaaka/brewtime',
|
||||
'WEIGHT': 'sik/kiltahuone/kahvivaaka/weight',
|
||||
'BREWING': 'sik/kiltahuone/kahvivaaka/brewing',
|
||||
'CUPS': 'sik/kiltahuone/kahvivaaka/cups',
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user