Add command emits to MQTT #186

This commit is contained in:
Robert Strouse 2023-10-31 16:15:43 -07:00
parent 7c983c9b2e
commit e2ec2ad436
3 changed files with 17 additions and 1 deletions

View file

@ -1458,6 +1458,14 @@ bool SomfyShade::publish(const char *topic, int8_t val, bool retain) {
} }
return false; return false;
} }
bool SomfyShade::publish(const char *topic, const char *val, bool retain) {
if(mqtt.connected()) {
snprintf(mqttTopicBuffer, sizeof(mqttTopicBuffer), "shades/%u/%s", this->shadeId, topic);
mqtt.publish(mqttTopicBuffer, val, retain);
return true;
}
return false;
}
bool SomfyShade::publish(const char *topic, uint8_t val, bool retain) { bool SomfyShade::publish(const char *topic, uint8_t val, bool retain) {
if(mqtt.connected()) { if(mqtt.connected()) {
snprintf(mqttTopicBuffer, sizeof(mqttTopicBuffer), "shades/%u/%s", this->shadeId, topic); snprintf(mqttTopicBuffer, sizeof(mqttTopicBuffer), "shades/%u/%s", this->shadeId, topic);
@ -1666,9 +1674,13 @@ void SomfyShade::emitCommand(uint8_t num, somfy_commands cmd, const char *source
e.appendMessage(buf); e.appendMessage(buf);
snprintf(buf, sizeof(buf), ",\"sourceAddress\":%d}", sourceAddress); snprintf(buf, sizeof(buf), ",\"sourceAddress\":%d}", sourceAddress);
e.appendMessage(buf); e.appendMessage(buf);
if(num >= 255) sockEmit.sendToClients(&e); if(num >= 255) sockEmit.sendToClients(&e);
else sockEmit.sendToClient(num, &e); else sockEmit.sendToClient(num, &e);
if(mqtt.connected()) {
this->publish("cmdSource", source);
this->publish("cmdAddress", sourceAddress);
this->publish("cmd", translateSomfyCommand(cmd).c_str());
}
} }
void SomfyGroup::emitState(const char *evt) { this->emitState(255, evt); } void SomfyGroup::emitState(const char *evt) { this->emitState(255, evt); }
void SomfyGroup::emitState(uint8_t num, const char *evt) { void SomfyGroup::emitState(uint8_t num, const char *evt) {
@ -2030,6 +2042,7 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
else if(this->currentPos == 100.0f) this->p_target(0); else if(this->currentPos == 100.0f) this->p_target(0);
else if(this->currentPos == 0.0f) this->p_target(100); else if(this->currentPos == 0.0f) this->p_target(100);
else this->p_target(this->lastMovement == -1 ? 100 : 0); else this->p_target(this->lastMovement == -1 ? 100 : 0);
this->emitCommand(cmd, internal ? "internal" : "remote", frame.remoteAddress);
return; return;
} }
else if(this->shadeType == shade_types::drycontact) { else if(this->shadeType == shade_types::drycontact) {
@ -2040,6 +2053,7 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
if(this->currentPos == 100.0f) this->p_target(0); if(this->currentPos == 100.0f) this->p_target(0);
else if(this->currentPos == 0.0f) this->p_target(100); else if(this->currentPos == 0.0f) this->p_target(100);
else this->p_target(this->lastMovement == -1 ? 100 : 0); else this->p_target(this->lastMovement == -1 ? 100 : 0);
this->emitCommand(cmd, internal ? "internal" : "remote", frame.remoteAddress);
return; return;
} }
if(this->isIdle()) { if(this->isIdle()) {
@ -2061,6 +2075,7 @@ void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
if(this->tiltType != tilt_types::tiltonly) this->p_target(this->currentPos); if(this->tiltType != tilt_types::tiltonly) this->p_target(this->currentPos);
this->p_tiltTarget(this->currentTiltPos); this->p_tiltTarget(this->currentTiltPos);
} }
this->emitCommand(cmd, internal ? "internal" : "remote", frame.remoteAddress);
} }
break; break;
case somfy_commands::StepUp: case somfy_commands::StepUp:

View file

@ -316,6 +316,7 @@ class SomfyShade : public SomfyRemote {
float p_currentPos(float pos); float p_currentPos(float pos);
float p_currentTiltPos(float pos); float p_currentTiltPos(float pos);
uint16_t p_lastRollingCode(uint16_t code); uint16_t p_lastRollingCode(uint16_t code);
bool publish(const char *topic, const char *val, bool retain = false);
bool publish(const char *topic, uint8_t val, bool retain = false); bool publish(const char *topic, uint8_t val, bool retain = false);
bool publish(const char *topic, int8_t val, bool retain = false); bool publish(const char *topic, int8_t val, bool retain = false);
bool publish(const char *topic, uint32_t val, bool retain = false); bool publish(const char *topic, uint32_t val, bool retain = false);

Binary file not shown.