Skip to content

Commit

Permalink
Add MQTT settings to backup/restore #284
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrouse committed Feb 25, 2024
1 parent 632dd39 commit ea5614c
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 47 deletions.
32 changes: 28 additions & 4 deletions ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<conn_types>(this->readUInt8(static_cast<uint8_t>(conn_types::unset)));
settings.IP.dhcp = this->readBool(true);
Expand All @@ -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<eth_phy_type_t>(this->readUInt8(0));
settings.Ethernet.CLKMode = static_cast<eth_clock_mode_t>(this->readUInt8(0));
Expand All @@ -609,16 +622,16 @@ 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);
}
}
return true;
}
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<radio_proto>(this->readUInt8(0));
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<uint8_t>(settings.Ethernet.phyType));
this->writeUInt8(static_cast<uint8_t>(settings.Ethernet.CLKMode));
Expand Down
6 changes: 6 additions & 0 deletions ConfigSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 31 additions & 33 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(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);
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;
}
}
else
sockEmit.sendToClient(num, "ethernet", "{\"connected\":false, \"speed\":0,\"fullduplex\":false}");

}
void Network::setConnected(conn_types connType) {
this->connType = connType;
Expand Down
1 change: 1 addition & 0 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Binary file modified SomfyController.ino.esp32.bin
Binary file not shown.
Binary file modified SomfyController.ino.esp32s3.bin
Binary file not shown.
Binary file modified SomfyController.littlefs.bin
Binary file not shown.
11 changes: 11 additions & 0 deletions Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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\":");
Expand All @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css?v=2.4.0a" type="text/css" />
<link rel="stylesheet" href="widgets.css?v=2.4.0a" type="text/css" />
<link rel="stylesheet" href="icons.css?v=2.4.0a" type="text/css" />
<link rel="stylesheet" href="main.css?v=2.4.0c" type="text/css" />
<link rel="stylesheet" href="widgets.css?v=2.4.0c" type="text/css" />
<link rel="stylesheet" href="icons.css?v=2.4.0c" type="text/css" />
<link rel="icon" type="image/png" href="favicon.png" />
<script type="text/javascript" src="index.js?v=2.4.0a"></script>
<script type="text/javascript" src="index.js?v=2.4.0c"></script>
</head>
<body>
<div id="divContainer" class="container main" data-auth="false">
Expand Down
2 changes: 1 addition & 1 deletion data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions data/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css?v=2.0.0" type="text/css" />
<link rel="stylesheet" href="widgets.css?v=2.0.0" type="text/css" />
<link rel="stylesheet" href="icons.css?v=2.0.0" type="text/css" />
<link rel="stylesheet" href="main.css?v=2.4.0" type="text/css" />
<link rel="stylesheet" href="widgets.css?v=2.4.0" type="text/css" />
<link rel="stylesheet" href="icons.css?v=2.4.0" type="text/css" />
<link rel="icon" type="image/png" href="favicon.png" />
<script type="text/javascript" src="index.js?v=2.0.0"></script>
<script type="text/javascript" src="index.js?v=2.4.0"></script>
</head>
<body onload="general.loadLogin();">
<div id="divContainer" class="container" data-securitytype="0">
Expand Down

0 comments on commit ea5614c

Please sign in to comment.