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

This commit is contained in:
Matthew Frost 2024-02-02 13:59:17 +01:00
parent b52e9e8cb7
commit bcb97819f1
2 changed files with 29 additions and 21 deletions

View file

@ -152,11 +152,11 @@ input:checked + .slider:before {
<div class="col-md-2">
<label class="switch">
{% if outlet[1].state == "on" %}
<input type="checkbox" class="default" id="{{powerbar[0]}}-{{outlet[0]}}" onchange="toggleOutlet('/{{powerbar[0]}}/{{outlet[0]}}/on', '/{{powerbar[0]}}/{{outlet[0]}}/off', this.checked)" checked>
<input type="checkbox" class="default" id="{{powerbar[0]}}-{{outlet[0]}}" onchange="toggleOutlet('{{powerbar[0]}}-{{outlet[0]}}', '/{{powerbar[0]}}/{{outlet[0]}}/on', '/{{powerbar[0]}}/{{outlet[0]}}/off', this.checked)" checked>
{% else %}
<input type="checkbox" class="default" id="{{powerbar[0]}}-{{outlet[0]}}" onchange="toggleOutlet('/{{powerbar[0]}}/{{outlet[0]}}/on', '/{{powerbar[0]}}/{{outlet[0]}}/off', this.checked)">
<input type="checkbox" class="default" id="{{powerbar[0]}}-{{outlet[0]}}" onchange="toggleOutlet('{{powerbar[0]}}-{{outlet[0]}}', '/{{powerbar[0]}}/{{outlet[0]}}/on', '/{{powerbar[0]}}/{{outlet[0]}}/off', this.checked)">
{% endif %}
<span class="slider"></span>
<span id="{{powerbar[0]}}-{{outlet[0]}}-slider" class="slider"></span>
</label>
</div>
<div class="col-md-2">
@ -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

View file

@ -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}")
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': "200"})
return jsonify({'status': "500"})
except Exception as E:
print(f"Endpoint Exception Error: {E}")
return jsonify({'error': 'Endpoint Exception Error'}), 500