Update MQTT start up

This commit is contained in:
2026-01-02 18:52:46 +01:00
parent 35087ad54a
commit cd0e5f15ed
3 changed files with 42 additions and 22 deletions

View File

@@ -1,3 +1,5 @@
from typing import Callable
from board_mate.auth import *
from flask import jsonify, request
from pydantic import StrictStr
@@ -8,18 +10,30 @@ from src.models.AuthData import AuthData
class AuthController:
_auth_data : AuthData = None
_is_logged_in : bool = None
_login_callback : Callable[[AuthData], None] = None
def __init__(self, app, auth_data, host):
self._register_routes(app)
self.config = Configuration(host=host)
self._auth_data = auth_data
self.config.verify_ssl = False
self._auth_data = auth_data
self.is_logged_in = False
def _register_routes(self, app):
app.add_url_rule("/client/auth", view_func=self.auth, methods=['POST'])
def set_on_login(self, handler : Callable[[AuthData], None]):
self._login_callback = handler
def _notify_login(self, data : AuthData):
if not self._login_callback: return
self._login_callback(data)
def auth(self):
try:
if self._is_logged_in: jsonify({"success" : False, "message" : "Already logged in", "data" : None}), 400
received_data = request.get_json()
print(received_data)
with ApiClient(self.config) as client:
@@ -37,6 +51,8 @@ class AuthController:
self._auth_data.set_token(response.data["authToken"])
self._auth_data.set_client_id(response.data["clientId"])
self._notify_login(self._auth_data)
self._is_logged_in = True
return jsonify({"success" : response.success, "message" : response.message , "data" : response.data}), 200
except Exception as e:
print(f"Exception when calling AuthAPI->login: {e}")

View File

@@ -79,9 +79,9 @@ class MQTTService:
self._subscriptions[topic] = handler
def on_message(client, userdata, msg):
for sub_topic, h in self._subscriptions.items():
for sub_topic, sub_handler in self._subscriptions.items():
if mqtt.topic_matches_sub(sub_topic, msg.topic):
h(msg.topic, msg.payload.decode())
sub_handler(msg.topic, msg.payload.decode())
self.client.on_message = on_message
self.client.subscribe(topic, qos=qos)