gets default state on reboot
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 6s

This commit is contained in:
Matthew Frost 2023-11-20 11:33:19 +01:00
parent 91f0971aef
commit 4062918ebb
4 changed files with 54 additions and 3 deletions

View file

@ -1,12 +1,30 @@
from flask import Flask, request, redirect, url_for, render_template
from flask import Flask
from app.views import routes
from flask import jsonify
from app.settings import powerbars
from app.utils import get_baytech_status_outlet
def create_app():
# create and configure the app
app = Flask(__name__)
app.config.from_pyfile('settings.py')
app.register_blueprint(routes)
for powerbar_name, powerbar_info in powerbars.items():
print(f"Checking powerbar state {powerbar_name}")
if powerbar_info['type'] == 'baytech':
try:
print("Is a baytech powerbar, getting outlet status")
outlets = get_baytech_status_outlet(powerbar_info['host'], powerbar_info['port'])
try:
for outlet_id, outlet_info in powerbar_info['outlets'].items():
powerbar_info['outlets'][outlet_id]['state'] = outlets[outlet_id].lower()
print(f" Powerbar: {powerbar_name} Outlet: {outlet_id}, State: {outlets[outlet_id].lower()}")
except:
print(f"Failed to get outlet status from powerbar {powerbar_name} outlet: {outlet_id}")
continue
except:
print(f"Failed to get outlet status from powerbar {powerbar_name}")
continue
return app
app = create_app()
@ -19,3 +37,4 @@ def not_authorised(e):
def internal_server_error(e):
return jsonify(error="Internal server error"), 500

View file

@ -1,10 +1,11 @@
from app.utils import get_baytech_status_outlet
powerbars = {
"powerbar-aux-space" : {
"name" : "Powerbar Aux Space",
"host" : "10.209.10.111",
"port" : 3696,
"type" : "baytech",
"description" : "Powerbar Controlling Aux Space",
"outlets" : {
1 : { "name" : "Aux Main Table Light" },
@ -33,6 +34,7 @@ powerbars = {
"name" : "Powerbar Main Space",
"host" : "10.209.10.111",
"port" : 3697,
"type" : "baytech",
"description" : "Powerbar Controlling Main Space",
"outlets" : {
4 : { "name" : "LIGHT_MAKERTABLE" },
@ -62,3 +64,5 @@ powerbars = {
]
}
}

File diff suppressed because one or more lines are too long

28
app/utils.py Normal file
View file

@ -0,0 +1,28 @@
import telnetlib
import re
def get_baytech_status_outlet(hostname,port):
try:
# Create a Telnet object and connect to the host
tn = telnetlib.Telnet(hostname, port)
# Send the command you want to execute
command = "status"
tn.write(command.encode('ascii') + b"\r\n")
# Read the output until you receive the prompt or a timeout occurs
output = tn.read_until(b"<prompt>", timeout=5).decode('ascii')
# Close the Telnet connection
tn.close()
pattern = r"(\d+)\)\.\.\.Outlet\s+(\d+)\s+:\s+(\w+)"
outlets = {}
matches = re.findall(pattern, output)
for match in matches:
number = int(match[1])
status = match[2]
outlets[number] = status
return outlets
except:
return None