diff --git a/coffee_scale/__init__.py b/coffee_scale/__init__.py index 61bc51a..e69de29 100644 --- a/coffee_scale/__init__.py +++ b/coffee_scale/__init__.py @@ -1,4 +0,0 @@ -from . import mqtt -import logging - -mqtt.client.loop_start() diff --git a/coffee_scale/apps.py b/coffee_scale/apps.py index 7785abc..57c4c5b 100644 --- a/coffee_scale/apps.py +++ b/coffee_scale/apps.py @@ -1,5 +1,23 @@ from django.apps import AppConfig +import logging +import sys + +from coffee_scale import mqtt + class CoffeeScaleConfig(AppConfig): name = 'coffee_scale' + + def ready(self): + if ('makemigrations' in sys.argv or 'migrate' in sys.argv): + return + + try: + logging.info('Connecting to MQTT (coffee scale) at {}...'.format(mqtt.HOST)) + logging.info('If there is no confirmation, the MQTT connection has probably failed.') + mqtt.client.connect_async(mqtt.HOST, mqtt.PORT, 60) + mqtt.client.loop_start() + except Exception as ex: + logging.error(ex) + logging.error('Failed to connect to MQTT at {}'.format(mqtt.HOST)) diff --git a/coffee_scale/mqtt.py b/coffee_scale/mqtt.py index ee49cd7..47a4592 100644 --- a/coffee_scale/mqtt.py +++ b/coffee_scale/mqtt.py @@ -56,12 +56,3 @@ client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect - - -try: - logging.info('Connecting to MQTT at {}...'.format(HOST)) - logging.info('If there is no confirmation, the MQTT connection has probably failed.') - client.connect_async(HOST, PORT, 60) -except Exception as ex: - logging.error(ex) - logging.error('Failed to connect to MQTT at {}'.format(HOST)) diff --git a/infoscreen/hsl_fetcher.py b/infoscreen/hsl_fetcher.py index 6ad17f7..d4d00ea 100644 --- a/infoscreen/hsl_fetcher.py +++ b/infoscreen/hsl_fetcher.py @@ -16,8 +16,8 @@ class HSLFetcher: last_fetched = datetime.fromtimestamp(86400) # epoch INTERVAL = 1 # minutes - logging.info( - "Set up scheduled HSL API fetch every {} minutes".format(INTERVAL)) + # logging.info( + # "Set up scheduled HSL API fetch every {} minutes".format(INTERVAL)) def fetch_if_needed(self): """Check if new fetch from HSL API is needed.""" diff --git a/sikweb/base.py b/sikweb/base.py index 527c645..c094344 100644 --- a/sikweb/base.py +++ b/sikweb/base.py @@ -78,7 +78,7 @@ INSTALLED_APPS = [ 'webapp', 'members', 'infoscreen', - 'coffee_scale', + 'coffee_scale.apps.CoffeeScaleConfig', 'rest_framework', 'django_nose', 'bootstrap3',