Skip to content

Commit 98a6ce2

Browse files
committed
refactor(iot): adopt reset-complete and centralized mqtt lifecycle
Align sample apps with SDK event flow; add secrets templates for cloud demos. Made-with: Cursor
1 parent 9af72c0 commit 98a6ce2

20 files changed

Lines changed: 283 additions & 186 deletions

File tree

apps/tuya.ai/duo_eye_mood/src/tuya_main.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ tuya_iot_license_t license;
6464

6565
#define DPID_VOLUME 3
6666

67-
static uint8_t _need_reset = 0;
68-
6967
/**
7068
* @brief user defined log output api, in this demo, it will use uart0 as log-tx
7169
*
@@ -167,15 +165,10 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
167165
switch (event->id) {
168166
case TUYA_EVENT_BIND_START:
169167
PR_INFO("Device Bind Start!");
170-
if (_need_reset == 1) {
171-
PR_INFO("Device Reset!");
172-
tal_system_reset();
173-
}
174168

175169
#if defined(ENABLE_COMP_AI_AUDIO) && (ENABLE_COMP_AI_AUDIO == 1)
176170
ai_audio_player_alert(AI_AUDIO_ALERT_NETWORK_CFG);
177171
#endif
178-
179172
break;
180173

181174
/* Print the QRCode for Tuya APP bind */
@@ -193,7 +186,6 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
193186
/* MQTT with tuya cloud is connected, device online */
194187
case TUYA_EVENT_MQTT_CONNECTED:
195188
PR_INFO("Device MQTT Connected!");
196-
tal_event_publish(EVENT_MQTT_CONNECTED, NULL);
197189

198190
static uint8_t first = 1;
199191
if (first) {
@@ -207,29 +199,29 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
207199
}
208200
break;
209201

210-
/* MQTT with tuya cloud is disconnected, device offline */
211-
case TUYA_EVENT_MQTT_DISCONNECT:
212-
PR_INFO("Device MQTT DisConnected!");
213-
tal_event_publish(EVENT_MQTT_DISCONNECTED, NULL);
214-
break;
215-
216202
/* RECV upgrade request */
217203
case TUYA_EVENT_UPGRADE_NOTIFY:
218204
user_upgrade_notify_on(client, event->value.asJSON);
219205
break;
220206

221207
/* Sync time with tuya Cloud */
222208
case TUYA_EVENT_TIMESTAMP_SYNC:
223-
PR_INFO("Sync timestamp:%d", event->value.asInteger);
224-
tal_time_set_posix(event->value.asInteger, 1);
225209
tal_event_publish("app.time.sync", NULL);
226210
break;
227211

228-
case TUYA_EVENT_RESET:
229-
PR_INFO("Device Reset:%d", event->value.asInteger);
212+
case TUYA_EVENT_RESET: {
213+
tuya_reset_type_t reset_type = (tuya_reset_type_t)event->value.asInteger;
214+
PR_INFO("Device Reset:%d", reset_type);
230215

231-
_need_reset = 1;
232-
break;
216+
// TUYA_RESET_TYPE_FACTORY, TUYA_RESET_TYPE_REMOTE_FACTORY, TUYA_RESET_TYPE_DATA_FACTORY
217+
// Need remove the device application data from the kv store
218+
} break;
219+
case TUYA_EVENT_RESET_COMPLETE: {
220+
PR_INFO("Device Reset Complete!");
221+
222+
// Restart the device
223+
tal_system_reset();
224+
} break;
233225

234226
/* RECV OBJ DP */
235227
case TUYA_EVENT_DP_RECEIVE_OBJ: {

apps/tuya.ai/your_chat_bot/src/tuya_main.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ tuya_iot_license_t license;
6464

6565
#define DPID_VOLUME 3
6666

67-
static uint8_t _need_reset = 0;
68-
6967
/**
7068
* @brief user defined log output api, in this demo, it will use uart0 as log-tx
7169
*
@@ -167,10 +165,6 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
167165
switch (event->id) {
168166
case TUYA_EVENT_BIND_START:
169167
PR_INFO("Device Bind Start!");
170-
if (_need_reset == 1) {
171-
PR_INFO("Device Reset!");
172-
tal_system_reset();
173-
}
174168

175169
#if defined(ENABLE_COMP_AI_AUDIO) && (ENABLE_COMP_AI_AUDIO == 1)
176170
ai_audio_player_alert(AI_AUDIO_ALERT_NETWORK_CFG);
@@ -193,7 +187,6 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
193187
/* MQTT with tuya cloud is connected, device online */
194188
case TUYA_EVENT_MQTT_CONNECTED:
195189
PR_INFO("Device MQTT Connected!");
196-
tal_event_publish(EVENT_MQTT_CONNECTED, NULL);
197190

198191
static uint8_t first = 1;
199192
if (first) {
@@ -210,7 +203,6 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
210203
/* MQTT with tuya cloud is disconnected, device offline */
211204
case TUYA_EVENT_MQTT_DISCONNECT:
212205
PR_INFO("Device MQTT DisConnected!");
213-
tal_event_publish(EVENT_MQTT_DISCONNECTED, NULL);
214206
break;
215207

216208
/* RECV upgrade request */
@@ -221,15 +213,22 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
221213
/* Sync time with tuya Cloud */
222214
case TUYA_EVENT_TIMESTAMP_SYNC:
223215
PR_INFO("Sync timestamp:%d", event->value.asInteger);
224-
tal_time_set_posix(event->value.asInteger, 1);
225216
tal_event_publish("app.time.sync", NULL);
226217
break;
227218

228-
case TUYA_EVENT_RESET:
229-
PR_INFO("Device Reset:%d", event->value.asInteger);
219+
case TUYA_EVENT_RESET: {
220+
tuya_reset_type_t reset_type = (tuya_reset_type_t)event->value.asInteger;
221+
PR_INFO("Device Reset:%d", reset_type);
230222

231-
_need_reset = 1;
232-
break;
223+
// TUYA_RESET_TYPE_FACTORY, TUYA_RESET_TYPE_REMOTE_FACTORY, TUYA_RESET_TYPE_DATA_FACTORY
224+
// Need remove the device application data from the kv store
225+
} break;
226+
case TUYA_EVENT_RESET_COMPLETE: {
227+
PR_INFO("Device Reset Complete!");
228+
229+
// Restart the device
230+
tal_system_reset();
231+
} break;
233232

234233
/* RECV OBJ DP */
235234
case TUYA_EVENT_DP_RECEIVE_OBJ: {

apps/tuya.ai/your_desk_emoji/src/tuya_main.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ tuya_iot_license_t license;
5858
#define DPID_SERVO 5
5959

6060
bool _s_servo_busy = FALSE;
61-
static uint8_t _need_reset = 0;
6261
static SERVO_ACTION_E _s_servo_action = SERVO_CENTER;
6362

6463
/**
@@ -293,53 +292,44 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
293292
switch (event->id) {
294293
case TUYA_EVENT_BIND_START:
295294
PR_INFO("Device Bind Start!");
296-
if (_need_reset == 1) {
297-
PR_INFO("Device Reset!");
298-
tal_system_reset();
299-
}
300295

301296
#if defined(ENABLE_COMP_AI_AUDIO) && (ENABLE_COMP_AI_AUDIO == 1)
302297
ai_audio_player_alert(AI_AUDIO_ALERT_NETWORK_CFG);
303298
#endif
304-
305-
break;
306-
case TUYA_EVENT_BIND_TOKEN_ON:
307299
break;
308-
309300
/* MQTT with tuya cloud is connected, device online */
310301
case TUYA_EVENT_MQTT_CONNECTED:
311302
PR_INFO("Device MQTT Connected!");
312-
tal_event_publish(EVENT_MQTT_CONNECTED, NULL);
313303
static uint8_t first = 1;
314304
if (first) {
315305
first = 0;
316306
ai_audio_volume_upload();
317307
}
318308
break;
319309

320-
/* MQTT with tuya cloud is disconnected, device offline */
321-
case TUYA_EVENT_MQTT_DISCONNECT:
322-
PR_INFO("Device MQTT DisConnected!");
323-
tal_event_publish(EVENT_MQTT_DISCONNECTED, NULL);
324-
break;
325-
326310
/* RECV upgrade request */
327311
case TUYA_EVENT_UPGRADE_NOTIFY:
328312
user_upgrade_notify_on(client, event->value.asJSON);
329313
break;
330314

331315
/* Sync time with tuya Cloud */
332316
case TUYA_EVENT_TIMESTAMP_SYNC:
333-
PR_INFO("Sync timestamp:%d", event->value.asInteger);
334-
tal_time_set_posix(event->value.asInteger, 1);
335317
tal_event_publish("app.time.sync", NULL);
336318
break;
337319

338-
case TUYA_EVENT_RESET:
339-
PR_INFO("Device Reset:%d", event->value.asInteger);
320+
case TUYA_EVENT_RESET: {
321+
tuya_reset_type_t reset_type = (tuya_reset_type_t)event->value.asInteger;
322+
PR_INFO("Device Reset:%d", reset_type);
340323

341-
_need_reset = 1;
342-
break;
324+
// TUYA_RESET_TYPE_FACTORY, TUYA_RESET_TYPE_REMOTE_FACTORY, TUYA_RESET_TYPE_DATA_FACTORY
325+
// Need remove the device application data from the kv store
326+
} break;
327+
case TUYA_EVENT_RESET_COMPLETE: {
328+
PR_INFO("Device Reset Complete!");
329+
330+
// Restart the device
331+
tal_system_reset();
332+
} break;
343333

344334
/* RECV OBJ DP */
345335
case TUYA_EVENT_DP_RECEIVE_OBJ: {

apps/tuya.ai/your_otto_robot/src/tuya_main.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ tuya_iot_license_t license;
6868

6969
#define DPID_VOLUME 6
7070

71-
static uint8_t _need_reset = 0;
72-
7371
/**
7472
* @brief user defined log output api, in this demo, it will use uart0 as log-tx
7573
*
@@ -177,15 +175,10 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
177175
switch (event->id) {
178176
case TUYA_EVENT_BIND_START:
179177
PR_INFO("Device Bind Start!");
180-
if (_need_reset == 1) {
181-
PR_INFO("Device Reset!");
182-
tal_system_reset();
183-
}
184178

185179
#if defined(ENABLE_COMP_AI_AUDIO) && (ENABLE_COMP_AI_AUDIO == 1)
186180
ai_audio_player_alert(AI_AUDIO_ALERT_NETWORK_CFG);
187181
#endif
188-
189182
break;
190183

191184
/* Print the QRCode for Tuya APP bind */
@@ -197,13 +190,9 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
197190
#endif
198191
} break;
199192

200-
case TUYA_EVENT_BIND_TOKEN_ON:
201-
break;
202-
203193
/* MQTT with tuya cloud is connected, device online */
204194
case TUYA_EVENT_MQTT_CONNECTED:
205195
PR_INFO("Device MQTT Connected!");
206-
tal_event_publish(EVENT_MQTT_CONNECTED, NULL);
207196

208197
static uint8_t first = 1;
209198
if (first) {
@@ -217,29 +206,28 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
217206
}
218207
break;
219208

220-
/* MQTT with tuya cloud is disconnected, device offline */
221-
case TUYA_EVENT_MQTT_DISCONNECT:
222-
PR_INFO("Device MQTT DisConnected!");
223-
tal_event_publish(EVENT_MQTT_DISCONNECTED, NULL);
224-
break;
225-
226209
/* RECV upgrade request */
227210
case TUYA_EVENT_UPGRADE_NOTIFY:
228211
user_upgrade_notify_on(client, event->value.asJSON);
229212
break;
230213

231214
/* Sync time with tuya Cloud */
232215
case TUYA_EVENT_TIMESTAMP_SYNC:
233-
PR_INFO("Sync timestamp:%d", event->value.asInteger);
234-
tal_time_set_posix(event->value.asInteger, 1);
235216
tal_event_publish("app.time.sync", NULL);
236217
break;
218+
case TUYA_EVENT_RESET: {
219+
tuya_reset_type_t reset_type = (tuya_reset_type_t)event->value.asInteger;
220+
PR_INFO("Device Reset:%d", reset_type);
237221

238-
case TUYA_EVENT_RESET:
239-
PR_INFO("Device Reset:%d", event->value.asInteger);
222+
// TUYA_RESET_TYPE_FACTORY, TUYA_RESET_TYPE_REMOTE_FACTORY, TUYA_RESET_TYPE_DATA_FACTORY
223+
// Need remove the device application data from the kv store
224+
} break;
225+
case TUYA_EVENT_RESET_COMPLETE: {
226+
PR_INFO("Device Reset Complete!");
240227

241-
_need_reset = 1;
242-
break;
228+
// Restart the device
229+
tal_system_reset();
230+
} break;
243231

244232
/* RECV OBJ DP */
245233
case TUYA_EVENT_DP_RECEIVE_OBJ: {

apps/tuya.ai/your_robot_dog/src/tuya_main.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ tuya_iot_license_t license;
7373
#define DPID_VOLUME 3
7474
#define DPID_MOVE 8
7575

76-
static uint8_t _need_reset = 0;
77-
7876
/**
7977
* @brief user defined log output api, in this demo, it will use uart0 as log-tx
8078
*
@@ -242,13 +240,9 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
242240
#endif
243241
} break;
244242

245-
case TUYA_EVENT_BIND_TOKEN_ON:
246-
break;
247-
248243
/* MQTT with tuya cloud is connected, device online */
249244
case TUYA_EVENT_MQTT_CONNECTED:
250245
PR_INFO("Device MQTT Connected!");
251-
tal_event_publish(EVENT_MQTT_CONNECTED, NULL);
252246

253247
static uint8_t first = 1;
254248
if (first) {
@@ -257,28 +251,23 @@ void user_event_handler_on(tuya_iot_client_t *client, tuya_event_msg_t *event)
257251
}
258252
break;
259253

260-
/* MQTT with tuya cloud is disconnected, device offline */
261-
case TUYA_EVENT_MQTT_DISCONNECT:
262-
PR_INFO("Device MQTT DisConnected!");
263-
tal_event_publish(EVENT_MQTT_DISCONNECTED, NULL);
264-
break;
265-
266254
/* RECV upgrade request */
267255
case TUYA_EVENT_UPGRADE_NOTIFY:
268256
user_upgrade_notify_on(client, event->value.asJSON);
269257
break;
258+
case TUYA_EVENT_RESET: {
259+
tuya_reset_type_t reset_type = (tuya_reset_type_t)event->value.asInteger;
260+
PR_INFO("Device Reset:%d", reset_type);
270261

271-
/* Sync time with tuya Cloud */
272-
case TUYA_EVENT_TIMESTAMP_SYNC:
273-
PR_INFO("Sync timestamp:%d", event->value.asInteger);
274-
tal_time_set_posix(event->value.asInteger, 1);
275-
break;
276-
277-
case TUYA_EVENT_RESET:
278-
PR_INFO("Device Reset:%d", event->value.asInteger);
262+
// TUYA_RESET_TYPE_FACTORY, TUYA_RESET_TYPE_REMOTE_FACTORY, TUYA_RESET_TYPE_DATA_FACTORY
263+
// Need remove the device application data from the kv store
264+
} break;
265+
case TUYA_EVENT_RESET_COMPLETE: {
266+
PR_INFO("Device Reset Complete!");
279267

280-
_need_reset = 1;
281-
break;
268+
// Restart the device
269+
tal_system_reset();
270+
} break;
282271

283272
/* RECV OBJ DP */
284273
case TUYA_EVENT_DP_RECEIVE_OBJ: {

0 commit comments

Comments
 (0)