45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from queue import Queue
|
|
from dotenv import dotenv_values
|
|
import paho.mqtt.client as mqtt
|
|
import json
|
|
import os
|
|
print(f"__init__.py eseguito in PID {os.getpid()}")
|
|
|
|
config = dotenv_values(".env")
|
|
|
|
listClients_queue = Queue()
|
|
listRoles_queue = Queue()
|
|
getClient_queue = Queue()
|
|
changeClient_queue = Queue()
|
|
|
|
command_queue_map = {
|
|
"listClients": listClients_queue,
|
|
"listRoles": listRoles_queue,
|
|
"getClient": getClient_queue,
|
|
"createClient": changeClient_queue,
|
|
"deleteClient": changeClient_queue,
|
|
"enableClient": changeClient_queue,
|
|
"disableClient": changeClient_queue
|
|
}
|
|
|
|
def on_message(client, userdata, msg):
|
|
msg_json = json.loads(msg.payload.decode("utf-8"))
|
|
command = msg_json['responses'][0]['command']
|
|
print(f"Received message: {msg.payload.decode("utf-8")} - Command: {command}")
|
|
|
|
if command in command_queue_map:
|
|
command_queue_map[command].put(msg.payload.decode("utf-8"))
|
|
|
|
def on_connect(client, userdata, flags, rc, properties):
|
|
if rc == 0:
|
|
print('Connected successfully. Properties:', properties)
|
|
client.subscribe(config['MQTT_DS_RESP_TOPIC'])
|
|
else:
|
|
print('Bad connection. Code:', rc)
|
|
|
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv5)
|
|
client.on_connect = on_connect
|
|
client.on_message = on_message
|
|
client.username_pw_set(config['MQTT_USER'], config['MQTT_PASSWORD'])
|
|
client.connect(config['MQTT_HOST'], int(config['MQTT_PORT']), int(config['MQTT_KEEPALIVE']))
|
|
client.loop_start() |