I'd use AJAX firing a CGI request (POST) for the controls from the buttons, so that you never re-direct away from the home page.
Code: Select all
<head>
<link rel="stylesheet" href="styles/XSwitch.css" />
<script src="lib/x-tag-components.js"></script>
<script src="lib/jquery-3.3.1.min.js"></script>
<link rel="import" href="classes/XSwitch.html" />
</head>
<body>
<table>
<tr><td>GPIO#01</td><td style="align:right;"><acidjs-xswitch id="GPIOAS1"></acidjs-xswitch></td></tr>
</table>
<script>
$.ajax({
'url' : 'http://pi-home-control.local/cgi-bin/getCurrentStatus.cgi',
'type' : 'GET',
'data' : { 'q': 'str' },
success: function(data) {
var obj = $.parseJSON(data);
var newHTML = '<script> $(document).ready(function() { ';
$.each(obj, function(key, value) {
if (value.status == 'on') {
newHTML += ' $(\''+value.topic+'\').attr({mode:\"'+value.status+'\"});'}
});
newHTML += ' }); <\/script>';
$('body').append(newHTML);
},
error: function(request, error) {
}
});
</script>
<script>
$("#GPIOAS1").on("acidjs-xswitch-mode-change", function(e, data) {
$.post("http://pi-home-control.local/cgi-bin/gpioactivate.cgi", { switch: "GPIOAS1:"+data.selected})
});
</script>
</body>
Using
http://experiments.wemakesites.net/x-sw ... onent.html to drive the switches and make them look pretty.
The first CGI program reads current status to set the switch based on last time it was activated or deactivated. The second CGI program sets current status depending on user action on the HTML page.