99
1010#include "atop_base.h"
1111#include "tal_log.h"
12+ #include "tal_workq_service.h"
13+ #include "tuya_cloud_types.h"
1214#include "tuya_iot.h"
1315#include "tal_api.h"
1416
2830typedef struct {
2931 cJSON * json ;
3032 MUTEX_HANDLE mutex ;
33+
34+ DELAYED_WORK_HANDLE delayed_work ;
3135} tuya_device_meta_t ;
3236
3337/***********************************************************
@@ -44,14 +48,52 @@ static tuya_device_meta_t s_meta = {0};
4448***********************function define**********************
4549***********************************************************/
4650
51+ static void __device_meta_event_workq (void * data )
52+ {
53+ (void )data ;
54+
55+ OPERATE_RET rt = OPRT_OK ;
56+
57+ rt = tuya_device_meta_add_number ("timerCapability" , 1 );
58+ if (OPRT_OK != rt ) {
59+ PR_ERR ("add timerCapability failed:%d" , rt );
60+ return ;
61+ }
62+
63+ rt = tuya_device_meta_report ();
64+ if (OPRT_OK != rt ) {
65+ PR_ERR ("report meta failed:%d" , rt );
66+ return ;
67+ }
68+
69+ PR_DEBUG ("device meta report success" );
70+
71+ tal_event_publish (EVENT_DEVICE_META_REPORT , NULL );
72+
73+ PR_DEBUG ("device meta report success, event publish" );
74+
75+ tal_workq_cancel_delayed (s_meta .delayed_work );
76+ s_meta .delayed_work = NULL ;
77+ }
78+
4779static int __device_meta_event_cb (void * data )
4880{
4981 (void )data ;
5082
83+ OPERATE_RET rt = OPRT_OK ;
84+
5185 /* Add common default device meta here. Meta for other features may be added
5286 * after tuya_iot_init() and before tuya_iot_start(). */
53- tuya_device_meta_add_number ("timerCapability" , 1 );
54- tuya_device_meta_report ();
87+ rt = tal_workq_init_delayed (WORKQ_SYSTEM , __device_meta_event_workq , NULL , & s_meta .delayed_work );
88+ if (OPRT_OK != rt ) {
89+ PR_ERR ("init delayed work failed:%d" , rt );
90+ return -1 ;
91+ }
92+ rt = tal_workq_start_delayed (s_meta .delayed_work , 5 * 1000 , LOOP_CYCLE );
93+ if (OPRT_OK != rt ) {
94+ PR_ERR ("start delayed work failed:%d" , rt );
95+ return -1 ;
96+ }
5597
5698 return 0 ;
5799}
@@ -70,7 +112,7 @@ OPERATE_RET tuya_device_meta_init(void)
70112 return rt ;
71113 }
72114
73- tal_event_subscribe (EVENT_MQTT_CONNECTED , "tuya_device_meta" , __device_meta_event_cb ,
115+ tal_event_subscribe (EVENT_TIME_SYNC , "tuya_device_meta" , __device_meta_event_cb ,
74116 SUBSCRIBE_TYPE_ONETIME );
75117
76118 return OPRT_OK ;
@@ -228,7 +270,8 @@ OPERATE_RET tuya_device_meta_report(void)
228270 tal_mutex_lock (s_meta .mutex );
229271
230272 /* 1. Build payload: update timestamp and serialize */
231- cJSON * new_time = cJSON_CreateNumber ((double )tal_time_get_posix ());
273+ TIME_T timestamp = tal_time_get_posix ();
274+ cJSON * new_time = cJSON_CreateNumber ((double )timestamp );
232275 if (new_time == NULL ) {
233276 rt = OPRT_MALLOC_FAILED ;
234277 goto __EXIT ;
@@ -251,7 +294,7 @@ OPERATE_RET tuya_device_meta_report(void)
251294 atop_request .devid = client -> activate .devid ;
252295 atop_request .key = client -> activate .seckey ;
253296 atop_request .path = "/d.json" ;
254- atop_request .timestamp = (uint32_t )tal_time_get_posix () ;
297+ atop_request .timestamp = (uint32_t )timestamp ;
255298 atop_request .api = DEVICE_META_SAVE_API ;
256299 atop_request .version = "1.0" ;
257300 atop_request .data = buffer ;
0 commit comments