diff --git a/code/html/custom.js b/code/html/custom.js index 248f226a..bf859869 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -1,40 +1,28 @@ var websock; function doUpdate() { - var self = $(this); - self.addClass("loading"); - $.ajax({ - 'method': 'POST', - 'url': '/save', - 'dataType': 'json', - 'data': $("#formSave").serializeArray() - }).done(function(data) { - self.removeClass("loading"); - }).fail(function() { - self.removeClass("loading"); - }); + var data = $("#formSave").serializeArray(); + websock.send(JSON.stringify({'config': data})); + return false; } function doReset() { var response = window.confirm("Are you sure you want to reset the device?"); - if (response == false) return; - var self = $(this); - self.addClass("loading"); - $.ajax({ - 'method': 'GET', - 'url': '/reset' - }); + if (response == false) return false; + websock.send(JSON.stringify({'action': 'reset'})); + return false; } function doReconnect() { var response = window.confirm("Are you sure you want to disconnect from the current WIFI network?"); - if (response == false) return; - var self = $(this); - self.addClass("loading"); - $.ajax({ - 'method': 'GET', - 'url': '/reconnect' - }); + if (response == false) return false; + websock.send(JSON.stringify({'action': 'reconnect'})); + return false; +} + +function doToggle(element, value) { + websock.send(JSON.stringify({'action': value ? 'on' : 'off'})); + return false; } function showPanel() { @@ -66,13 +54,7 @@ function processData(data) { .iphoneStyle({ checkedLabel: 'ON', uncheckedLabel: 'OFF', - onChange: function(elem, value) { - $.ajax({ - 'method': 'GET', - 'url': value ? '/relay/on' : '/relay/off', - 'dataType': 'json' - }); - } + onChange: doToggle }) .iphoneStyle("refresh"); } @@ -94,7 +76,8 @@ function processData(data) { var element = $("input[name=" + key + "]"); if (element.length > 0) { if (element.attr('type') == 'checkbox') { - element.prop("checked", data[key] == 1) + element + .prop("checked", data[key] == 1) .iphoneStyle({ resizeContainer: false, resizeHandle: false, @@ -144,6 +127,7 @@ function init() { $(".pure-menu-link").on('click', showPanel); var host = window.location.hostname; + //host = "studiolamp.local"; websock = new WebSocket('ws://' + host + ':81/'); websock.onopen = function(evt) {}; websock.onclose = function(evt) {}; diff --git a/code/src/webserver.ino b/code/src/webserver.ino index 7b5b93c3..3374e958 100644 --- a/code/src/webserver.ino +++ b/code/src/webserver.ino @@ -34,28 +34,6 @@ String getContentType(String filename) { return "text/plain"; } -void handleReconnect() { - DEBUG_MSG("[WEBSERVER] Request: /reconnect\n"); - wifiDisconnect(); -} - -void handleReset() { - DEBUG_MSG("[WEBSERVER] Request: /reset\n"); - ESP.reset(); -} - -void handleRelayOn() { - DEBUG_MSG("[WEBSERVER] Request: /relay/on\n"); - switchRelayOn(); - server.send(200, "text/plain", "ON"); -} - -void handleRelayOff() { - DEBUG_MSG("[WEBSERVER] Request: /relay/off\n"); - switchRelayOff(); - server.send(200, "text/plain", "OFF"); -} - bool handleFileRead(String path) { DEBUG_MSG("[WEBSERVER] Request: %s\n", (char *) path.c_str()); @@ -77,70 +55,8 @@ bool handleFileRead(String path) { } -void handleSave() { - - DEBUG_MSG("[WEBSERVER] Request: /save\n"); - - bool dirty = false; - bool dirtyMQTT = false; - unsigned int network = 0; - - for (unsigned int i=0; i()) { + + JsonArray& config = root["config"]; + DEBUG_MSG("[WEBSOCKET] Parsing configuration data\n"); + + bool dirty = false; + bool dirtyMQTT = false; + unsigned int network = 0; + + for (unsigned int i=0; i