diff --git a/server/app.py b/server/app.py index 4a2d4e6..853c6cc 100644 --- a/server/app.py +++ b/server/app.py @@ -109,19 +109,24 @@ def connect_mqtt(): client.connect("localhost", 1883) return client -if __name__ == "__main__": - flask_thread = Process(target=flask_api) - flask_thread.start() +def mqtt_publisher(): + def on_message(mqttc, obj, msg): + print(msg.payload) + print(type(msg.payload)) + global TUBE_INDEX + TUBE_INDEX = msg.payload + # Set Connecting Client ID + mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + # client.username_pw_set(username, password) + mqtt_client.on_message = on_message + mqtt_client.connect("localhost", 1883) # Create and start a thread for each universe - mqtt_client = connect_mqtt() artnetBindIp = get_eth0_ip() artNet = Artnet.Artnet(BINDIP = artnetBindIp, DEBUG = True, SHORTNAME = "PiXelTubeMaster", LONGNAME = "PiXelTubeMaster", PORT = 6454) + mqtt_client.subscribe("pxm/tube_index") + mqtt_client.loop_start() while True: - cur = db.cursor() - cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") - TUBE_INDEX = cur.fetchall() - cur.close() try: # Gets whatever the last Art-Net packet we received is artNetPacket = artNet.readPacket() @@ -144,4 +149,21 @@ if __name__ == "__main__": mqtt_client.publish(p1_topic, str([str([p1_r, p1_g, p1_b]), str([p2_r, p2_g, p2_b]), str([p3_r, p3_g, p3_b]), str([p4_r, p4_g, p4_b]), str([p5_r, p5_g, p5_b]), str([p6_r, p6_g, p6_b])])) except KeyboardInterrupt: artNet.close() - sys.exit() \ No newline at end of file + sys.exit() + +def tube_index_updater(): + mqtt_client = connect_mqtt() + cur = db.cursor() + cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") + TUBE_INDEX = cur.fetchall() + cur.close() + mqtt_client.publish("pxm/tube_index", str(TUBE_INDEX)) + + +if __name__ == "__main__": + update_tube_index_thread = Process(target=tube_index_updater) + update_tube_index_thread.start() + flask_thread = Process(target=flask_api) + flask_thread.start() + publisher_thread = Process(target=mqtt_publisher) + publisher_thread.start() \ No newline at end of file