12
12
#include <esp_insights.h>
13
13
#include <string.h>
14
14
#include <esp_rmaker_core.h>
15
- #include <esp_rmaker_factory .h>
15
+ #include <esp_rmaker_common_events .h>
16
16
17
17
#if CONFIG_APP_INSIGHTS_ENABLE_LOG_TYPE_ALL
18
18
#define APP_INSIGHTS_LOG_TYPE ESP_DIAG_LOG_TYPE_ERROR \
24
24
25
25
esp_err_t esp_insights_enable (esp_insights_config_t * config );
26
26
27
+ #define INSIGHTS_TOPIC_SUFFIX "diagnostics/from-node"
28
+
29
+ static int app_insights_data_send (void * data , size_t len )
30
+ {
31
+ char topic [128 ];
32
+ int msg_id = -1 ;
33
+ if (data == NULL ) {
34
+ return 0 ;
35
+ }
36
+ char * node_id = esp_rmaker_get_node_id ();
37
+ if (!node_id ) {
38
+ return -1 ;
39
+ }
40
+ snprintf (topic , sizeof (topic ), "node/%s/%s" , node_id , INSIGHTS_TOPIC_SUFFIX );
41
+ esp_rmaker_mqtt_publish (topic , data , len , RMAKER_MQTT_QOS1 , & msg_id );
42
+ return msg_id ;
43
+ }
44
+
45
+ static void rmaker_common_event_handler (void * arg , esp_event_base_t event_base ,
46
+ int32_t event_id , void * event_data )
47
+ {
48
+ if (event_base != RMAKER_COMMON_EVENT ) {
49
+ return ;
50
+ }
51
+ esp_insights_transport_event_data_t data ;
52
+ switch (event_id ) {
53
+ case RMAKER_MQTT_EVENT_PUBLISHED :
54
+ memset (& data , 0 , sizeof (data ));
55
+ data .msg_id = * (int * )event_data ;
56
+ esp_event_post (INSIGHTS_EVENT , INSIGHTS_EVENT_TRANSPORT_SEND_SUCCESS , & data , sizeof (data ), portMAX_DELAY );
57
+ break ;
58
+ #ifdef CONFIG_MQTT_REPORT_DELETED_MESSAGES
59
+ case RMAKER_MQTT_EVENT_MSG_DELETED :
60
+ memset (& data , 0 , sizeof (data ));
61
+ data .msg_id = * (int * )event_data ;
62
+ esp_event_post (INSIGHTS_EVENT , INSIGHTS_EVENT_TRANSPORT_SEND_FAILED , & data , sizeof (data ), portMAX_DELAY );
63
+ break ;
64
+ #endif /* CONFIG_MQTT_REPORT_DELETED_MESSAGES */
65
+ default :
66
+ break ;
67
+ }
68
+ }
27
69
#endif /* CONFIG_ESP_INSIGHTS_ENABLED */
28
70
29
71
#define TAG "app_insights"
@@ -33,26 +75,19 @@ esp_err_t app_insights_enable(void)
33
75
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
34
76
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
35
77
ESP_LOGW (TAG , "Nodes with Self Claiming may not be accessible for Insights." );
36
- /* This is required so that the esp insights component will get correct
37
- * node id from NVS
38
- */
39
- char * node_id = esp_rmaker_get_node_id ();
40
- if (node_id ) {
41
- esp_rmaker_factory_set ("node_id" , node_id , strlen (node_id ));
42
- }
43
78
#endif
44
- esp_rmaker_mqtt_config_t mqtt_config = {
45
- .init = NULL ,
46
- .connect = NULL ,
47
- .disconnect = NULL ,
48
- .publish = esp_rmaker_mqtt_publish ,
49
- .subscribe = esp_rmaker_mqtt_subscribe ,
50
- .unsubscribe = esp_rmaker_mqtt_unsubscribe ,
79
+ char * node_id = esp_rmaker_get_node_id ();
80
+
81
+ esp_insights_transport_config_t transport = {
82
+ .callbacks .data_send = app_insights_data_send ,
51
83
};
52
- esp_insights_mqtt_setup (mqtt_config );
84
+ esp_insights_transport_register (& transport );
85
+
86
+ esp_event_handler_register (RMAKER_COMMON_EVENT , ESP_EVENT_ANY_ID , rmaker_common_event_handler , NULL );
53
87
54
88
esp_insights_config_t config = {
55
89
.log_type = APP_INSIGHTS_LOG_TYPE ,
90
+ .node_id = node_id ,
56
91
};
57
92
esp_insights_enable (& config );
58
93
#else
0 commit comments