For the API vist '/powerbars' and '/groups' | A project brought to you by Piele & Mattronix for the code vist
code.techinc.nl
diff --git a/app/threads.py b/app/threads.py
new file mode 100644
index 0000000..fa36354
--- /dev/null
+++ b/app/threads.py
@@ -0,0 +1,39 @@
+import threading
+from app.settings import powerbars
+import time
+from app.utils import run_telnet_command
+from queue import Queue
+
+queues = {}
+
+def baytech_batch_update(powerbar, powerbars, queue):
+ while True:
+ time.sleep(1)
+ print(f"Running Sync Job: {powerbar}")
+ print(f"Queue Size: {queue.qsize()}")
+
+ if queue.qsize() > 0:
+ print(f"Queue Size: {queue.qsize()}")
+ try:
+ job = queue.get()
+ print(f"Job: {job}")
+ outlet = job['outlet']
+ action = job['action']
+ print(f"Turning {action} powerbar {powerbar} outlet {outlet}")
+ command = run_telnet_command(powerbar,f"{action} {outlet}")
+ if command:
+ powerbars[powerbar]['outlets'][outlet]['state'] = action
+ print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
+ except Exception as E:
+ print(f"Job Exception Error: {E}")
+ pass
+
+
+def generate_threads():
+ for powerbar in powerbars:
+ if powerbars[powerbar]['method'] == 'batch_telnet':
+ if powerbars[powerbar]['type'] == 'baytech':
+
+ queues[powerbar] = Queue(maxsize=100)
+ t = threading.Thread(target=baytech_batch_update, args=[powerbar, powerbars, queues[powerbar]])
+ t.start()
\ No newline at end of file
diff --git a/app/views.py b/app/views.py
index 7d82883..573ab43 100644
--- a/app/views.py
+++ b/app/views.py
@@ -4,6 +4,7 @@ from app.utils import run_telnet_command
from flask import jsonify
import time
import app
+from app.threads import queues
routes = Blueprint('routes', __name__)
@@ -93,6 +94,7 @@ def powerbar_control(powerbar, outlet, action):
command = run_telnet_command(powerbar,f"{action} {outlet}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : action})
command = True
if command:
@@ -111,6 +113,7 @@ def powerbar_control(powerbar, outlet, action):
command = run_telnet_command(powerbar,f"off {outlet}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : "off"})
command = True
if command:
@@ -123,6 +126,7 @@ def powerbar_control(powerbar, outlet, action):
command = run_telnet_command(powerbar,f"on {outlet}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : "on"})
command = True
if command:
@@ -137,6 +141,7 @@ def powerbar_control(powerbar, outlet, action):
command = run_telnet_command(powerbar,f"off {outlet}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : "off"})
command = True
if command:
@@ -149,6 +154,7 @@ def powerbar_control(powerbar, outlet, action):
command = run_telnet_command(powerbar,f"on {outlet}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : "off"})
command = True
if command:
@@ -239,6 +245,7 @@ def powebar_group_action(group, action):
command = run_telnet_command(powerbar,f"{action} {outlets}")
if powerbars[powerbar]['method'] == "batch_telnet":
+ queues[powerbar].put({"outlet": outlet, "action" : action})
command = True
if command: