From b2c8dc5d83a954c9dcafe3b84ac25bb9ed981a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Sun, 18 Feb 2024 00:41:47 +0100 Subject: [PATCH] added queue system for mem share between publisher and ti updater MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- server/app.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/server/app.py b/server/app.py index 4a2d4e6..372117c 100644 --- a/server/app.py +++ b/server/app.py @@ -7,7 +7,7 @@ import os from getmac import get_mac_address import time import sys -from multiprocessing import Process +from multiprocessing import Process, Queue import asyncio app = Flask(__name__) @@ -109,19 +109,13 @@ def connect_mqtt(): client.connect("localhost", 1883) return client -if __name__ == "__main__": - flask_thread = Process(target=flask_api) - flask_thread.start() - +def mqtt_publisher(data_queue): # 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) while True: - cur = db.cursor() - cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") - TUBE_INDEX = cur.fetchall() - cur.close() + tube_index = data_queue.get() try: # Gets whatever the last Art-Net packet we received is artNetPacket = artNet.readPacket() @@ -130,8 +124,8 @@ if __name__ == "__main__": #Checks to see if the current packet is for the specified DMX Universe dmxPacket = artNetPacket.data # Create MQTT topic based on the universe and channel - if TUBE_INDEX is not None: - for index_row in TUBE_INDEX: + if tube_index is not None: + for index_row in tube_index: if artNetPacket.universe == int(index_row[1]): dmx_address = int(index_row[2]) #Define RGB values per pixel @@ -144,4 +138,24 @@ 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(data_queue): + try: + cur = db.cursor() + cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") + tube_index = cur.fetchall() + cur.close() + data_queue.put(tube_index) + + except Exception as e: + print(e) + +if __name__ == "__main__": + data_queue = Queue() + ti_updater_thread = Process(target=tube_index_updater(), args=(data_queue, )) + ti_updater_thread.start() + publisher_thread = Process(target=mqtt_publisher, args=(data_queue, )) + publisher_thread.start() + flask_thread = Process(target=flask_api) + flask_thread.start() \ No newline at end of file