Merge branch 'feature/rmaker_events' into 'master'

Add a framework for RainMaker Events

See merge request app-frameworks/esp-rainmaker!122
This commit is contained in:
Piyush Shah
2020-07-03 11:02:30 +08:00
4 changed files with 56 additions and 1 deletions

View File

@@ -15,11 +15,27 @@
#include <stdint.h>
#include <stdbool.h>
#include <esp_err.h>
#include <esp_event.h>
#define ESP_RMAKER_CONFIG_VERSION "2020-03-20"
#define MAX_VERSION_STRING_LEN 16
/** ESP RainMaker Event Base */
ESP_EVENT_DECLARE_BASE(RMAKER_EVENT);
/** ESP RainMaker Events */
typedef enum {
/** RainMaker Core Initialisation Done */
RMAKER_EVENT_INIT_DONE = 1,
/** Self Claiming Started */
RMAKER_EVENT_CLAIM_STARTED,
/** Self Claiming was Successful */
RMAKER_EVENT_CLAIM_SUCCESSFUL,
/** Self Claiming Failed */
RMAKER_EVENT_CLAIM_FAILED,
} esp_rmaker_event_t;
/** ESP RainMaker Node information */
typedef struct {
/** Name of the Node */

View File

@@ -35,6 +35,8 @@ static EventGroupHandle_t wifi_event_group;
#include <esp_wifi.h>
ESP_EVENT_DEFINE_BASE(RMAKER_EVENT);
#define ESP_CLAIM_NODE_ID_SIZE 12
static const char *TAG = "esp_rmaker_core";
@@ -221,6 +223,7 @@ esp_err_t esp_rmaker_init(esp_rmaker_config_t *config)
}
}
g_ra_handle->enable_time_sync = config->enable_time_sync;
esp_rmaker_post_event(RMAKER_EVENT_INIT_DONE, NULL, 0);
return ESP_OK;
}
@@ -749,11 +752,14 @@ static void esp_rmaker_task(void *param)
}
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
if (g_ra_handle->self_claim) {
esp_rmaker_post_event(RMAKER_EVENT_CLAIM_STARTED, NULL, 0);
err = esp_rmaker_self_claim_perform();
if (err != ESP_OK) {
esp_rmaker_post_event(RMAKER_EVENT_CLAIM_FAILED, NULL, 0);
ESP_LOGE(TAG, "esp_rmaker_self_claim_perform() returned %d. Aborting", err);
vTaskDelete(NULL);
}
esp_rmaker_post_event(RMAKER_EVENT_CLAIM_SUCCESSFUL, NULL, 0);
g_ra_handle->mqtt_config = esp_rmaker_get_mqtt_config();
if (!g_ra_handle->mqtt_config) {
ESP_LOGE(TAG, "Failed to initialise MQTT Config after claiming. Aborting");

View File

@@ -78,3 +78,7 @@ esp_err_t esp_rmaker_report_param(void);
esp_err_t esp_rmaker_queue_report_param(esp_rmaker_work_fn_t work_fn, void *priv_data);
esp_err_t esp_rmaker_param_get_stored_value(esp_rmaker_param_t *param, esp_rmaker_param_val_t *val);
esp_err_t esp_rmaker_param_store_value(esp_rmaker_param_t *param);
static inline esp_err_t esp_rmaker_post_event(esp_rmaker_event_t event_id, void* data, size_t data_size)
{
return esp_event_post(RMAKER_EVENT, event_id, data, data_size, portMAX_DELAY);
}

View File

@@ -11,6 +11,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include <esp_rmaker_core.h>
@@ -37,6 +38,31 @@ static esp_err_t switch_callback(const char *dev_name, const char *name, esp_rma
}
return ESP_OK;
}
/* Event handler for catching RainMaker events */
static void event_handler(void* arg, esp_event_base_t event_base,
int event_id, void* event_data)
{
if (event_base == RMAKER_EVENT) {
switch (event_id) {
case RMAKER_EVENT_INIT_DONE:
ESP_LOGI(TAG, "RainMaker Initialised.");
break;
case RMAKER_EVENT_CLAIM_STARTED:
ESP_LOGI(TAG, "RainMaker Claim Started.");
break;
case RMAKER_EVENT_CLAIM_SUCCESSFUL:
ESP_LOGI(TAG, "RainMaker Claim Successful.");
break;
case RMAKER_EVENT_CLAIM_FAILED:
ESP_LOGI(TAG, "RainMaker Claim Failed.");
break;
default:
ESP_LOGW(TAG, "Unhandled RainMaker Event: %d", event_id);
}
} else {
ESP_LOGW(TAG, "Invalid event received!");
}
}
void app_main()
{
@@ -58,7 +84,10 @@ void app_main()
/* Initialize Wi-Fi. Note that, this should be called before esp_rmaker_init()
*/
app_wifi_init();
/* Register an event handler to catch RainMaker events */
ESP_ERROR_CHECK(esp_event_handler_register(RMAKER_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
/* Initialize the ESP RainMaker Agent.
* Note that this should be called after app_wifi_init() but before app_wifi_start()
* */