some refactoring and optimization in mqtt and rest api handler
This commit is contained in:
parent
f5f28eb209
commit
fb552427d6
|
|
@ -97,7 +97,7 @@ typedef struct
|
||||||
esp_err_t GetConfVar(char* name, char* val, rest_var_types *tp);
|
esp_err_t GetConfVar(char* name, char* val, rest_var_types *tp);
|
||||||
esp_err_t SetConfVar(char* name, char* val, rest_var_types *tp);
|
esp_err_t SetConfVar(char* name, char* val, rest_var_types *tp);
|
||||||
|
|
||||||
esp_err_t SysServiceDataHandler(data_message_t *MSG);
|
esp_err_t ServiceDataHandler(data_message_t *MSG);
|
||||||
sys_error_code SysVarsPayloadHandler(data_message_t *MSG);
|
sys_error_code SysVarsPayloadHandler(data_message_t *MSG);
|
||||||
void GetSysErrorDetales(sys_error_code err, const char **br, const char **ds);
|
void GetSysErrorDetales(sys_error_code err, const char **br, const char **ds);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ HTTP_IO_RESULT HTTPPostSysAPI(httpd_req_t *req, char *PostData)
|
||||||
M.chlidx = 100;
|
M.chlidx = 100;
|
||||||
M.outputDataBuffer = respbuf;
|
M.outputDataBuffer = respbuf;
|
||||||
M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE;
|
M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE;
|
||||||
SysServiceDataHandler(&M);
|
ServiceDataHandler(&M);
|
||||||
httpd_resp_set_type(req, "application/json");
|
httpd_resp_set_type(req, "application/json");
|
||||||
httpd_resp_sendstr(req, respbuf);
|
httpd_resp_sendstr(req, respbuf);
|
||||||
free(respbuf);
|
free(respbuf);
|
||||||
|
|
|
||||||
43
src/MQTT.c
43
src/MQTT.c
|
|
@ -26,6 +26,11 @@
|
||||||
#include "MQTT.h"
|
#include "MQTT.h"
|
||||||
#include "UserCallbacks.h"
|
#include "UserCallbacks.h"
|
||||||
|
|
||||||
|
#define TAG "MQTT"
|
||||||
|
#define SERVICE_NAME "SYSTEM" // Dedicated service name
|
||||||
|
#define UPLINK_SUBTOPIC "UPLINK" // Device publish to this topic
|
||||||
|
#define DOWNLINK_SUBTOPIC "DWLINK" // Device listen from this topic
|
||||||
|
|
||||||
#define MQTT_DEBUG_MODE CONFIG_WEBGUIAPP_MQTT_DEBUG_LEVEL
|
#define MQTT_DEBUG_MODE CONFIG_WEBGUIAPP_MQTT_DEBUG_LEVEL
|
||||||
|
|
||||||
#define MQTT_MESSAGE_BUFER_LENTH 5 //size of mqtt queue
|
#define MQTT_MESSAGE_BUFER_LENTH 5 //size of mqtt queue
|
||||||
|
|
@ -47,7 +52,7 @@ uint8_t MQTT2MessagesQueueStorageArea[MQTT_MESSAGE_BUFER_LENTH * sizeof(MQTT_DAT
|
||||||
|
|
||||||
mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 };
|
mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 };
|
||||||
|
|
||||||
#define TAG "MQTTApp"
|
|
||||||
|
|
||||||
static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_base_t base, int32_t event_id,
|
static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_base_t base, int32_t event_id,
|
||||||
void *event_data);
|
void *event_data);
|
||||||
|
|
@ -119,7 +124,7 @@ esp_err_t SysServiceMQTTSend(char *data, int len, int idx)
|
||||||
{
|
{
|
||||||
memcpy(buf, data, len);
|
memcpy(buf, data, len);
|
||||||
MQTT_DATA_SEND_STRUCT DSS;
|
MQTT_DATA_SEND_STRUCT DSS;
|
||||||
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
|
ComposeTopic(DSS.topic, idx, SERVICE_NAME, UPLINK_SUBTOPIC);
|
||||||
DSS.raw_data_ptr = buf;
|
DSS.raw_data_ptr = buf;
|
||||||
DSS.data_length = len;
|
DSS.data_length = len;
|
||||||
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
|
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
|
||||||
|
|
@ -151,32 +156,16 @@ mqtt_app_err_t PublicTestMQTT(int idx)
|
||||||
strcat(resp, ":");
|
strcat(resp, ":");
|
||||||
strcat(resp, tmp);
|
strcat(resp, tmp);
|
||||||
jwObj_string(&jwc, "url", resp);
|
jwObj_string(&jwc, "url", resp);
|
||||||
ComposeTopic(resp, idx, "SYSTEM", "UPLINK");
|
ComposeTopic(resp, idx, SERVICE_NAME, UPLINK_SUBTOPIC);
|
||||||
jwObj_string(&jwc, "tx_topic", resp);
|
jwObj_string(&jwc, "tx_topic", resp);
|
||||||
ComposeTopic(resp, idx, "SYSTEM", "DWLINK");
|
ComposeTopic(resp, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
|
||||||
jwObj_string(&jwc, "rx_topic", resp);
|
jwObj_string(&jwc, "rx_topic", resp);
|
||||||
jwEnd(&jwc);
|
jwEnd(&jwc);
|
||||||
jwClose(&jwc);
|
jwClose(&jwc);
|
||||||
char *buf = (char*) malloc(strlen(JSONMess) + 1);
|
mqtt_app_err_t merr = API_OK;
|
||||||
if (buf)
|
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
|
||||||
{
|
merr = API_INTERNAL_ERR;
|
||||||
memcpy(buf, JSONMess, strlen(JSONMess));
|
return merr;
|
||||||
MQTT_DATA_SEND_STRUCT DSS;
|
|
||||||
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
|
|
||||||
DSS.raw_data_ptr = buf;
|
|
||||||
DSS.data_length = strlen(JSONMess);
|
|
||||||
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
|
|
||||||
return API_OK;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
free(buf);
|
|
||||||
return API_INTERNAL_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ // ERR internal error on publish error
|
|
||||||
return API_INTERNAL_ERR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_base_t base, int32_t event_id,
|
static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_base_t base, int32_t event_id,
|
||||||
|
|
@ -200,7 +189,7 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
|
||||||
#if MQTT_DEBUG_MODE > 0
|
#if MQTT_DEBUG_MODE > 0
|
||||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED client %d", idx);
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED client %d", idx);
|
||||||
#endif
|
#endif
|
||||||
ComposeTopic(topic, idx, "SYSTEM", "DWLINK");
|
ComposeTopic(topic, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
|
||||||
msg_id = esp_mqtt_client_subscribe(client, (const char*) topic, 0);
|
msg_id = esp_mqtt_client_subscribe(client, (const char*) topic, 0);
|
||||||
#if MQTT_DEBUG_MODE > 0
|
#if MQTT_DEBUG_MODE > 0
|
||||||
ESP_LOGI(TAG, "Subscribe to %s", topic);
|
ESP_LOGI(TAG, "Subscribe to %s", topic);
|
||||||
|
|
@ -242,7 +231,7 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
|
||||||
ESP_LOGI(TAG, "MQTT_EVENT_DATA, client %d", idx);
|
ESP_LOGI(TAG, "MQTT_EVENT_DATA, client %d", idx);
|
||||||
#endif
|
#endif
|
||||||
//Check if topic is SYSTEM and pass data to handler
|
//Check if topic is SYSTEM and pass data to handler
|
||||||
ComposeTopic(topic, idx, "SYSTEM", "DWLINK");
|
ComposeTopic(topic, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
|
||||||
if (!memcmp(topic, event->topic, event->topic_len))
|
if (!memcmp(topic, event->topic, event->topic_len))
|
||||||
{
|
{
|
||||||
//SystemDataHandler(event->data, event->data_len, idx); //Old API
|
//SystemDataHandler(event->data, event->data_len, idx); //Old API
|
||||||
|
|
@ -255,7 +244,7 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
|
||||||
M.chlidx = idx;
|
M.chlidx = idx;
|
||||||
M.outputDataBuffer = respbuf;
|
M.outputDataBuffer = respbuf;
|
||||||
M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE;
|
M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE;
|
||||||
SysServiceDataHandler(&M);
|
ServiceDataHandler(&M);
|
||||||
SysServiceMQTTSend(M.outputDataBuffer, strlen(M.outputDataBuffer), idx);
|
SysServiceMQTTSend(M.outputDataBuffer, strlen(M.outputDataBuffer), idx);
|
||||||
free(respbuf);
|
free(respbuf);
|
||||||
#if(MQTT_DEBUG_MODE > 0)
|
#if(MQTT_DEBUG_MODE > 0)
|
||||||
|
|
|
||||||
|
|
@ -35,18 +35,6 @@
|
||||||
"wifi_mode":"",
|
"wifi_mode":"",
|
||||||
"wifi_sta_ip":"",
|
"wifi_sta_ip":"",
|
||||||
"wifi_sta_mask":"",
|
"wifi_sta_mask":"",
|
||||||
"wifi_sta_gw":"",
|
|
||||||
"wifi_ap_ip":"",
|
|
||||||
"wifi_dns1":"",
|
|
||||||
"wifi_dns2":"",
|
|
||||||
"wifi_dns3":"",
|
|
||||||
"wifi_sta_ssid":"",
|
|
||||||
"wifi_sta_key":"",
|
|
||||||
"wifi_ap_ssid":"",
|
|
||||||
"wifi_ap_key":"",
|
|
||||||
"wifi_enab":"",
|
|
||||||
"wifi_isdhcp":"",
|
|
||||||
"wifi_power":""
|
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
"signature":"6a11b872e8f766673eb82e127b6918a0dc96a42c5c9d184604f9787f3d27bcef"}
|
"signature":"6a11b872e8f766673eb82e127b6918a0dc96a42c5c9d184604f9787f3d27bcef"}
|
||||||
|
|
@ -59,9 +47,6 @@
|
||||||
#define TAG "SysComm"
|
#define TAG "SysComm"
|
||||||
|
|
||||||
#define MAX_JSON_DATA_SIZE 1024
|
#define MAX_JSON_DATA_SIZE 1024
|
||||||
|
|
||||||
//sys_error_code SysPayloadTypeVarsHandler(data_message_t *MSG)
|
|
||||||
|
|
||||||
sys_error_code (*CustomPayloadTypeHandler)(data_message_t *MSG);
|
sys_error_code (*CustomPayloadTypeHandler)(data_message_t *MSG);
|
||||||
|
|
||||||
void regCustomPayloadTypeHandler(sys_error_code (*payload_handler)(data_message_t *MSG))
|
void regCustomPayloadTypeHandler(sys_error_code (*payload_handler)(data_message_t *MSG))
|
||||||
|
|
@ -86,17 +71,8 @@ static esp_err_t SHA256hmacHash(unsigned char *data,
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
static void Timestamp(char *ts)
|
|
||||||
{
|
|
||||||
struct timeval tp;
|
|
||||||
gettimeofday(&tp, NULL);
|
|
||||||
unsigned long long ms = (((unsigned long long) tp.tv_sec) * 1000000 + tp.tv_usec);
|
|
||||||
sprintf(ts, "%llu", ms);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static sys_error_code SysPayloadTypeVarsHandler(data_message_t *MSG)
|
static sys_error_code PayloadType_1_Handler(data_message_t *MSG)
|
||||||
{
|
{
|
||||||
struct jReadElement result;
|
struct jReadElement result;
|
||||||
const char *err_br;
|
const char *err_br;
|
||||||
|
|
@ -222,7 +198,7 @@ static sys_error_code SysPayloadTypeVarsHandler(data_message_t *MSG)
|
||||||
return SYS_OK_DATA;
|
return SYS_OK_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
static sys_error_code SysDataParser(data_message_t *MSG)
|
static sys_error_code DataHeaderParser(data_message_t *MSG)
|
||||||
{
|
{
|
||||||
struct jReadElement result;
|
struct jReadElement result;
|
||||||
jRead(MSG->inputDataBuffer, "", &result);
|
jRead(MSG->inputDataBuffer, "", &result);
|
||||||
|
|
@ -297,8 +273,7 @@ static sys_error_code SysDataParser(data_message_t *MSG)
|
||||||
switch (MSG->parsedData.payloadType)
|
switch (MSG->parsedData.payloadType)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
//MSG->parsedData.payload = malloc(sizeof(payload_type_vars)); Not needed for this case
|
return PayloadType_1_Handler(MSG);
|
||||||
return SysPayloadTypeVarsHandler(MSG);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -311,9 +286,9 @@ static sys_error_code SysDataParser(data_message_t *MSG)
|
||||||
return SYS_ERROR_UNKNOWN;
|
return SYS_ERROR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t SysServiceDataHandler(data_message_t *MSG)
|
esp_err_t ServiceDataHandler(data_message_t *MSG)
|
||||||
{
|
{
|
||||||
MSG->err_code = (int) SysDataParser(MSG);
|
MSG->err_code = (int) DataHeaderParser(MSG);
|
||||||
if (MSG->err_code)
|
if (MSG->err_code)
|
||||||
{
|
{
|
||||||
struct jWriteControl jwc;
|
struct jWriteControl jwc;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user