mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2025-12-13 02:52:11 +01:00
Disconnect from MQTT and reconnect when changing settings
This commit is contained in:
parent
07255fc7f0
commit
d91535c9ac
6 changed files with 52 additions and 7 deletions
|
|
@ -360,11 +360,21 @@ bool ShadeConfigFile::writeShadeRecord(SomfyShade *shade) {
|
|||
this->writeUInt32(rem->getRemoteAddress());
|
||||
}
|
||||
this->writeUInt16(shade->lastRollingCode);
|
||||
if(shade->getShadeId() != 255) {
|
||||
this->writeUInt8(shade->flags & static_cast<uint8_t>(somfy_flags_t::SunFlag));
|
||||
this->writeFloat(shade->myPos, 5);
|
||||
this->writeFloat(shade->myTiltPos, 5);
|
||||
this->writeFloat(shade->currentPos, 5);
|
||||
this->writeFloat(shade->currentTiltPos, 5, CFG_REC_END);
|
||||
}
|
||||
else {
|
||||
// Make sure that we write cleared values when the shade is deleted.
|
||||
this->writeUInt8(0);
|
||||
this->writeFloat(-1.0f, 5); // MyPos
|
||||
this->writeFloat(-1.0f, 5); // MyTiltPos
|
||||
this->writeFloat(0.0f, 5); // currentPos
|
||||
this->writeFloat(0.0f, 5, CFG_REC_END); // currentTiltPos
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool ShadeConfigFile::exists() { return LittleFS.exists("/shades.cfg"); }
|
||||
|
|
|
|||
9
MQTT.cpp
9
MQTT.cpp
|
|
@ -18,8 +18,13 @@ bool MQTTClass::begin() {
|
|||
}
|
||||
bool MQTTClass::end() {
|
||||
this->disconnect();
|
||||
this->lastConnect = 0;
|
||||
this->connect();
|
||||
return true;
|
||||
}
|
||||
void MQTTClass::reset() {
|
||||
this->disconnect();
|
||||
}
|
||||
bool MQTTClass::loop() {
|
||||
if(settings.MQTT.enabled && !mqttClient.connected())
|
||||
this->connect();
|
||||
|
|
@ -121,6 +126,7 @@ bool MQTTClass::connect() {
|
|||
this->subscribe("shades/+/tiltTarget/set");
|
||||
this->subscribe("shades/+/direction/set");
|
||||
this->subscribe("shades/+/mypos/set");
|
||||
this->subscribe("shades/+/myTiltPos/set");
|
||||
this->subscribe("shades/+/sunFlag/set");
|
||||
mqttClient.setCallback(MQTTClass::receive);
|
||||
this->lastConnect = millis();
|
||||
|
|
@ -144,6 +150,7 @@ bool MQTTClass::disconnect() {
|
|||
this->unsubscribe("shades/+/direction/set");
|
||||
this->unsubscribe("shades/+/tiltTarget/set");
|
||||
this->unsubscribe("shades/+/mypos/set");
|
||||
this->unsubscribe("shades/+/myTiltPos/set");
|
||||
this->unsubscribe("shades/+/sunFlag/set");
|
||||
mqttClient.disconnect();
|
||||
}
|
||||
|
|
@ -156,6 +163,8 @@ bool MQTTClass::unsubscribe(const char *topic) {
|
|||
snprintf(top, sizeof(top), "%s/%s", settings.MQTT.rootTopic, topic);
|
||||
else
|
||||
strlcpy(top, topic, sizeof(top));
|
||||
Serial.print("MQTT Unsubscribed from:");
|
||||
Serial.println(top);
|
||||
return mqttClient.unsubscribe(top);
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
1
MQTT.h
1
MQTT.h
|
|
@ -13,6 +13,7 @@ class MQTTClass {
|
|||
bool connect();
|
||||
bool disconnect();
|
||||
bool connected();
|
||||
void reset();
|
||||
bool publish(const char *topic, const char *payload);
|
||||
bool publish(const char *topic, JsonDocument &doc);
|
||||
bool publish(const char *topic, JsonArray &arr);
|
||||
|
|
|
|||
23
Somfy.cpp
23
Somfy.cpp
|
|
@ -971,7 +971,9 @@ void SomfyShade::publish() {
|
|||
snprintf(topic, sizeof(topic), "shades/%u/lastRollingCode", this->shadeId);
|
||||
mqtt.publish(topic, this->lastRollingCode);
|
||||
snprintf(topic, sizeof(topic), "shades/%u/mypos", this->shadeId);
|
||||
mqtt.publish(topic, static_cast<uint8_t>(floor(this->myPos)));
|
||||
mqtt.publish(topic, static_cast<int8_t>(floor(this->myPos)));
|
||||
snprintf(topic, sizeof(topic), "shades/%u/myTiltPos", this->shadeId);
|
||||
mqtt.publish(topic, static_cast<int8_t>(floor(this->myTiltPos)));
|
||||
snprintf(topic, sizeof(topic), "shades/%u/shadeType", this->shadeId);
|
||||
mqtt.publish(topic, static_cast<uint8_t>(this->shadeType));
|
||||
snprintf(topic, sizeof(topic), "shades/%u/tiltType", this->shadeId);
|
||||
|
|
@ -987,6 +989,18 @@ void SomfyShade::publish() {
|
|||
snprintf(topic, sizeof(topic), "shades/%u/tiltTarget", this->shadeId);
|
||||
mqtt.publish(topic, static_cast<uint8_t>(floor(this->tiltTarget)));
|
||||
}
|
||||
else if (this->shadeType == shade_types::awning) {
|
||||
const uint8_t sunFlag = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::SunFlag));
|
||||
const uint8_t isSunny = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny));
|
||||
const uint8_t isWindy = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::Windy));
|
||||
|
||||
snprintf(topic, sizeof(topic), "shades/%u/sunFlag", this->shadeId);
|
||||
mqtt.publish(topic, sunFlag);
|
||||
snprintf(topic, sizeof(topic), "shades/%u/sunny", this->shadeId);
|
||||
mqtt.publish(topic, isSunny);
|
||||
snprintf(topic, sizeof(topic), "shades/%u/windy", this->shadeId);
|
||||
mqtt.publish(topic, isWindy);
|
||||
}
|
||||
}
|
||||
}
|
||||
void SomfyShade::emitState(const char *evt) { this->emitState(255, evt); }
|
||||
|
|
@ -1898,6 +1912,13 @@ bool SomfyShadeController::deleteShade(uint8_t shadeId) {
|
|||
if(this->shades[i].getShadeId() == shadeId) {
|
||||
shades[i].emitState("shadeRemoved");
|
||||
this->shades[i].setShadeId(255);
|
||||
this->shades[i].currentPos = 0;
|
||||
this->shades[i].currentTiltPos = 0;
|
||||
this->shades[i].myPos = -1.0f;
|
||||
this->shades[i].myTiltPos = -1.0f;
|
||||
this->shades[i].shadeType = shade_types::roller;
|
||||
this->shades[i].tiltType = tilt_types::none;
|
||||
this->shades[i].flags = 0;
|
||||
}
|
||||
}
|
||||
if(this->useNVS()) {
|
||||
|
|
|
|||
Binary file not shown.
6
Web.cpp
6
Web.cpp
|
|
@ -7,13 +7,14 @@
|
|||
#include "Utils.h"
|
||||
#include "SSDP.h"
|
||||
#include "Somfy.h"
|
||||
#include "MQTT.h"
|
||||
|
||||
extern ConfigSettings settings;
|
||||
extern SSDPClass SSDP;
|
||||
extern rebootDelay_t rebootDelay;
|
||||
extern SomfyShadeController somfy;
|
||||
extern Web webServer;
|
||||
|
||||
extern MQTTClass mqtt;
|
||||
#define WEB_MAX_RESPONSE 16384
|
||||
static char g_content[WEB_MAX_RESPONSE];
|
||||
|
||||
|
|
@ -1477,11 +1478,14 @@ void Web::begin() {
|
|||
else {
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
HTTPMethod method = server.method();
|
||||
Serial.print("Saving MQTT ");
|
||||
Serial.print(F("HTTP Method: "));
|
||||
Serial.println(server.method());
|
||||
if (method == HTTP_POST || method == HTTP_PUT) {
|
||||
mqtt.disconnect();
|
||||
settings.MQTT.fromJSON(obj);
|
||||
settings.MQTT.save();
|
||||
|
||||
StaticJsonDocument<512> sdoc;
|
||||
JsonObject sobj = sdoc.to<JsonObject>();
|
||||
settings.MQTT.toJSON(sobj);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue