Skip to content

Commit

Permalink
automatic SENSOR broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Jan 25, 2023
1 parent 2105868 commit 9b31e50
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/httpserver/json_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,13 @@ static int http_tasmota_json_ENERGY(void* request, jsonCb_t printer) {
}
}
*/
static int http_tasmota_json_status_SNS(void* request, jsonCb_t printer) {
static int http_tasmota_json_status_SNS(void* request, jsonCb_t printer, bool bAppendHeader) {
char buff[20];

printer(request, "\"StatusSNS\":{");
if (bAppendHeader) {
printer(request, "\"StatusSNS\":");
}
printer(request, "{");

time_t localTime = (time_t)NTP_GetCurrentTime();
strftime(buff, sizeof(buff), "%Y-%m-%dT%H:%M:%S", localtime(&localTime));
Expand Down Expand Up @@ -570,7 +573,7 @@ static int http_tasmota_json_status_generic(void* request, jsonCb_t printer) {
printer(request, ",");


http_tasmota_json_status_SNS(request, printer);
http_tasmota_json_status_SNS(request, printer, true);

printer(request, ",");

Expand All @@ -584,8 +587,6 @@ static int http_tasmota_json_status_generic(void* request, jsonCb_t printer) {

return 0;
}
// TODO: move it somewhere else?
void MQTT_PublishPrinterContentsToStat(struct obk_mqtt_publishReplyPrinter_s *printer, const char *statName);
int JSON_ProcessCommandReply(const char *cmd, const char *arg, void *request, jsonCb_t printer, int flags) {

if (!wal_strnicmp(cmd, "POWER", 5)) {
Expand Down Expand Up @@ -632,10 +633,17 @@ int JSON_ProcessCommandReply(const char *cmd, const char *arg, void *request, js
MQTT_PublishPrinterContentsToTele((struct obk_mqtt_publishReplyPrinter_s *)request, "STATE");
}
}
else if (!wal_strnicmp(cmd, "SENSOR", 5)) {
// not a Tasmota command, but still required for us
http_tasmota_json_status_SNS(request, printer, false);
if (flags == COMMAND_FLAG_SOURCE_TELESENDER) {
MQTT_PublishPrinterContentsToTele((struct obk_mqtt_publishReplyPrinter_s *)request, "SENSOR");
}
}
else if (!wal_strnicmp(cmd, "STATUS", 6)) {
if (!stricmp(arg, "8") || !stricmp(arg, "10")) {
printer(request, "{");
http_tasmota_json_status_SNS(request, printer);
http_tasmota_json_status_SNS(request, printer, true);
printer(request, "}");
if (flags == COMMAND_FLAG_SOURCE_MQTT) {
if (arg[0] == '8') {
Expand Down
16 changes: 14 additions & 2 deletions src/mqtt/new_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,17 @@ void MQTT_BroadcastTasmotaTeleSTATE() {
MQTT_ProcessCommandReplyJSON("STATE", "", COMMAND_FLAG_SOURCE_TELESENDER);
g_wantTasmotaTeleSend = 0;
}

void MQTT_BroadcastTasmotaTeleSENSOR() {
bool bHasAnySensor = false;
#ifndef OBK_DISABLE_ALL_DRIVERS
if (DRV_IsMeasuringPower()) {
bHasAnySensor = true;
}
#endif
if (bHasAnySensor) {
MQTT_ProcessCommandReplyJSON("SENSOR", "", COMMAND_FLAG_SOURCE_TELESENDER);
}
}
// called from user timer.
int MQTT_RunEverySecondUpdate()
{
Expand All @@ -1769,7 +1779,6 @@ int MQTT_RunEverySecondUpdate()
if (g_mqtt_bBaseTopicDirty) {
MQTT_InitCallbacks();
mqtt_reconnect = 5;
return 0;
}

// reconnect if went into MQTT library ERR_MEM forever loop
Expand Down Expand Up @@ -1879,6 +1888,9 @@ int MQTT_RunEverySecondUpdate()

static int c = 0;
c++;
if (c == 30) {
MQTT_BroadcastTasmotaTeleSENSOR();
}
if (c > 120) {
c = 0;
MQTT_BroadcastTasmotaTeleSTATE();
Expand Down
1 change: 1 addition & 0 deletions src/mqtt/new_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void MQTT_init();
int MQTT_RunQuickTick();
int MQTT_RunEverySecondUpdate();
void MQTT_BroadcastTasmotaTeleSTATE();
void MQTT_BroadcastTasmotaTeleSENSOR();


#define PUBLISHITEM_ALL_INDEX_FIRST -15
Expand Down
8 changes: 7 additions & 1 deletion src/selftest/selftest_tasmota.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void Test_Tasmota_MQTT_Switch() {
const char *my_full_device_name = "TestingDevMQTTSwitch";
CFG_SetDeviceName(my_full_device_name);

CFG_SetFlag(OBK_FLAG_DO_TASMOTA_TELE_PUBLISHES, true);

PIN_SetPinRoleForPinIndex(9, IOR_Relay);
PIN_SetPinChannelForPinIndex(9, 1);

Expand Down Expand Up @@ -263,6 +265,8 @@ void Test_Tasmota_MQTT_Switch_Double() {
const char *my_full_device_name = "TestingDevMQTTSwitch";
CFG_SetDeviceName(my_full_device_name);

CFG_SetFlag(OBK_FLAG_DO_TASMOTA_TELE_PUBLISHES, true);

PIN_SetPinRoleForPinIndex(9, IOR_Relay);
PIN_SetPinChannelForPinIndex(9, 1);

Expand Down Expand Up @@ -384,7 +388,7 @@ void Test_Tasmota_MQTT_Switch_Double() {
}
*/
SIM_ClearMQTTHistory();
Sim_RunSeconds(10, false);
Sim_RunSeconds(130, false);
SELFTEST_ASSERT_HAS_MQTT_JSON_SENT("tele/twoRelaysDevice/STATE", false);
SELFTEST_ASSERT_JSON_VALUE_EXISTS(0, "Time");
SELFTEST_ASSERT_JSON_VALUE_EXISTS(0, "Uptime");
Expand All @@ -403,6 +407,8 @@ void Test_Tasmota_MQTT_RGBCW() {

CMD_ExecuteCommand("led_dimmer 50", 0);

CFG_SetFlag(OBK_FLAG_DO_TASMOTA_TELE_PUBLISHES, true);

PIN_SetPinRoleForPinIndex(24, IOR_PWM);
PIN_SetPinChannelForPinIndex(24, 1);

Expand Down

0 comments on commit 9b31e50

Please sign in to comment.