Skip to content

Commit

Permalink
TuyaMCU: add missing heartbeats event so we can reset cpu or take act…
Browse files Browse the repository at this point in the history
…ion when something is wrong, sample script: addChangeHandler MissedHeartbeats > 4 setChannel 0 1
  • Loading branch information
openshwprojects committed Nov 25, 2024
1 parent d450bf7 commit 8c0bafc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cmnds/cmd_eventHandlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ int EVENT_ParseEventName(const char *s) {
return CMD_EVENT_CUSTOM_DOWN;
if (!stricmp(s, "OnCustomUP"))
return CMD_EVENT_CUSTOM_UP;
if (!stricmp(s, "MissedHeartbeats"))
return CMD_EVENT_MISSEDHEARTBEATS;
if (isdigit((unsigned char)*s)) {
return atoi(s);
}
Expand Down
2 changes: 2 additions & 0 deletions src/cmnds/cmd_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ enum EventCode {
CMD_EVENT_CUSTOM_DOWN,
CMD_EVENT_CUSTOM_UP,

CMD_EVENT_MISSEDHEARTBEATS,

// must be lower than 256
CMD_EVENT_MAX_TYPES
};
Expand Down
11 changes: 9 additions & 2 deletions src/driver/drv_tuyaMCU.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const char* TuyaMCU_GetCommandTypeLabel(int t) {
return "TUYA_CMD_REPORT_STATUS_RECORD_TYPE";
return "Unknown";
}

typedef struct rtcc_s {
uint8_t second;
uint8_t minute;
Expand Down Expand Up @@ -348,6 +349,12 @@ tuyaMCUMapping_t* TuyaMCU_MapIDToChannel(int dpId, int dpType, int channel, int
return cur;
}

// now you can detect TuyaMCU faults with event handler
// addChangeHandler MissedHeartbeats > 4 setChannel 0 1
void TuyaMCU_SetHeartbeatTimer(int v) {
EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_MISSEDHEARTBEATS, heartbeat_counter, v);
heartbeat_counter = v;
}

// header version command lenght data checksum
// 55AA 00 00 0000 xx 00
Expand Down Expand Up @@ -1764,7 +1771,7 @@ void TuyaMCU_ProcessIncoming(const byte* data, int len) {
{
case TUYA_CMD_HEARTBEAT:
heartbeat_valid = true;
heartbeat_counter = 0;
TuyaMCU_SetHeartbeatTimer(0);
break;
case TUYA_CMD_MCU_CONF:
working_mode_valid = true;
Expand Down Expand Up @@ -2062,7 +2069,7 @@ void TuyaMCU_RunStateMachine_V3() {
/* Generate heartbeat to keep communication alove */
TuyaMCU_SendCommandWithData(TUYA_CMD_HEARTBEAT, NULL, 0);
heartbeat_timer = 3;
heartbeat_counter++;
TuyaMCU_SetHeartbeatTimer(heartbeat_counter+1);
if (heartbeat_counter >= 4)
{
/* unanswerred heartbeats -> lost communication */
Expand Down

0 comments on commit 8c0bafc

Please sign in to comment.