queues
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 7s

This commit is contained in:
Matthew Frost 2024-02-28 20:10:53 +01:00
parent e0a0dedea9
commit 3e9b086c33
4 changed files with 49 additions and 2 deletions

View file

@ -6,6 +6,7 @@ from app.utils import get_baytech_status_outlet_telnet
from flask_socketio import SocketIO, emit
from gevent import monkey
from app.jobs import scheduler, generate_batch_jobs
from app.threads import generate_threads
socketio = SocketIO(async_mode="gevent", logger=True, engineio_logger=True, cors_allowed_origins="*")
@ -43,7 +44,7 @@ def create_app():
app.register_blueprint(routes)
powerbar_status()
scheduler.init_app(app)
generate_batch_jobs()
generate_threads()
socketio.init_app(app)
return app

File diff suppressed because one or more lines are too long

39
app/threads.py Normal file
View file

@ -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()

View file

@ -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: