resolved conflicts of JSON doc definitions

This commit is contained in:
strawmanbobi
2024-11-05 17:12:29 +08:00
parent 5e5da96c41
commit 6167940599
6 changed files with 160 additions and 138 deletions

View File

@@ -25,14 +25,32 @@
#include "global.h" #include "global.h"
StaticJsonDocument<1024> iris_msg_doc; StaticJsonDocument<512> http_request_doc;
StaticJsonDocument<1024> iris_ind_doc; StaticJsonDocument<512> http_response_doc;
// out solicited request message
StaticJsonDocument<512> mqtt_upstream_topic_msg_doc;
// out solicited response message
StaticJsonDocument<512> mqtt_upstream_topic_rsp_doc;
// out unsolicited message
StaticJsonDocument<512> mqtt_upstream_topic_ind_doc;
// out heartbeat message
StaticJsonDocument<512> mqtt_upstream_topic_hbt_doc;
// in message
StaticJsonDocument<512> mqtt_downstream_topic_msg_doc;
// inner json data
StaticJsonDocument<512> status_notify_doc;
StaticJsonDocument<2048> emit_code_doc;
// IR json data
StaticJsonDocument<1024> recv_msg_doc; StaticJsonDocument<1024> recv_msg_doc;
StaticJsonDocument<1024> send_msg_doc; StaticJsonDocument<1024> send_msg_doc;
StaticJsonDocument<1024> http_request_doc;
StaticJsonDocument<1024> http_response_doc;
StaticJsonDocument<2048> emit_code_doc;
StaticJsonDocument<1024> status_notify_doc;
WiFiManager wifi_manager; WiFiManager wifi_manager;
WiFiClient wifi_client; WiFiClient wifi_client;

View File

@@ -29,6 +29,17 @@
#include <WiFiClient.h> #include <WiFiClient.h>
/* goable json variable */ /* goable json variable */
extern StaticJsonDocument<512> http_request_doc;
extern StaticJsonDocument<512> http_response_doc;
extern StaticJsonDocument<512> mqtt_upstream_topic_msg_doc;
extern StaticJsonDocument<512> mqtt_upstream_topic_rsp_doc;
extern StaticJsonDocument<512> mqtt_upstream_topic_ind_doc;
extern StaticJsonDocument<512> mqtt_upstream_topic_hbt_doc;
extern StaticJsonDocument<512> mqtt_downstream_topic_msg_doc;
extern StaticJsonDocument<512> status_notify_doc;
extern StaticJsonDocument<2048> emit_code_doc;
extern StaticJsonDocument<1024> recv_msg_doc; extern StaticJsonDocument<1024> recv_msg_doc;
extern StaticJsonDocument<1024> send_msg_doc; extern StaticJsonDocument<1024> send_msg_doc;

View File

@@ -168,16 +168,16 @@ void sendStatus(String file, t_remote_ac_status status) {
} }
} }
void prepareRecvIR() { void prepareStudyIR() {
removeReceived(); removeReceived();
enableIRIn(); enableIRIn();
} }
void cancelRecvIR() { void cancelStudyIR() {
disableIRIn(); disableIRIn();
} }
int completeRecvIR(String &ir_data) { int completeStudyIR(String &ir_data) {
// called unsolicited // called unsolicited
disableIRIn(); disableIRIn();
return loadReceived(ir_data); return loadReceived(ir_data);
@@ -196,9 +196,9 @@ void recvIR() {
saveReceived(results); saveReceived(results);
processStatusChange(IRIS_KIT_STATUS_STUDIED, processStatusChange(IRIS_KIT_STATUS_STUDIED,
g_iris_kit_status.console_id, g_iris_kit_status.console_id,
g_iris_kit_status.remote_index,
g_iris_kit_status.key_id, g_iris_kit_status.key_id,
g_iris_kit_status.key_name, g_iris_kit_status.key_name);
g_iris_kit_status.remote_index);
} }
} }

View File

@@ -46,11 +46,11 @@ bool sendCommand(String file_name, int key);
void sendStatus(String file_name, t_remote_ac_status status); void sendStatus(String file_name, t_remote_ac_status status);
void prepareRecvIR(); void prepareStudyIR();
void cancelRecvIR(); void cancelStudyIR();
int completeRecvIR(String &ir_data); int completeStudyIR(String &ir_data);
void recvIR(); void recvIR();

View File

@@ -40,13 +40,6 @@
#include "iris_client.h" #include "iris_client.h"
extern StaticJsonDocument<1024> http_request_doc;
extern StaticJsonDocument<1024> http_response_doc;
extern StaticJsonDocument<1024> iris_msg_doc;
extern StaticJsonDocument<1024> iris_ind_doc;
extern StaticJsonDocument<2048> emit_code_doc;
extern StaticJsonDocument<1024> status_notify_doc;
extern String g_product_key; extern String g_product_key;
extern String g_device_name; extern String g_device_name;
extern String g_upstream_topic; extern String g_upstream_topic;
@@ -65,10 +58,10 @@ static String buildHeartBeat();
static void buildGeneralResponse(String notify_name); static void buildGeneralResponse(String notify_name);
static void buildGeneralIndication(String notify_name); static void buildGeneralIndication(String notify_name);
static String buildTestResponse(); static String buildTestResponse();
static String buildRecvPreparedResponse(); static String buildStudyPreparedResponse();
static String buildRecvCompletedIndication(String ir_data); static String buildStudyCompletedIndication(String ir_data);
static String buildRecvErrorIndication(); static String buildStudyErrorIndication();
static String buildRecvCancelledResponse(); static String buildStudyCancelledResponse();
static int handleConnected(String product_key, String device_name, String content); static int handleConnected(String product_key, String device_name, String content);
static int handleHartBeat(String product_key, String device_name, String content); static int handleHartBeat(String product_key, String device_name, String content);
static int handleEmit(String product_key, String device_name, String content); static int handleEmit(String product_key, String device_name, String content);
@@ -233,11 +226,11 @@ void handleIrisKitMessage(const char* data, int length) {
strncpy(payload, data, length); strncpy(payload, data, length);
payload[length] = '\0'; payload[length] = '\0';
INFOF("--> %s\n", payload); INFOF("--> %s\n", payload);
if (DeserializationError::Ok == deserializeJson(iris_ind_doc, payload)) { if (DeserializationError::Ok == deserializeJson(mqtt_downstream_topic_msg_doc, payload)) {
String event_name = iris_ind_doc["eventName"]; String event_name = mqtt_downstream_topic_msg_doc["eventName"];
String product_key = iris_ind_doc["productKey"]; String product_key = mqtt_downstream_topic_msg_doc["productKey"];
String device_name = iris_ind_doc["deviceName"]; String device_name = mqtt_downstream_topic_msg_doc["deviceName"];
String content = iris_ind_doc["content"]; String content = mqtt_downstream_topic_msg_doc["content"];
INFOF("Received ind : %s\n", event_name.c_str()); INFOF("Received ind : %s\n", event_name.c_str());
ret = processEvent(event_name.c_str(), product_key, device_name, content); ret = processEvent(event_name.c_str(), product_key, device_name, content);
INFOF("Event handle result = %d\n", ret); INFOF("Event handle result = %d\n", ret);
@@ -249,14 +242,14 @@ void handleIrisKitMessage(const char* data, int length) {
} }
} }
int processStatusChange(int status, int console_id, int key_id, String key_name, String remote_index) { int processStatusChange(int status, int console_id, String remote_index, int key_id, String key_name) {
switch(status) { switch(status) {
case IRIS_KIT_STATUS_READY_TO_STUDY: case IRIS_KIT_STATUS_READY_TO_STUDY:
{ {
// enter into IR receive mode and send response // enter into IR receive mode and send response
updateIrisKitStatus(IRIS_KIT_STATUS_READY_TO_STUDY, console_id, remote_index, key_id, key_name); updateIrisKitStatus(IRIS_KIT_STATUS_READY_TO_STUDY, console_id, remote_index, key_id, key_name);
prepareRecvIR(); prepareStudyIR();
String recvPreparedResponseData = buildRecvPreparedResponse(); String recvPreparedResponseData = buildStudyPreparedResponse();
sendData(g_upstream_topic.c_str(), (uint8_t*) recvPreparedResponseData.c_str(), recvPreparedResponseData.length()); sendData(g_upstream_topic.c_str(), (uint8_t*) recvPreparedResponseData.c_str(), recvPreparedResponseData.length());
break; break;
} }
@@ -265,21 +258,21 @@ int processStatusChange(int status, int console_id, int key_id, String key_name,
// after IR data are received, load saved IR data and send to IRIS server // after IR data are received, load saved IR data and send to IRIS server
updateIrisKitStatus(IRIS_KIT_STATUS_STUDIED, console_id, remote_index, key_id, key_name); updateIrisKitStatus(IRIS_KIT_STATUS_STUDIED, console_id, remote_index, key_id, key_name);
String ir_data = ""; String ir_data = "";
String recvCompletedIndicationData = ""; String studyCompletedIndicationData = "";
if (completeRecvIR(ir_data) > 0) { if (completeStudyIR(ir_data) > 0) {
recvCompletedIndicationData = buildRecvCompletedIndication(ir_data); studyCompletedIndicationData = buildStudyCompletedIndication(ir_data);
} else { } else {
recvCompletedIndicationData = buildRecvErrorIndication(); studyCompletedIndicationData = buildStudyErrorIndication();
} }
sendData(g_upstream_topic.c_str(), (uint8_t*) recvCompletedIndicationData.c_str(), recvCompletedIndicationData.length()); sendData(g_upstream_topic.c_str(), (uint8_t*) studyCompletedIndicationData.c_str(), studyCompletedIndicationData.length());
updateIrisKitStatus(IRIS_KIT_STATUS_UPLOADED, console_id, remote_index, key_id, key_name); updateIrisKitStatus(IRIS_KIT_STATUS_UPLOADED, console_id, remote_index, key_id, key_name);
break; break;
} }
case IRIS_KIT_STATUS_CANCEL_STUDY: case IRIS_KIT_STATUS_CANCEL_STUDY:
{ {
// cancel IR receiving and reset // cancel IR receiving and reset
cancelRecvIR(); cancelStudyIR();
String studyCancelledResponseData = buildRecvCancelledResponse(); String studyCancelledResponseData = buildStudyCancelledResponse();
sendData(g_upstream_topic.c_str(), (uint8_t*) studyCancelledResponseData.c_str(), studyCancelledResponseData.length()); sendData(g_upstream_topic.c_str(), (uint8_t*) studyCancelledResponseData.c_str(), studyCancelledResponseData.length());
resetIrisKitStatus(); resetIrisKitStatus();
break; break;
@@ -305,8 +298,8 @@ int processStatusChange(int status, int console_id, int key_id, String key_name,
void updateIrisKitStatus(status_t status, void updateIrisKitStatus(status_t status,
int console_id, int console_id,
String remote_index, String remote_index,
int key_id, String int key_id,
key_name) { String key_name) {
g_iris_kit_status.status = status; g_iris_kit_status.status = status;
g_iris_kit_status.console_id = console_id; g_iris_kit_status.console_id = console_id;
g_iris_kit_status.remote_index = remote_index; g_iris_kit_status.remote_index = remote_index;
@@ -324,6 +317,94 @@ void resetIrisKitStatus() {
// private function definitions // private function definitions
static String buildConnect() {
String connectMessage = "";
mqtt_upstream_topic_msg_doc.clear();
mqtt_upstream_topic_msg_doc["eventName"] = String(EVENT_NAME_CONNECT);
mqtt_upstream_topic_msg_doc["productKey"] = g_product_key;
mqtt_upstream_topic_msg_doc["deviceName"] = g_device_name;
serializeJson(mqtt_upstream_topic_msg_doc, connectMessage);
return connectMessage;
}
static String buildHeartBeat() {
String heartBeatMessage = "";
mqtt_upstream_topic_hbt_doc.clear();
mqtt_upstream_topic_hbt_doc["eventName"] = String(EVENT_HEART_BEAT_REQ);
mqtt_upstream_topic_hbt_doc["productKey"] = g_product_key;
mqtt_upstream_topic_hbt_doc["deviceName"] = g_device_name;
mqtt_upstream_topic_hbt_doc["appId"] = g_app_id;
serializeJson(mqtt_upstream_topic_hbt_doc, heartBeatMessage);
return heartBeatMessage;
}
static void buildGeneralMessageBody(String notify_name, StaticJsonDocument<512> &json_doc) {
json_doc.clear();
json_doc["productKey"] = g_product_key;
json_doc["deviceName"] = g_device_name;
json_doc["appId"] = g_app_id;
json_doc["consoleId"] = g_iris_kit_status.console_id;
json_doc["remoteIndex"] = g_iris_kit_status.remote_index;
json_doc["keyId"] = g_iris_kit_status.key_id;
json_doc["keyName"] = g_iris_kit_status.key_name;
json_doc["resp"] = String(notify_name);
}
static void buildGeneralResponse(String notify_name, StaticJsonDocument<512> &json_doc) {
buildGeneralMessageBody(notify_name, json_doc);
json_doc["eventName"] = String(EVENT_NOTIFY_RESP);
}
static void buildGeneralIndication(String notify_name, StaticJsonDocument<512> &json_doc) {
buildGeneralMessageBody(notify_name, json_doc);
json_doc["eventName"] = String(EVENT_INDICATION);
}
static String buildTestResponse() {
String testReponse = "";
buildGeneralResponse(NOTIFY_RESP_TEST, mqtt_upstream_topic_rsp_doc);
serializeJson(mqtt_upstream_topic_rsp_doc, testReponse);
return testReponse;
}
static String buildStudyPreparedResponse() {
String studyPreparedResponse = "";
buildGeneralResponse(NOTIFY_STUDY_PREPARED, mqtt_upstream_topic_rsp_doc);
serializeJson(mqtt_upstream_topic_rsp_doc, studyPreparedResponse);
return studyPreparedResponse;
}
static String buildStudyCompletedIndication(String ir_data) {
String studyCompletedIndication = "";
buildGeneralIndication(NOTIFY_STUDY_COMPLETED, mqtt_upstream_topic_ind_doc);
mqtt_upstream_topic_ind_doc["payload"] = ir_data;
serializeJson(mqtt_upstream_topic_ind_doc, studyCompletedIndication);
return studyCompletedIndication;
}
static String buildStudyErrorIndication() {
String studyErrorIndication = "";
buildGeneralIndication(NOTIFY_STUDY_ERROR, mqtt_upstream_topic_ind_doc);
serializeJson(mqtt_upstream_topic_ind_doc, studyErrorIndication);
return studyErrorIndication;
}
static String buildStudyCancelledResponse() {
String studyCancelledResponse = "";
buildGeneralResponse(NOTIFY_STUDY_CANCELLED, mqtt_upstream_topic_rsp_doc);
serializeJson(mqtt_upstream_topic_rsp_doc, studyCancelledResponse);
return studyCancelledResponse;
}
static int processEvent(String event_name, String product_key, String device_name, String content) { static int processEvent(String event_name, String product_key, String device_name, String content) {
int event_table_length = sizeof(event_handler_table) / sizeof(event_handler_table[0]); int event_table_length = sizeof(event_handler_table) / sizeof(event_handler_table[0]);
for (int i = 0; i < event_table_length; i++) { for (int i = 0; i < event_table_length; i++) {
@@ -335,96 +416,6 @@ static int processEvent(String event_name, String product_key, String device_nam
return -1; return -1;
} }
static String buildConnect() {
String connectMessage = "";
iris_msg_doc.clear();
iris_msg_doc["eventName"] = String(EVENT_NAME_CONNECT);
iris_msg_doc["productKey"] = g_product_key;
iris_msg_doc["deviceName"] = g_device_name;
serializeJson(iris_msg_doc, connectMessage);
return connectMessage;
}
static String buildHeartBeat() {
String heartBeatMessage = "";
iris_msg_doc.clear();
iris_msg_doc["eventName"] = String(EVENT_HEART_BEAT_REQ);
iris_msg_doc["productKey"] = g_product_key;
iris_msg_doc["deviceName"] = g_device_name;
iris_msg_doc["appId"] = g_app_id;
serializeJson(iris_msg_doc, heartBeatMessage);
return heartBeatMessage;
}
static void buildGeneralResponse(String notify_name) {
iris_msg_doc.clear();
iris_msg_doc["eventName"] = String(EVENT_NOTIFY_RESP);
iris_msg_doc["productKey"] = g_product_key;
iris_msg_doc["deviceName"] = g_device_name;
iris_msg_doc["appId"] = g_app_id;
iris_msg_doc["consoleId"] = g_iris_kit_status.console_id;
iris_msg_doc["resp"] = String(notify_name);
}
static void buildGeneralIndication(String notify_name) {
iris_ind_doc.clear();
iris_ind_doc["eventName"] = String(EVENT_NOTIFY_RESP);
iris_ind_doc["productKey"] = g_product_key;
iris_ind_doc["deviceName"] = g_device_name;
iris_ind_doc["appId"] = g_app_id;
iris_ind_doc["consoleId"] = g_iris_kit_status.console_id;
iris_ind_doc["remoteIndex"] = g_iris_kit_status.remote_index;
iris_ind_doc["keyId"] = g_iris_kit_status.key_id;
iris_ind_doc["keyName"] = g_iris_kit_status.key_name;
iris_ind_doc["resp"] = String(notify_name);
}
static String buildTestResponse() {
String testReponse = "";
buildGeneralResponse(NOTIFY_RESP_TEST);
serializeJson(iris_msg_doc, testReponse);
return testReponse;
}
static String buildRecvPreparedResponse() {
String recvPreparedResponse = "";
buildGeneralResponse(NOTIFY_RECV_PREPARED);
serializeJson(iris_msg_doc, recvPreparedResponse);
return recvPreparedResponse;
}
static String buildRecvCompletedIndication(String ir_data) {
String recvCompletedIndication = "";
buildGeneralIndication(NOTIFY_RECV_COMPLETED);
iris_ind_doc["payload"] = ir_data;
serializeJson(iris_ind_doc, recvCompletedIndication);
return recvCompletedIndication;
}
static String buildRecvErrorIndication() {
String recvErrorIndication = "";
buildGeneralIndication(NOTIFY_RECV_COMPLETED);
iris_ind_doc["payload"] = "error";
serializeJson(iris_ind_doc, recvErrorIndication);
return recvErrorIndication;
}
static String buildRecvCancelledResponse() {
String recvCancelledResponse = "";
buildGeneralResponse(NOTIFY_RECV_CANCELLED);
serializeJson(iris_msg_doc, recvCancelledResponse);
return recvCancelledResponse;
}
static int handleConnected(String product_key, String device_name, String content) { static int handleConnected(String product_key, String device_name, String content) {
return 0; return 0;
} }
@@ -462,7 +453,7 @@ static int handleNotifyStatus(String product_key, String device_name, String con
int console_id = status_notify_doc["consoleId"]; int console_id = status_notify_doc["consoleId"];
INFOF("Entering status : %d for %s(%d)\n", status, key_name.c_str(), key_id); INFOF("Entering status : %d for %s(%d)\n", status, key_name.c_str(), key_id);
processStatusChange(status, console_id, key_id, key_name, remote_index); processStatusChange(status, console_id, remote_index, key_id, key_name);
} else { } else {
INFOF("Deserialize failed\n"); INFOF("Deserialize failed\n");
} }

View File

@@ -64,11 +64,13 @@ typedef struct {
#define EVENT_NAME_CONNECT "__connect" #define EVENT_NAME_CONNECT "__connect"
#define EVENT_HEART_BEAT_REQ "__hb_request" #define EVENT_HEART_BEAT_REQ "__hb_request"
#define EVENT_NOTIFY_RESP "__notify_response" #define EVENT_NOTIFY_RESP "__notify_response"
#define EVENT_INDICATION "__indication"
#define NOTIFY_RESP_TEST "test_ok" #define NOTIFY_RESP_TEST "test_ok"
#define NOTIFY_RECV_PREPARED "recv_prepared" #define NOTIFY_STUDY_PREPARED "study_prepared"
#define NOTIFY_RECV_CANCELLED "recv_cancelled" #define NOTIFY_STUDY_CANCELLED "study_cancelled"
#define NOTIFY_RECV_COMPLETED "recv_completed" #define NOTIFY_STUDY_COMPLETED "study_completed"
#define NOTIFY_STUDY_ERROR "study_error"
typedef int (*eventHandler)(String, String, String); typedef int (*eventHandler)(String, String, String);
typedef struct { typedef struct {
@@ -96,9 +98,9 @@ void handleIrisKitMessage(const char* data, int length);
int processStatusChange(int status, int processStatusChange(int status,
int console_id, int console_id,
String remote_index,
int key_id, int key_id,
String key_name, String key_name);
String remote_index);
void updateIrisKitStatus(status_t status, void updateIrisKitStatus(status_t status,
int console_id, int console_id,