From 696fed74cfb4b56ffb1bd87347857d503d67144c Mon Sep 17 00:00:00 2001 From: Matthew Frost <m.frost@mattronix.nl> Date: Sat, 2 Dec 2023 19:00:33 +0100 Subject: [PATCH] websocket --- app/templates/index.html | 22 ++++++++++++++++++---- app/views.py | 10 +++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/templates/index.html b/app/templates/index.html index f721cbc..2e56bbb 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -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"); + } }); }); diff --git a/app/views.py b/app/views.py index 9f38c90..a1839b1 100644 --- a/app/views.py +++ b/app/views.py @@ -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')})