diff --git a/app/templates/index.html b/app/templates/index.html index 3076d42..94e83d8 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -152,11 +152,11 @@ input:checked + .slider:before {
@@ -217,15 +217,18 @@ input:checked + .slider:before { console.log('Received message from server in /powerupdates namespace:', message); checkboxid = message.powerbar + "-" + message.outlet; badgeid = message.powerbar + "-" + message.outlet + "-badge"; + sliderid = message.powerbar + "-" + message.outlet + "-slider"; var checkbox = document.getElementById(checkboxid); var badge = document.getElementById(badgeid); + var slider = document.getElementById(sliderid); if (message.action == "on") { checkbox.setAttribute('checked',''); badge.textContent = "On"; badge.classList.replace("badge-danger","badge-success"); } else { + console.log(checkbox) checkbox.removeAttribute('checked'); badge.textContent = "Off"; badge.classList.replace("badge-success","badge-danger"); @@ -246,17 +249,23 @@ input:checked + .slider:before { console.error('Failed to make GET request'); } }) - .then(data => { - // Handle the response data - console.log('Response data:', data); - }) .catch(error => { console.error('An error occurred while making GET request:', error); }); } - function toggleOutlet(on_url, off_url, checked) { + function toggleOutlet(id, on_url, off_url, checked) { const url = checked ? on_url : off_url; + + checkboxid = id; + var checkbox = document.getElementById(checkboxid); + + if (checked) { + checkbox.setAttribute('checked',''); + } else { + checkbox.removeAttribute('checked'); + } + getUrl(url) .then(data => { // Handle the response data diff --git a/app/views.py b/app/views.py index 76c6f58..75556b6 100644 --- a/app/views.py +++ b/app/views.py @@ -119,7 +119,6 @@ def powerbar_control(powerbar, outlet, action): command = run_telnet_command(powerbar,f"on {outlet}") if command: powerbars[powerbar]['outlets'][outlet]['state'] = "on" - app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') if action == 'toggle': print(powerbars[powerbar]['outlets'][outlet]['state']) @@ -130,7 +129,6 @@ def powerbar_control(powerbar, outlet, action): command = run_telnet_command(powerbar,f"off {outlet}") if command: powerbars[powerbar]['outlets'][outlet]['state'] = "off" - app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') elif powerbars[powerbar]['outlets'][outlet]['state'] == "off": @@ -139,10 +137,9 @@ def powerbar_control(powerbar, outlet, action): command = run_telnet_command(powerbar,f"on {outlet}") if command: powerbars[powerbar]['outlets'][outlet]['state'] = "on" - app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') - - return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) + state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown') + app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : state}, namespace='/powerupdates') return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) @@ -223,15 +220,17 @@ def powebar_group_action(group, action): print(f"Turning {action} powerbar {powerbar} outlet {outlets}") if powerbars[powerbar]['method'] == "telnet": - run_telnet_command(powerbar,f"{action} {outlets}") - for outlet in device['outlets']: - if outlet in powerbars[powerbar]['outlets']: - powerbars[powerbar]['outlets'][outlet]['state'] = action - app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') - print(f"Turned {action} powerbar {powerbar} outlet {outlet}") - - return jsonify({'status': "200"}) + command = run_telnet_command(powerbar,f"{action} {outlets}") + if command: + for outlet in device['outlets']: + if outlet in powerbars[powerbar]['outlets']: + powerbars[powerbar]['outlets'][outlet]['state'] = action + app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') + print(f"Turned {action} powerbar {powerbar} outlet {outlet}") + return jsonify({'status': "200"}) + + return jsonify({'status': "500"}) except Exception as E: print(f"Endpoint Exception Error: {E}") return jsonify({'error': 'Endpoint Exception Error'}), 500