ti-powerbar/app/__init__.py
Matthew Frost 4062918ebb
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 6s
gets default state on reboot
2023-11-20 11:33:19 +01:00

40 lines
1.5 KiB
Python

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()
@app.errorhandler(403)
def not_authorised(e):
return jsonify(error="Not authorized"), 403
@app.errorhandler(500)
def internal_server_error(e):
return jsonify(error="Internal server error"), 500