mqtt_client.py: move implemntation into MqttClient class
This commit is contained in:
parent
5ae6d1f832
commit
6acb919486
2 changed files with 44 additions and 38 deletions
|
@ -22,13 +22,13 @@ def main():
|
||||||
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
|
|
||||||
client.connect(args.mqtt_hostname, mqtt_client.MQTT_SERVER_PORT, 60)
|
client.connect(args.mqtt_hostname, mqtt_client.MqttClient.MQTT_SERVER_PORT, 60)
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
|
||||||
j = json.dumps({"command": "scene_recall", "args": {"scene_id": 0}})
|
j = json.dumps({"command": "scene_recall", "args": {"scene_id": 0}})
|
||||||
# j = json.dumps({'command': 'shutdown'})
|
# j = json.dumps({'command': 'shutdown'})
|
||||||
|
|
||||||
client.publish(mqtt_client.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False)
|
client.publish(mqtt_client.MqttClient.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False)
|
||||||
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,11 @@ MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer"
|
||||||
m: Mixer = None
|
m: Mixer = None
|
||||||
|
|
||||||
|
|
||||||
def mqtt_autodiscovery(client):
|
class MqttClient:
|
||||||
|
def __init__(self):
|
||||||
|
self.client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
||||||
|
|
||||||
|
def mqtt_autodiscovery(self, client):
|
||||||
j = json.dumps(
|
j = json.dumps(
|
||||||
{
|
{
|
||||||
"name": "Reset Settings",
|
"name": "Reset Settings",
|
||||||
|
@ -32,16 +36,15 @@ def mqtt_autodiscovery(client):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False)
|
self, client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False)
|
||||||
|
|
||||||
|
def on_connect(self, client, userdata, flags, reason_code, properties):
|
||||||
def on_connect(client, userdata, flags, reason_code, properties):
|
|
||||||
print(f"Connected: {reason_code}")
|
print(f"Connected: {reason_code}")
|
||||||
|
|
||||||
client.subscribe(MQTT_COMMAND_TOPIC)
|
client.subscribe(MQTT_COMMAND_TOPIC)
|
||||||
mqtt_autodiscovery(client)
|
self.mqtt_autodiscovery(client)
|
||||||
|
|
||||||
|
def on_message(self, client, userdata, msg):
|
||||||
def on_message(client, userdata, msg):
|
|
||||||
payload = msg.payload.decode()
|
payload = msg.payload.decode()
|
||||||
|
|
||||||
print(f"{msg.topic}: {payload}")
|
print(f"{msg.topic}: {payload}")
|
||||||
|
@ -54,6 +57,13 @@ def on_message(client, userdata, msg):
|
||||||
case "shutdown":
|
case "shutdown":
|
||||||
m.shutdown()
|
m.shutdown()
|
||||||
|
|
||||||
|
def run(self, hostname):
|
||||||
|
self.client.on_connect = self.on_connect
|
||||||
|
self.client.on_message = self.on_message
|
||||||
|
|
||||||
|
self.client.connect(hostname, MQTT_SERVER_PORT, 60)
|
||||||
|
self.client.loop_forever()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Allen & Heath Qu MQTT Remote Control")
|
parser = argparse.ArgumentParser(description="Allen & Heath Qu MQTT Remote Control")
|
||||||
|
@ -78,12 +88,8 @@ def main():
|
||||||
global m
|
global m
|
||||||
m = mixer.Mixer(args.mixer_ip)
|
m = mixer.Mixer(args.mixer_ip)
|
||||||
|
|
||||||
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
client = MqttClient()
|
||||||
client.on_connect = on_connect
|
client.run(args.mqtt_hostname)
|
||||||
client.on_message = on_message
|
|
||||||
|
|
||||||
client.connect(args.mqtt_hostname, MQTT_SERVER_PORT, 60)
|
|
||||||
client.loop_forever()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue