Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver: tuyaMCU: introduce battery-powered device ack-delay #1497

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/driver/drv_tuyaMCU.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ static int wifi_state_timer = 0;
static bool self_processing_mode = true;
static bool state_updated = false;
static int g_sendQueryStatePackets = 0;
static int g_tuyaMCUBatteryAckDelay = 0;

// wifistate to send when not online
// See: https://imgur.com/a/mEfhfiA
Expand Down Expand Up @@ -1895,6 +1896,11 @@ void TuyaMCU_ProcessIncoming(const byte* data, int len) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "ProcessIncoming: 0x04 replying");
// added for https://www.elektroda.com/rtvforum/viewtopic.php?p=21095905#21095905
TuyaMCU_SendCommandWithData(0x04, 0, 0);

/* On Version 0, this indicates system reset */
if (version == 0) {
g_openAP = 1;
}
break;
case 0x22:
{
Expand Down Expand Up @@ -2040,6 +2046,35 @@ commandResult_t TuyaMCU_FakePacket(const void* context, const char* cmd, const c
TuyaMCU_ProcessIncoming(packet, c);
return CMD_RES_OK;
}

commandResult_t Cmd_TuyaMCU_SetBatteryAckDelay(const void* context, const char* cmd, const char* args, int cmdFlags) {
int delay;

Tokenizer_TokenizeString(args, 0);
Tokenizer_CheckArgsCountAndPrintWarning(args, 1);

delay = Tokenizer_GetArgInteger(0);

if (!Tokenizer_IsArgInteger(0)) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "SetBatteryAckDelay: requires 1 argument [delay in seconds]\n");
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

if (delay < 0) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "SetBatteryAckDelay: delay must be positive\n");
return CMD_RES_BAD_ARGUMENT;
}

if (delay > 60) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "SetBatteryAckDelay: delay too long, max 60 seconds\n");
return CMD_RES_BAD_ARGUMENT;
}

g_tuyaMCUBatteryAckDelay = delay;

return CMD_RES_OK;
}

void TuyaMCU_RunWiFiUpdateAndPackets() {
//addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU,"WifiCheck %d ", wifi_state_timer);
/* Monitor WIFI and MQTT connection and apply Wifi state
Expand Down Expand Up @@ -2239,6 +2274,11 @@ void TuyaMCU_RunStateMachine_BatteryPowered() {
break;
case TM0_STATE_AWAITING_STATES:
if (g_tuyaNextRequestDelay <= 0) {
if (g_tuyaMCUBatteryAckDelay > 0) {
g_tuyaMCUBatteryAckDelay--;
break;
}

byte dat = 0x00;
if (g_tuyaMCUConfirmationsToSend_0x08 > 0) {
g_tuyaMCUConfirmationsToSend_0x08--;
Expand Down Expand Up @@ -2463,6 +2503,12 @@ void TuyaMCU_Init()
//cmddetail:"fn":"NULL);","file":"driver/drv_tuyaMCU.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("tuyaMcu_setupLED", Cmd_TuyaMCU_SetupLED, NULL);

//cmddetail:{"name":"Cmd_TuyaMCU_SetBatteryAckDelay","args":"[ackDelay]",
//cmddetail:"descr":"Defines the delay before the ACK is sent to TuyaMCU sending the device to sleep. Default value is 20 seconds.",
//cmddetail:"fn":"NULL);","file":"driver/drv_tuyaMCU.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("tuyaMcu_setBatteryAckDelay", Cmd_TuyaMCU_SetBatteryAckDelay, NULL);
}


Expand Down