Skip to content

Commit d84f146

Browse files
committed
Added insights in rainmaker switch example
1 parent ace78d7 commit d84f146

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ set(LIBRARY_SRCS
9999
libraries/RainMaker/src/RMakerType.cpp
100100
libraries/RainMaker/src/RMakerQR.cpp
101101
libraries/RainMaker/src/RMakerUtils.cpp
102+
libraries/RainMaker/src/AppInsights.cpp
102103
libraries/SD_MMC/src/SD_MMC.cpp
103104
libraries/SD/src/SD.cpp
104105
libraries/SD/src/sd_diskio.cpp

Diff for: libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "RMaker.h"
33
#include "WiFi.h"
44
#include "WiFiProv.h"
5+
#include "AppInsights.h"
56

67
#define DEFAULT_POWER_MODE true
78
const char *service_name = "PROV_1234";
@@ -88,6 +89,9 @@ void setup()
8889
RMaker.enableSchedule();
8990

9091
RMaker.enableScenes();
92+
// Enable ESP Insights. Insteads of using the default http transport, this function will
93+
// reuse the existing MQTT connection of Rainmaker, thereby saving memory space.
94+
initAppInsights();
9195

9296
RMaker.start();
9397

Diff for: libraries/RainMaker/src/AppInsights.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "sdkconfig.h"
8+
#if defined(CONFIG_ESP_INSIGHTS_ENABLED) && defined(CONFIG_ESP_RMAKER_WORK_QUEUE_TASK_STACK)
9+
#include "Arduino.h"
10+
#include "AppInsights.h"
11+
#include "Insights.h"
12+
#include <esp_rmaker_mqtt.h>
13+
#include <esp_insights.h>
14+
#include <esp_diagnostics.h>
15+
#include <esp_rmaker_core.h>
16+
#include <esp_rmaker_common_events.h>
17+
#include <inttypes.h>
18+
19+
extern "C" {
20+
bool esp_rmaker_mqtt_is_budget_available();
21+
}
22+
23+
#define INSIGHTS_TOPIC_SUFFIX "diagnostics/from-node"
24+
#define INSIGHTS_TOPIC_RULE "insights_message_delivery"
25+
26+
static void _rmakerCommonEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
27+
{
28+
if (event_base != RMAKER_COMMON_EVENT) {
29+
return;
30+
}
31+
esp_insights_transport_event_data_t data;
32+
switch(event_id) {
33+
case RMAKER_MQTT_EVENT_PUBLISHED:
34+
memset(&data, 0, sizeof(data));
35+
data.msg_id = *(int *)event_data;
36+
esp_event_post(INSIGHTS_EVENT, INSIGHTS_EVENT_TRANSPORT_SEND_SUCCESS, &data, sizeof(data), portMAX_DELAY);
37+
break;
38+
default:
39+
break;
40+
}
41+
}
42+
43+
static int _appInsightsDataSend(void *data, size_t len)
44+
{
45+
char topic[128];
46+
int msg_id = -1;
47+
if (data == NULL) {
48+
return 0;
49+
}
50+
char *node_id = esp_rmaker_get_node_id();
51+
if (!node_id) {
52+
return -1;
53+
}
54+
if (esp_rmaker_mqtt_is_budget_available() == false) {
55+
return ESP_FAIL;
56+
}
57+
esp_rmaker_create_mqtt_topic(topic, sizeof(topic), INSIGHTS_TOPIC_SUFFIX, INSIGHTS_TOPIC_RULE);
58+
esp_rmaker_mqtt_publish(topic, data, len, RMAKER_MQTT_QOS1, &msg_id);
59+
return msg_id;
60+
}
61+
62+
bool initAppInsights(uint32_t log_type, bool alloc_ext_ram)
63+
{
64+
char *node_id = esp_rmaker_get_node_id();
65+
esp_insights_transport_config_t transport;
66+
transport.userdata = NULL;
67+
transport.callbacks.data_send = _appInsightsDataSend;
68+
transport.callbacks.init = NULL;
69+
transport.callbacks.deinit = NULL;
70+
transport.callbacks.connect = NULL;
71+
transport.callbacks.disconnect = NULL;
72+
esp_insights_transport_register(&transport);
73+
esp_event_handler_register(RMAKER_COMMON_EVENT, ESP_EVENT_ANY_ID, _rmakerCommonEventHandler, NULL);
74+
return Insights.begin(NULL, node_id, log_type, alloc_ext_ram, false);
75+
}
76+
#else
77+
bool initAppInsights(uint32_t log_type, bool alloc_ext_ram)
78+
{
79+
return false;
80+
}
81+
#endif

Diff for: libraries/RainMaker/src/AppInsights.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
#include "sdkconfig.h"
9+
#include "Arduino.h"
10+
#include "inttypes.h"
11+
12+
bool initAppInsights(uint32_t log_type = 0xffffffff, bool alloc_ext_ram = false);

0 commit comments

Comments
 (0)