Make MqttClient a singleton
parent
4dbba2b44c
commit
50d67fd521
|
@ -3,12 +3,13 @@ from flask import Blueprint
|
|||
from .mqtt_client import MqttClient
|
||||
|
||||
devices = Blueprint('devices', __name__)
|
||||
mqtt_client = MqttClient()
|
||||
mqtt_client = None
|
||||
|
||||
|
||||
# When app dies, stop mqtt connection
|
||||
def on_stop():
|
||||
mqtt_client.tear_down()
|
||||
if mqtt_client:
|
||||
mqtt_client.tear_down()
|
||||
|
||||
|
||||
atexit.register(on_stop)
|
||||
|
@ -22,4 +23,8 @@ def hello():
|
|||
|
||||
@devices.record
|
||||
def on_blueprint_setup(setup_state):
|
||||
mqtt_client.setup(setup_state.app)
|
||||
print('Blueprint setup')
|
||||
mqtt_client = MqttClient()
|
||||
|
||||
if mqtt_client:
|
||||
mqtt_client.setup(setup_state.app)
|
||||
|
|
|
@ -5,11 +5,25 @@ from .models import Recording
|
|||
from app import db, app
|
||||
|
||||
class MqttClient:
|
||||
def __init__(self):
|
||||
self.mqtt = Mqtt()
|
||||
self.initialized = False
|
||||
class __MqttClient:
|
||||
def __init__(self):
|
||||
self.mqtt = Mqtt()
|
||||
self.initialized = False
|
||||
def __str__(self):
|
||||
return repr(self)
|
||||
|
||||
|
||||
instance = None
|
||||
|
||||
|
||||
def __init__(self):
|
||||
if not MqttClient.instance:
|
||||
MqttClient.instance = MqttClient.__MqttClient()
|
||||
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.instance, name)
|
||||
|
||||
# Mqtt setup
|
||||
def setup(self, app):
|
||||
if not self.initialized:
|
||||
|
|
Loading…
Reference in New Issue