chore: added the instant thread for new station
parent
6d3899e8e1
commit
494e6b4c7c
|
|
@ -201,8 +201,8 @@ def add_station():
|
|||
db.session.add(new_station)
|
||||
db.session.commit()
|
||||
|
||||
# You might want to start the new MQTT client here as well
|
||||
# start_single_mqtt_client(new_station)
|
||||
# Immediately start the MQTT client for the station just created.
|
||||
start_single_mqtt_client(new_station)
|
||||
|
||||
return jsonify({"message": "Station added successfully."}), 201
|
||||
|
||||
|
|
@ -424,21 +424,16 @@ def handle_rpc_request(payload):
|
|||
print(f"Publishing to {topic}")
|
||||
mqtt_client.client.publish(topic, serialized_payload)
|
||||
|
||||
# --- Main Application Logic ---
|
||||
def start_mqtt_clients():
|
||||
# ADD THIS NEW FUNCTION
|
||||
def start_single_mqtt_client(station):
|
||||
"""
|
||||
Initializes and starts an MQTT client for each station found in the database,
|
||||
using the specific MQTT credentials stored for each station.
|
||||
Creates and starts a new MQTT client thread for a SINGLE station.
|
||||
This is our new reusable function.
|
||||
"""
|
||||
try:
|
||||
with app.app_context():
|
||||
stations = Station.query.all()
|
||||
except Exception as e:
|
||||
print(f"CRITICAL: Could not query stations from the database in MQTT thread: {e}")
|
||||
if station.station_id in mqtt_clients and mqtt_clients[station.station_id].is_connected:
|
||||
print(f"MQTT client for {station.station_id} is already running.")
|
||||
return
|
||||
|
||||
for station in stations:
|
||||
if station.station_id not in mqtt_clients:
|
||||
print(f"Creating and starting MQTT client for station: {station.name} ({station.station_id})")
|
||||
|
||||
client = MqttClient(
|
||||
|
|
@ -449,9 +444,53 @@ def start_mqtt_clients():
|
|||
station_id=station.station_id,
|
||||
on_message_callback=on_message_handler
|
||||
)
|
||||
client.start()
|
||||
client.start() # The start method should handle threading
|
||||
mqtt_clients[station.station_id] = client
|
||||
|
||||
# --- Main Application Logic ---
|
||||
# def start_mqtt_clients():
|
||||
# """
|
||||
# Initializes and starts an MQTT client for each station found in the database,
|
||||
# using the specific MQTT credentials stored for each station.
|
||||
# """
|
||||
# try:
|
||||
# with app.app_context():
|
||||
# stations = Station.query.all()
|
||||
# except Exception as e:
|
||||
# print(f"CRITICAL: Could not query stations from the database in MQTT thread: {e}")
|
||||
# return
|
||||
|
||||
# for station in stations:
|
||||
# if station.station_id not in mqtt_clients:
|
||||
# print(f"Creating and starting MQTT client for station: {station.name} ({station.station_id})")
|
||||
|
||||
# client = MqttClient(
|
||||
# broker=station.mqtt_broker,
|
||||
# port=station.mqtt_port,
|
||||
# user=station.mqtt_user,
|
||||
# password=station.mqtt_password,
|
||||
# station_id=station.station_id,
|
||||
# on_message_callback=on_message_handler
|
||||
# )
|
||||
# client.start()
|
||||
# mqtt_clients[station.station_id] = client
|
||||
|
||||
def start_mqtt_clients():
|
||||
"""
|
||||
Initializes and starts an MQTT client for each station found in the database
|
||||
by calling our new reusable function.
|
||||
"""
|
||||
try:
|
||||
with app.app_context():
|
||||
stations = Station.query.all()
|
||||
print(f"Found {len(stations)} existing stations to monitor.")
|
||||
except Exception as e:
|
||||
print(f"CRITICAL: Could not query stations from the database: {e}")
|
||||
return
|
||||
|
||||
for station in stations:
|
||||
start_single_mqtt_client(station)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
with app.app_context():
|
||||
|
|
|
|||
Loading…
Reference in New Issue