powerbar
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 2023-12-02 18:17:42 +01:00
parent d649cbfb18
commit 79c240c2f5
4 changed files with 39 additions and 10 deletions

View file

@ -3,6 +3,16 @@ from app.views import routes
from flask import jsonify
from app.settings import powerbars
from app.utils import get_baytech_status_outlet
from flask_socketio import SocketIO, emit
socketio = SocketIO(engineio_logger=True, logger=True, cors_allowed_origins="*")
@socketio.on('connect', namespace='/powerupdates')
def connect_handler():
print('connected')
emit('response', {'meta': 'WS connected'})
def create_app():
# create and configure the app
@ -25,6 +35,7 @@ def create_app():
except:
print(f"Failed to get outlet status from powerbar {powerbar_name}")
continue
socketio.init_app(app)
return app
app = create_app()
@ -37,4 +48,3 @@ def not_authorised(e):
def internal_server_error(e):
return jsonify(error="Internal server error"), 500

View file

@ -203,11 +203,23 @@ input:checked + .slider:before {
<!-- Bootstrap JS -->
<script type="text/javascript" src="node_modules/mdbootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="node_modules/mdbootstrap/js/popper.min.js"></script>
<script type="text/javascript" src="node_modules/mdbootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="node_modules/mdbootstrap/js/mdb.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Connect to the /powerupdates namespace
var socket = io.connect('http://' + document.domain + ':' + location.port + '/powerupdates');
// Listen for messages from the server in the /powerupdates namespace
socket.on('message_from_server', function (message) {
console.log('Received message from server in /powerupdates namespace:', message);
// Handle the received message as needed
});
});
</script>
<script>
function getUrl(url) {
return fetch(url)
@ -222,7 +234,6 @@ input:checked + .slider:before {
.then(data => {
// Handle the response data
console.log('Response data:', data);
location.reload(); // Refresh the page
})
.catch(error => {
console.error('An error occurred while making GET request:', error);
@ -267,3 +278,4 @@ input:checked + .slider:before {
// Add event listener to the search input
document.getElementById('searchInput').addEventListener('input', search);
</script>

View file

@ -3,6 +3,7 @@ from app.settings import powerbars, groups
from flask import jsonify
import telnetlib
import time
import app
routes = Blueprint('routes', __name__)
@ -111,6 +112,7 @@ def powerbar_control(powerbar, outlet, action):
tn = get_telnet_connection(powerbar)
run_telnet_command(powerbar,f"{action} {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = action
app.socketio.emit('power-event',{'powebar': powerbar,'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
except Exception as E:
print(f"Telnet error: {E}")
@ -119,13 +121,14 @@ def powerbar_control(powerbar, outlet, action):
if action == 'cycle':
tn = get_telnet_connection(powerbar)
print(f"Turning Off powerbar {powerbar} outlet {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "off"
run_telnet_command(powerbar,f"off {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "off"
app.socketio.emit('power-event',{'powebar': powerbar,'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
time.sleep(5)
print(f"Turning On powerbar {powerbar} outlet {outlet}")
run_telnet_command(powerbar,f"on {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
app.socketio.emit('power-event',{'powebar': powerbar,'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
if action == 'toggle':
print(powerbars[powerbar]['outlets'][outlet]['state'])
@ -133,13 +136,14 @@ def powerbar_control(powerbar, outlet, action):
tn = get_telnet_connection(powerbar)
print(f"Turning Off powerbar {powerbar} outlet {outlet}")
run_telnet_command(powerbar,f"off {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "off"
app.socketio.emit('power-event',{'powebar': powerbar,'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
elif powerbars[powerbar]['outlets'][outlet]['state'] == "off":
tn = get_telnet_connection(powerbar)
print(f"Turning On powerbar {powerbar} outlet {outlet}")
run_telnet_command(powerbar,f"on {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
app.socketio.emit('power-event',{'powebar': powerbar,'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})

View file

@ -1,2 +1,5 @@
flask
telnetlib3
telnetlib3
flask-socketio
gunicorn
gevent