Browse Source

Encapsulate getHostname, getPassword, webMode,...

v2
Xose Pérez 6 years ago
parent
commit
6635b70aa7
26 changed files with 11770 additions and 11749 deletions
  1. +1
    -1
      code/espurna/alexa.ino
  2. +5
    -0
      code/espurna/button.ino
  3. BIN
      code/espurna/data/index.all.html.gz
  4. BIN
      code/espurna/data/index.light.html.gz
  5. BIN
      code/espurna/data/index.rfbridge.html.gz
  6. BIN
      code/espurna/data/index.sensor.html.gz
  7. BIN
      code/espurna/data/index.small.html.gz
  8. +1
    -1
      code/espurna/debug.ino
  9. +1
    -4
      code/espurna/espurna.ino
  10. +4
    -4
      code/espurna/homeassistant.ino
  11. +1
    -1
      code/espurna/influxdb.ino
  12. +1
    -1
      code/espurna/llmnr.ino
  13. +1
    -1
      code/espurna/mdns.ino
  14. +2
    -2
      code/espurna/mqtt.ino
  15. +1
    -1
      code/espurna/netbios.ino
  16. +2
    -2
      code/espurna/ota.ino
  17. +2
    -2
      code/espurna/ssdp.ino
  18. +2665
    -2665
      code/espurna/static/index.all.html.gz.h
  19. +2537
    -2537
      code/espurna/static/index.light.html.gz.h
  20. +2165
    -2166
      code/espurna/static/index.rfbridge.html.gz.h
  21. +2205
    -2205
      code/espurna/static/index.sensor.html.gz.h
  22. +2121
    -2122
      code/espurna/static/index.small.html.gz.h
  23. +15
    -6
      code/espurna/utils.ino
  24. +28
    -7
      code/espurna/web.ino
  25. +5
    -5
      code/espurna/wifi.ino
  26. +7
    -16
      code/espurna/ws.ino

+ 1
- 1
code/espurna/alexa.ino View File

@ -62,7 +62,7 @@ void alexaSetup() {
#endif
unsigned int relays = relayCount();
String hostname = getSetting("hostname");
String hostname = getHostname();
if (relays == 1) {
alexa.addDevice(hostname.c_str());
} else {


+ 5
- 0
code/espurna/button.ino View File

@ -40,6 +40,10 @@ bool _buttonWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "btn", 3) == 0);
}
void _buttonWebSocketOnSend(JsonObject& root) {
root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
}
#endif
int buttonFromRelay(unsigned int relayID) {
@ -204,6 +208,7 @@ void buttonSetup() {
// Websocket Callbacks
#if WEB_SUPPORT
wsOnSendRegister(_buttonWebSocketOnSend);
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif


BIN
code/espurna/data/index.all.html.gz View File


BIN
code/espurna/data/index.light.html.gz View File


BIN
code/espurna/data/index.rfbridge.html.gz View File


BIN
code/espurna/data/index.sensor.html.gz View File


BIN
code/espurna/data/index.small.html.gz View File


+ 1
- 1
code/espurna/debug.ino View File

@ -148,7 +148,7 @@ void debugWebSetup() {
#if DEBUG_UDP_SUPPORT
#if DEBUG_UDP_PORT == 514
snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getHostname().c_str());
#endif
#endif


+ 1
- 4
code/espurna/espurna.ino View File

@ -58,10 +58,7 @@ void setup() {
// Init persistance and terminal features
settingsSetup();
// Hostname & board name initialization
if (getSetting("hostname").length() == 0) {
setDefaultHostname();
}
// Board name initialization
setBoardName();
// Show welcome message and system configuration


+ 4
- 4
code/espurna/homeassistant.ino View File

@ -24,7 +24,7 @@ bool _haSendFlag = false;
void _haSendMagnitude(unsigned char i, JsonObject& config) {
unsigned char type = magnitudeType(i);
config["name"] = getSetting("hostname") + String(" ") + magnitudeTopic(type);
config["name"] = getHostname() + String(" ") + magnitudeTopic(type);
config.set("platform", "mqtt");
config["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false);
config["unit_of_measurement"] = magnitudeUnits(type);
@ -37,7 +37,7 @@ void _haSendMagnitudes() {
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
"/sensor/" +
getSetting("hostname") + "_" + String(i) +
getHostname() + "_" + String(i) +
"/config";
String output;
@ -64,7 +64,7 @@ void _haSendMagnitudes() {
void _haSendSwitch(unsigned char i, JsonObject& config) {
String name = getSetting("hostname");
String name = getHostname();
if (relayCount() > 1) {
name += String(" #") + String(i);
}
@ -117,7 +117,7 @@ void _haSendSwitches() {
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
"/" + type +
"/" + getSetting("hostname") + "_" + String(i) +
"/" + getHostname() + "_" + String(i) +
"/config";
String output;


+ 1
- 1
code/espurna/influxdb.ino View File

@ -71,7 +71,7 @@ bool idbSend(const char * topic, const char * payload) {
if (_idb_client.connect((const char *) host, port)) {
char data[128];
snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getHostname().c_str(), String(payload).c_str());
DEBUG_MSG("[INFLUXDB] Data: %s\n", data);
char request[256];


+ 1
- 1
code/espurna/llmnr.ino View File

@ -11,7 +11,7 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
#include <ESP8266LLMNR.h>
void llmnrSetup() {
LLMNR.begin(getSetting("hostname").c_str());
LLMNR.begin(getHostname().c_str());
DEBUG_MSG_P(PSTR("[LLMNR] Configured\n"));
}


+ 1
- 1
code/espurna/mdns.ino View File

@ -28,7 +28,7 @@ void _mdnsFindMQTT() {
#endif
void _mdnsServerStart() {
if (MDNS.begin((char *) getSetting("hostname").c_str())) {
if (MDNS.begin((char *) getHostname().c_str())) {
DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
} else {
DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));


+ 2
- 2
code/espurna/mqtt.ino View File

@ -220,7 +220,7 @@ void _mqttConfigure() {
if (_mqtt_topic.endsWith("/")) _mqtt_topic.remove(_mqtt_topic.length()-1);
// Placeholders
_mqtt_topic.replace("{hostname}", getSetting("hostname"));
_mqtt_topic.replace("{hostname}", getHostname());
_mqtt_topic.replace("{magnitude}", "#");
if (_mqtt_topic.indexOf("#") == -1) _mqtt_topic = _mqtt_topic + "/#";
String mac = WiFi.macAddress();
@ -608,7 +608,7 @@ void mqttFlush() {
root[MQTT_TOPIC_MAC] = WiFi.macAddress();
#endif
#if MQTT_ENQUEUE_HOSTNAME
root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname");
root[MQTT_TOPIC_HOSTNAME] = getHostname();
#endif
#if MQTT_ENQUEUE_IP
root[MQTT_TOPIC_IP] = getIP();


+ 1
- 1
code/espurna/netbios.ino View File

@ -12,7 +12,7 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
void netbiosSetup() {
static WiFiEventHandler _netbios_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
NBNS.begin(getSetting("hostname").c_str());
NBNS.begin(getHostname().c_str());
DEBUG_MSG_P(PSTR("[NETBIOS] Configured\n"));
});
}


+ 2
- 2
code/espurna/ota.ino View File

@ -16,9 +16,9 @@ Module key prefix: ota
void _otaConfigure() {
ArduinoOTA.setPort(OTA_PORT);
ArduinoOTA.setHostname(getSetting("hostname").c_str());
ArduinoOTA.setHostname(getHostname().c_str());
#if USE_PASSWORD
ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
ArduinoOTA.setPassword(getPassword().c_str());
#endif
}


+ 2
- 2
code/espurna/ssdp.ino View File

@ -51,7 +51,7 @@ void ssdpSetup() {
WiFi.localIP().toString().c_str(), // ip
webPort(), // port
SSDP_DEVICE_TYPE, // device type
getSetting("hostname").c_str(), // friendlyName
getHostname().c_str(), // friendlyName
chipId, // serialNumber
APP_NAME, // modelName
APP_VERSION, // modelNumber
@ -68,7 +68,7 @@ void ssdpSetup() {
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(webPort());
SSDP.setDeviceType(SSDP_DEVICE_TYPE); //https://github.com/esp8266/Arduino/issues/2283
SSDP.setName(getSetting("hostname"));
SSDP.setName(getHostname());
SSDP.setSerialNumber(String(ESP.getChipId()));
SSDP.setModelName(APP_NAME);
SSDP.setModelNumber(APP_VERSION);


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


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


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


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


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


+ 15
- 6
code/espurna/utils.ino View File

@ -17,12 +17,21 @@ String getIdentifier() {
return String(buffer);
}
void setDefaultHostname() {
if (strlen(HOSTNAME) > 0) {
setSetting("hostname", HOSTNAME);
} else {
setSetting("hostname", getIdentifier());
String getHostname() {
String hostname = getSetting("hostname");
if (hostname.length() == 0) {
if (strlen(HOSTNAME) > 0) {
setSetting("hostname", HOSTNAME);
} else {
setSetting("hostname", getIdentifier());
}
hostname = getSetting("hostname");
}
return hostname;
}
String getPassword() {
return getSetting("adminPass", ADMIN_PASS);
}
void setBoardName() {
@ -166,7 +175,7 @@ void heartbeat() {
mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str());
#endif
#if (HEARTBEAT_REPORT_HOSTNAME)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
mqttSend(MQTT_TOPIC_HOSTNAME, getHostname().c_str());
#endif
#if (HEARTBEAT_REPORT_IP)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());


+ 28
- 7
code/espurna/web.ino View File

@ -101,7 +101,7 @@ void _onDiscover(AsyncWebServerRequest *request) {
JsonObject &root = jsonBuffer.createObject();
root["app"] = APP_NAME;
root["version"] = APP_VERSION;
root["hostname"] = getSetting("hostname");
root["hostname"] = getHostname();
root["device"] = getBoardName();
root.printTo(*response);
@ -113,13 +113,13 @@ void _onGetConfig(AsyncWebServerRequest *request) {
webLog(request);
if (!webAuthenticate(request)) {
return request->requestAuthentication(getSetting("hostname").c_str());
return request->requestAuthentication(getHostname().c_str());
}
AsyncResponseStream *response = request->beginResponseStream("text/json");
char buffer[100];
snprintf_P(buffer, sizeof(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str());
snprintf_P(buffer, sizeof(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getHostname().c_str());
response->addHeader("Content-Disposition", buffer);
response->addHeader("X-XSS-Protection", "1; mode=block");
response->addHeader("X-Content-Type-Options", "nosniff");
@ -146,7 +146,7 @@ void _onGetConfig(AsyncWebServerRequest *request) {
void _onPostConfig(AsyncWebServerRequest *request) {
webLog(request);
if (!webAuthenticate(request)) {
return request->requestAuthentication(getSetting("hostname").c_str());
return request->requestAuthentication(getHostname().c_str());
}
request->send(_webConfigSuccess ? 200 : 400);
}
@ -196,7 +196,7 @@ void _onHome(AsyncWebServerRequest *request) {
webLog(request);
if (!webAuthenticate(request)) {
return request->requestAuthentication(getSetting("hostname").c_str());
return request->requestAuthentication(getHostname().c_str());
}
if (request->header("If-Modified-Since").equals(_last_modified)) {
@ -301,7 +301,7 @@ void _onUpgrade(AsyncWebServerRequest *request) {
webLog(request);
if (!webAuthenticate(request)) {
return request->requestAuthentication(getSetting("hostname").c_str());
return request->requestAuthentication(getHostname().c_str());
}
char buffer[10];
@ -363,11 +363,20 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde
}
}
void _webWebSocketOnSend(JsonObject& root) {
root["webPort"] = webPort();
}
bool _webWebSocketOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "web", 3) == 0) return true;
return false;
}
// -----------------------------------------------------------------------------
bool webAuthenticate(AsyncWebServerRequest *request) {
#if USE_PASSWORD
String password = getSetting("adminPass", ADMIN_PASS);
String password = getPassword();
char httpPassword[password.length() + 1];
password.toCharArray(httpPassword, password.length() + 1);
return request->authenticate(WEB_USERNAME, httpPassword);
@ -390,6 +399,14 @@ unsigned int webPort() {
#endif
}
unsigned char webMode() {
#if USE_PASSWORD && WEB_FORCE_PASS_CHANGE
return getPassword().equals(ADMIN_PASS) ? WEB_MODE_PASSWORD : WEB_MODE_NORMAL;
#else
return WEB_MODE_NORMAL;
#endif
}
void webLog(AsyncWebServerRequest *request) {
DEBUG_MSG_P(PSTR("[WEBSERVER] Request: %s %s\n"), request->methodToString(), request->url().c_str());
}
@ -440,6 +457,10 @@ void webSetup() {
#endif
DEBUG_MSG_P(PSTR("[WEBSERVER] Webserver running on port %u\n"), port);
// Websocket Callbacks
wsOnSendRegister(_webWebSocketOnSend);
wsOnReceiveRegister(_webWebSocketOnReceive);
}
#endif // WEB_SUPPORT

+ 5
- 5
code/espurna/wifi.ino View File

@ -34,11 +34,11 @@ void _wifiCheckAP() {
void _wifiConfigure() {
jw.setHostname(getSetting("hostname").c_str());
jw.setHostname(getHostname().c_str());
#if USE_PASSWORD
jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
jw.setSoftAP(getHostname().c_str(), getPassword().c_str());
#else
jw.setSoftAP(getSetting("hostname").c_str());
jw.setSoftAP(getHostname().c_str());
#endif
jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
wifiReconnectCheck();
@ -488,8 +488,8 @@ void wifiDebug(WiFiMode_t modes) {
if (((modes & WIFI_AP) > 0) && ((WiFi.getMode() & WIFI_AP) > 0)) {
DEBUG_MSG_P(PSTR("[WIFI] -------------------------------------- MODE AP\n"));
DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getSetting("hostname").c_str());
DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getHostname().c_str());
DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getPassword().c_str());
DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
footer = true;


+ 7
- 16
code/espurna/ws.ino View File

@ -4,6 +4,8 @@ WEBSOCKET MODULE
Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
Module key prefix: ws
*/
#if WEB_SUPPORT
@ -296,24 +298,16 @@ bool _wsOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "ws", 2) == 0) return true;
if (strncmp(key, "admin", 5) == 0) return true;
if (strncmp(key, "hostname", 8) == 0) return true;
if (strncmp(key, "webPort", 7) == 0) return true;
return false;
}
void _wsOnStart(JsonObject& root) {
#if USE_PASSWORD && WEB_FORCE_PASS_CHANGE
String adminPass = getSetting("adminPass", ADMIN_PASS);
bool changePassword = adminPass.equals(ADMIN_PASS);
#else
bool changePassword = false;
#endif
if (changePassword) {
unsigned char webMode = webMode();
root["webMode"] = WEB_MODE_PASSWORD;
root["webMode"] = webMode;
} else {
if (WEB_MODE_NORMAL == webMode) {
char chipid[7];
snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId());
@ -324,8 +318,6 @@ void _wsOnStart(JsonObject& root) {
bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]
);
root["webMode"] = WEB_MODE_NORMAL;
root["app_name"] = APP_NAME;
root["app_version"] = APP_VERSION;
root["app_build"] = buildTime();
@ -336,7 +328,7 @@ void _wsOnStart(JsonObject& root) {
root["bssid"] = String(bssid_str);
root["channel"] = WiFi.channel();
root["device"] = DEVICE;
root["hostname"] = getSetting("hostname");
root["hostname"] = getHostname();
root["network"] = getNetwork();
root["deviceip"] = getIP();
root["sketch_size"] = ESP.getSketchSize();
@ -346,9 +338,8 @@ void _wsOnStart(JsonObject& root) {
_wsUpdate(root);
root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
root["webPort"] = getSetting("webPort", WEB_PORT).toInt();
root["wsAuth"] = getSetting("wsAuth", WS_AUTHENTICATION).toInt() == 1;
#if TERMINAL_SUPPORT
root["cmdVisible"] = 1;
#endif


Loading…
Cancel
Save