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

This commit is contained in:
Matthew Frost 2023-12-02 19:00:33 +01:00
parent 0f4343acd2
commit 696fed74cf
2 changed files with 23 additions and 9 deletions

View file

@ -144,9 +144,9 @@ input:checked + .slider:before {
<div class="row">
<div class="col-md-8">{{outlet[1].name}}<br><span class="badge badge-secondary">Outlet: {{outlet[0]}}</span>
{% if outlet[1].state == "on" %}
<span class="badge badge-success">On</span>
<span id="{{powerbar[0]}}-{{outlet[0]}}-badge" class="badge badge-success">On</span>
{% else %}
<span class="badge badge-danger">Off</span>
<span id="{{powerbar[0]}}-{{outlet[0]}}-badge" class="badge badge-danger">Off</span>
{% endif %}</div>
<div class="col-md-2">
<label class="switch">
@ -212,9 +212,23 @@ input:checked + .slider:before {
// Listen for messages from the server in the /powerupdates namespace
socket.on('message_from_server', function (message) {
socket.on('power-event', function (message) {
console.log('Received message from server in /powerupdates namespace:', message);
// Handle the received message as needed
checkboxid = message.powerbar + "-" + message.outlet;
badgeid = message.powerbar + "-" + message.outlet + "-badge";
var checkbox = document.getElementById(checkboxid);
var badge = document.getElementById(badgeid);
if (message.action == "on") {
checkbox.setAttribute('checked', 'checked');
badge.textContent = "On";
badge.classList.replace("badge-danger","badge-success");
} else {
checkbox.removeAttribute('checked');
badge.textContent = "Off";
badge.classList.replace("badge-success","badge-danger");
}
});
});

View file

@ -112,7 +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')
app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
except Exception as E:
print(f"Telnet error: {E}")
@ -123,12 +123,12 @@ def powerbar_control(powerbar, outlet, action):
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')
app.socketio.emit('power-event',{'powerbar': 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')
app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
if action == 'toggle':
print(powerbars[powerbar]['outlets'][outlet]['state'])
@ -137,13 +137,13 @@ def powerbar_control(powerbar, outlet, action):
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')
app.socketio.emit('power-event',{'powerbar': 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')
app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})