Browse Source

Merge branch 'remote-webui' into webui

rfm69
Xose Pérez 6 years ago
parent
commit
8e41cd3ad0
5 changed files with 4259 additions and 1 deletions
  1. +9
    -0
      code/espurna/config/general.h
  2. +4208
    -0
      code/espurna/static/index.html.gz.h
  3. +19
    -0
      code/espurna/web.ino
  4. +10
    -0
      code/espurna/ws.ino
  5. +13
    -1
      code/html/custom.js

+ 9
- 0
code/espurna/config/general.h View File

@ -377,6 +377,15 @@
#define WEB_PORT 80 // HTTP port
#endif
// Defining a WEB_REMOTE_DOMAIN will enable Cross-Origin Resource Sharing (CORS)
// so you will be able to login to this device from another domain. This will allow
// you to manage all ESPurna devices in your local network from a unique installation
// of the web UI. This installation could be in a local server (a Raspberry Pi, for instance)
// or in the Internet. Since the WebUI is just one compressed file with HTML, CSS and JS
// there are no special requirements. Any static web server will do (NGinx, Apache, Lighttpd,...).
// The only requirement is that the resource must be available under this domain.
#define WEB_REMOTE_DOMAIN "http://tinkerman.cat"
// -----------------------------------------------------------------------------
// WEBSOCKETS
// -----------------------------------------------------------------------------


+ 4208
- 0
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 19
- 0
code/espurna/web.ino View File

@ -40,6 +40,24 @@ void _onReset(AsyncWebServerRequest *request) {
request->send(200);
}
void _onDiscover(AsyncWebServerRequest *request) {
webLog(request);
AsyncResponseStream *response = request->beginResponseStream("text/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["app"] = APP_NAME;
root["version"] = APP_VERSION;
root["hostname"] = getSetting("hostname");
root["device"] = getBoardName();
root.printTo(*response);
request->send(response);
}
void _onGetConfig(AsyncWebServerRequest *request) {
webLog(request);
@ -338,6 +356,7 @@ void webSetup() {
_server->on("/config", HTTP_GET, _onGetConfig);
_server->on("/config", HTTP_POST | HTTP_PUT, _onPostConfig, _onPostConfigData);
_server->on("/upgrade", HTTP_POST, _onUpgrade, _onUpgradeData);
_server->on("/discover", HTTP_GET, _onDiscover);
// Serve static files
#if SPIFFS_SUPPORT


+ 10
- 0
code/espurna/ws.ino View File

@ -489,12 +489,22 @@ void wsReload() {
}
void wsSetup() {
_ws.onEvent(_wsEvent);
webServer()->addHandler(&_ws);
// CORS
#ifdef WEB_REMOTE_DOMAIN
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", WEB_REMOTE_DOMAIN);
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
#endif
webServer()->on("/auth", HTTP_GET, _onAuth);
#if MQTT_SUPPORT
mqttRegister(_wsMQTTCallback);
#endif
wsOnSendRegister(_wsOnStart);
wsOnReceiveRegister(_wsOnReceive);
espurnaRegisterLoop(_wsLoop);


+ 13
- 1
code/html/custom.js View File

@ -1432,6 +1432,7 @@ function connectToURL(url) {
$.ajax({
'method': 'GET',
'crossDomain': true,
'url': urls.auth.href,
'xhrFields': { 'withCredentials': true }
}).done(function(data) {
@ -1460,6 +1461,11 @@ function connectToCurrentURL() {
connectToURL(new URL(window.location));
}
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
$(function() {
initMessages();
@ -1508,6 +1514,12 @@ $(function() {
// don't autoconnect when opening from filesystem
if (window.location.protocol === "file:") { return; }
connectToCurrentURL();
// Check host param in query string
if (host = getParameterByName('host')) {
connect(host);
} else {
connectToCurrentURL();
}
});

Loading…
Cancel
Save