diff --git a/ConfigFile.cpp b/ConfigFile.cpp index edde5ab..8bcab04 100644 --- a/ConfigFile.cpp +++ b/ConfigFile.cpp @@ -7,7 +7,7 @@ extern Preferences pref; -#define SHADE_HDR_VER 21 +#define SHADE_HDR_VER 22 #define SHADE_HDR_SIZE 76 #define SHADE_REC_SIZE 276 #define GROUP_REC_SIZE 194 @@ -554,6 +554,9 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename, } } } + else { + this->file.seek(this->file.position() + this->header.repeaterRecordSize, SeekSet); + } if(opts.settings) { // First read out the data. this->readSettingsRecord(); @@ -584,6 +587,7 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename, } bool ShadeConfigFile::readNetRecord() { if(this->header.netRecordSize > 0) { + uint32_t startPos = this->file.position(); Serial.println("Reading network settings from file..."); settings.connType = static_cast(this->readUInt8(static_cast(conn_types::unset))); settings.IP.dhcp = this->readBool(true); @@ -598,9 +602,18 @@ bool ShadeConfigFile::readNetRecord() { settings.IP.dns1.fromString(ip); this->readVarString(ip, sizeof(ip)); settings.IP.dns2.fromString(ip); + if(this->header.version >= 22) { + this->readVarString(settings.MQTT.protocol, sizeof(settings.MQTT.protocol)); + this->readVarString(settings.MQTT.hostname, sizeof(settings.MQTT.hostname)); + settings.MQTT.port = this->readUInt16(1883); + settings.MQTT.pubDisco = this->readBool(false); + this->readVarString(settings.MQTT.rootTopic, sizeof(settings.MQTT.rootTopic)); + this->readVarString(settings.MQTT.discoTopic, sizeof(settings.MQTT.discoTopic)); + } // Now lets check to see if we are the same board. If we are then we will restore // the ethernet phy settings. if(strncmp(settings.serverId, this->header.serverId, sizeof(settings.serverId)) == 0) { + Serial.println("Restoring Ethernet adapter settings"); settings.Ethernet.boardType = this->readUInt8(1); settings.Ethernet.phyType = static_cast(this->readUInt8(0)); settings.Ethernet.CLKMode = static_cast(this->readUInt8(0)); @@ -609,9 +622,8 @@ bool ShadeConfigFile::readNetRecord() { settings.Ethernet.MDCPin = this->readInt8(16); settings.Ethernet.MDIOPin = this->readInt8(23); } - else { - // We are not going to get the network adapter settings. - Serial.println("Skipping Ethernet adapter settings (Chip ids do not match)..."); + if(this->file.position() != startPos + this->header.netRecordSize) { + Serial.println("Reading to end of network record"); this->seekChar(CFG_REC_END); } } @@ -619,6 +631,7 @@ bool ShadeConfigFile::readNetRecord() { } bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) { if(this->header.transRecordSize > 0) { + uint32_t startPos = this->file.position(); Serial.println("Reading Transceiver settings from file..."); cfg.enabled = this->readBool(false); cfg.proto = static_cast(this->readUInt8(0)); @@ -633,6 +646,11 @@ bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) { cfg.rxBandwidth = this->readFloat(cfg.rxBandwidth); cfg.deviation = this->readFloat(cfg.deviation); cfg.txPower = this->readInt8(cfg.txPower); + if(this->file.position() != startPos + this->header.transRecordSize) { + Serial.println("Reading to end of transceiver record"); + this->seekChar(CFG_REC_END); + } + } return true; } @@ -947,6 +965,12 @@ bool ShadeConfigFile::writeNetRecord() { this->writeVarString(settings.IP.subnet.toString().c_str()); this->writeVarString(settings.IP.dns1.toString().c_str()); this->writeVarString(settings.IP.dns2.toString().c_str()); + this->writeVarString(settings.MQTT.protocol); + this->writeVarString(settings.MQTT.hostname); + this->writeUInt16(settings.MQTT.port); + this->writeBool(settings.MQTT.pubDisco); + this->writeVarString(settings.MQTT.rootTopic); + this->writeVarString(settings.MQTT.discoTopic); this->writeUInt8(settings.Ethernet.boardType); this->writeUInt8(static_cast(settings.Ethernet.phyType)); this->writeUInt8(static_cast(settings.Ethernet.CLKMode)); diff --git a/ConfigSettings.cpp b/ConfigSettings.cpp index 2ca9308..5b452ce 100644 --- a/ConfigSettings.cpp +++ b/ConfigSettings.cpp @@ -267,6 +267,12 @@ uint16_t ConfigSettings::calcNetRecSize() { + this->IP.subnet.toString().length() + 3 + this->IP.dns1.toString().length() + 3 + this->IP.dns2.toString().length() + 3 + + strlen(this->MQTT.protocol) + 3 + + strlen(this->MQTT.hostname) + 3 + + 6 // MQTT Port + + 6 // PubDisco + + strlen(this->MQTT.rootTopic) + 3 + + strlen(this->MQTT.discoTopic) + 3 + 4 // ETH.boardType + 4 // ETH.phyType + 4 // ETH.clkMode diff --git a/Network.cpp b/Network.cpp index 57f025a..4242e57 100644 --- a/Network.cpp +++ b/Network.cpp @@ -71,46 +71,44 @@ void Network::loop() { mqtt.loop(); } void Network::emitSockets() { - if(WiFi.status() == WL_CONNECTED) { - if(abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel) { - char buf[128]; - snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel()); - sockEmit.sendToClients("wifiStrength", buf); - this->lastRSSI = WiFi.RSSI(); - this->lastChannel = WiFi.channel(); - sockEmit.loop(); - } - } - else { - if(this->connType == conn_types::ethernet && this->lastRSSI != -100 && this->lastChannel != -1) { - sockEmit.sendToClients("wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}"); - this->lastRSSI = -100; - this->lastChannel = -1; - sockEmit.loop(); - } + if(this->needsBroadcast || abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel) { + this->emitSockets(255); + sockEmit.loop(); + this->needsBroadcast = false; } } void Network::emitSockets(uint8_t num) { char buf[128]; - if(WiFi.status() == WL_CONNECTED) { - snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel()); - sockEmit.sendToClient(num, "wifiStrength", buf); - this->lastRSSI = WiFi.RSSI(); - this->lastChannel = WiFi.channel(); + if(this->connType == conn_types::ethernet) { + snprintf(buf, sizeof(buf), "{\"connected\":%s,\"speed\":%d,\"fullduplex\":%s}", this->connected() ? "true" : "false", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false"); + if(num == 255) + sockEmit.sendToClients("ethernet", buf); + else + sockEmit.sendToClient(num, "ethernet", buf); } else { - if(this->connType == conn_types::ethernet && this->lastRSSI != -100 && this->lastChannel != -1) - sockEmit.sendToClient(num, "wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}"); - this->lastRSSI = -100; - this->lastChannel = -1; + if(WiFi.status() == WL_CONNECTED) { + snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel()); + if(num == 255) + sockEmit.sendToClients("wifiStrength", buf); + else + sockEmit.sendToClient(num, "wifiStrength", buf); + this->lastRSSI = WiFi.RSSI(); + this->lastChannel = WiFi.channel(); + } + else { + if(num == 255) { + sockEmit.sendToClients("wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}"); + sockEmit.sendToClients("ethernet", "{\"connected\":false,\"speed\":0,\"fullduplex\":false}"); + } + else { + sockEmit.sendToClient(num, "wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}"); + sockEmit.sendToClient(num, "ethernet", "{\"connected\":false,\"speed\":0,\"fullduplex\":false}"); + } + this->lastRSSI = -100; + this->lastChannel = -1; + } } - if(this->connType == conn_types::ethernet) { - snprintf(buf, sizeof(buf), "{\"connected\":true,\"speed\":%d,\"fullduplex\":%s}", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false"); - sockEmit.sendToClient(num, "ethernet", buf); - } - else - sockEmit.sendToClient(num, "ethernet", "{\"connected\":false, \"speed\":0,\"fullduplex\":false}"); - } void Network::setConnected(conn_types connType) { this->connType = connType; diff --git a/Network.h b/Network.h index afc2491..0a4b409 100644 --- a/Network.h +++ b/Network.h @@ -13,6 +13,7 @@ class Network { public: bool wifiFallback = false; bool softAPOpened = false; + bool needsBroadcast = true; conn_types connType = conn_types::unset; bool connected(); String ssid; diff --git a/Sockets.cpp b/Sockets.cpp index 7506223..5429acd 100644 --- a/Sockets.cpp +++ b/Sockets.cpp @@ -190,10 +190,12 @@ void SocketEmitter::wsEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t Serial.printf("Socket [%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); // Send all the current shade settings to the client. sockServer.sendTXT(num, "Connected"); + sockServer.loop(); settings.emitSockets(num); somfy.emitState(num); - net.emitSockets(num); git.emitUpdateCheck(num); + net.emitSockets(num); + sockServer.loop(); } break; case WStype_TEXT: diff --git a/SomfyController.ino.esp32.bin b/SomfyController.ino.esp32.bin index f9bc696..ca229da 100644 Binary files a/SomfyController.ino.esp32.bin and b/SomfyController.ino.esp32.bin differ diff --git a/SomfyController.ino.esp32s3.bin b/SomfyController.ino.esp32s3.bin index 002ffec..8dd001b 100644 Binary files a/SomfyController.ino.esp32s3.bin and b/SomfyController.ino.esp32s3.bin differ diff --git a/SomfyController.littlefs.bin b/SomfyController.littlefs.bin index 691e314..3fd9d09 100644 Binary files a/SomfyController.littlefs.bin and b/SomfyController.littlefs.bin differ diff --git a/Web.cpp b/Web.cpp index 32be4a3..71b08f6 100644 --- a/Web.cpp +++ b/Web.cpp @@ -818,6 +818,16 @@ void Web::handleDiscovery(WebServer &server) { snprintf(g_content, sizeof(g_content), "{\"serverId\":\"%s\",\"version\":\"%s\",\"latest\":\"%s\",\"model\":\"%s\",\"hostname\":\"%s\",\"authType\":%d,\"permissions\":%d,\"chipModel\":\"%s\",\"connType\":\"%s\",\"checkForUpdate\":%s", settings.serverId, settings.fwVersion.name, git.latest.name, "ESPSomfyRTS", settings.hostname, static_cast(settings.Security.type), settings.Security.permissions, settings.chipModel, connType, settings.checkForUpdate ? "true" : "false"); server.send_P(200, _encoding_json, g_content); + /* + if(net.connType == conn_types::ethernet) { + snprintf(g_content, sizeof(g_content), ",\"ethernet\":{\"connected\":true,\"speed\":%d,\"fullduplex\":%s}", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false"); + server.sendContent(g_content); + } + else { + snprintf(g_content, sizeof(g_content), ",\"wifi\":{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel()); + server.sendContent(g_content); + } + */ server.sendContent(",\"rooms\":"); this->chunkRoomsResponse(server); server.sendContent(",\"shades\":"); @@ -826,6 +836,7 @@ void Web::handleDiscovery(WebServer &server) { this->chunkGroupsResponse(server); server.sendContent("}"); server.sendContent("", 0); + net.needsBroadcast = true; } else server.send(500, _encoding_text, "Invalid http method"); diff --git a/data/index.html b/data/index.html index 9f6c202..cdba8f7 100644 --- a/data/index.html +++ b/data/index.html @@ -3,11 +3,11 @@ - - - + + + - +
diff --git a/data/index.js b/data/index.js index 3b15918..a8cf5bb 100644 --- a/data/index.js +++ b/data/index.js @@ -571,7 +571,7 @@ async function initSockets() { }; socket.onclose = (evt) => { wifi.procWifiStrength({ ssid: '', channel: -1, strength: -100 }); - wifi.procEthernet({ connected: '', speed: 0, fullduplex: false }); + wifi.procEthernet({ connected: false, speed: 0, fullduplex: false }); if (document.getElementsByClassName('socket-wait').length === 0) ui.waitMessage(document.getElementById('divContainer')).classList.add('socket-wait'); if (evt.wasClean) { diff --git a/data/login.html b/data/login.html index 3f9b68c..409f0d3 100644 --- a/data/login.html +++ b/data/login.html @@ -3,11 +3,11 @@ - - - + + + - +