diff --git a/esp8285/platformio.ini b/esp8285/platformio.ini index e503b0f..befbda7 100644 --- a/esp8285/platformio.ini +++ b/esp8285/platformio.ini @@ -27,6 +27,7 @@ build_flags = -DLOG_DEBUG=1 -DLOG_INFO=1 -DLOG_ERROR=1 + -DSTORE_RECEIVED_IR_DATA=1 -I${common.workspace}/lib/irext/include [env:esp8266-1m-base] diff --git a/esp8285/src/global.cpp b/esp8285/src/global.cpp index b6627ef..799fd78 100644 --- a/esp8285/src/global.cpp +++ b/esp8285/src/global.cpp @@ -35,7 +35,7 @@ StaticJsonDocument<512> mqtt_upstream_topic_msg_doc; StaticJsonDocument<512> mqtt_upstream_topic_rsp_doc; // out unsolicited message -StaticJsonDocument<512> mqtt_upstream_topic_ind_doc; +StaticJsonDocument<2048> mqtt_upstream_topic_ind_doc; // out heartbeat message StaticJsonDocument<512> mqtt_upstream_topic_hbt_doc; diff --git a/esp8285/src/global.h b/esp8285/src/global.h index 94713db..9db6f5b 100644 --- a/esp8285/src/global.h +++ b/esp8285/src/global.h @@ -33,7 +33,7 @@ extern StaticJsonDocument<1024> http_request_doc; extern StaticJsonDocument<1024> 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<2048> 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; diff --git a/esp8285/src/ir_drv_ctrl.cpp b/esp8285/src/ir_drv_ctrl.cpp index e297146..4e6fd45 100644 --- a/esp8285/src/ir_drv_ctrl.cpp +++ b/esp8285/src/ir_drv_ctrl.cpp @@ -172,7 +172,9 @@ void sendStatus(String file, t_remote_ac_status status) { } void prepareStudyIR() { +#if defined STORE_RECEIVED_IR_DATA removeReceived(); +#endif enableIRIn(); } @@ -183,19 +185,39 @@ void cancelStudyIR() { int completeStudyIR(String &ir_data) { // called unsolicited disableIRIn(); - return loadReceived(ir_data); +#if defined STORE_RECEIVED_IR_DATA + loadReceived(ir_data); +#else + ir_data = g_recv_ir_code_str; +#endif + DEBUGF("loaded received code : %s\n", ir_data.c_str()); + + return ir_data.length(); } void recvIR() { + int max_time_slice = 0; + int time_slice = 0; if (ir_recv->decode(&g_recv_results)) { INFOF("Recv IR, raw length = %d\n", g_recv_results.rawlen - 1); - String raw_data; + g_recv_ir_code_str.clear(); for (int i = 1; i < g_recv_results.rawlen; i++) { - raw_data += String(*(g_recv_results.rawbuf + i) * kRawTick) + ","; + time_slice = *(g_recv_results.rawbuf + i) * kRawTick; + if (time_slice > max_time_slice) { + max_time_slice = time_slice; + } + g_recv_ir_code_str += String(time_slice) + ","; } + g_recv_ir_code_str += String(3 * max_time_slice); + ir_recv->resume(); - INFOLN(raw_data.c_str()); - saveReceived(g_recv_results); + + DEBUGLN(g_recv_ir_code_str.c_str()); + +#if defined STORE_RECEIVED_IR_DATA + saveReceived(g_recv_ir_code_str); +#endif + processStatusChange(IRIS_KIT_STATUS_STUDIED, g_iris_kit_status.console_id, g_iris_kit_status.remote_index, @@ -204,17 +226,14 @@ void recvIR() { } } -bool saveReceived(decode_results& results) { +bool saveReceived(String& ir_data) { String save_path = SAVE_PATH; String file_name = ""; - uint16_t max_time_slice = 0; - char time_slice[16] = { 0 }; if (g_iris_kit_status.remote_index.isEmpty()) { return false; } -#if defined STORE_RECEIVED_TO_TMP_FILE file_name = "ir_" + g_iris_kit_status.remote_index + RECEIVED_SUFFIX; save_path += file_name; INFOF("Save received code to: %s\n", save_path.c_str()); @@ -223,25 +242,9 @@ bool saveReceived(decode_results& results) { ERRORF("Failed to create file\n"); return false; } -#endif - for (size_t i = 0; i < results.rawlen; i++) { - memset(time_slice, 0, sizeof(time_slice)); - if (*(results.rawbuf + i) > max_time_slice) { - max_time_slice = *(results.rawbuf + i); - } - snprintf(time_slice, 15, "%d", *(results.rawbuf + i)); - g_recv_ir_code_str += String(time_slice) + ","; - } - // append tail code - max_time_slice = 2 * max_time_slice; - snprintf(time_slice, 15, "%d", max_time_slice); - g_recv_ir_code_str += String(time_slice); - -#if defined STORE_RECEIVED_TO_TMP_FILE - cache.write(g_recv_ir_code_str.c_str(), g_recv_ir_code_str.length()); + cache.write(ir_data.c_str(), ir_data.length()); cache.close(); -#endif return true; } @@ -254,12 +257,10 @@ bool removeReceived() { return false; } -#if defined STORE_RECEIVED_TO_TMP_FILE file_name = "ir_" + g_iris_kit_status.remote_index + RECEIVED_SUFFIX; save_path += file_name; INFOF("Delete received code file: %s\n", save_path.c_str()); LittleFS.remove(save_path); -#endif return true; } @@ -272,10 +273,9 @@ int loadReceived(String &ir_data) { return -1; } -#if defined STORE_RECEIVED_TO_TMP_FILE file_name = "ir_" + g_iris_kit_status.remote_index + RECEIVED_SUFFIX; save_path += file_name; - INFOF("Load received code from: %s\n", save_path.c_str()); + DEBUGF("Load received code from: %s\n", save_path.c_str()); File cache = LittleFS.open(save_path, "r"); if (!cache) { @@ -284,9 +284,8 @@ int loadReceived(String &ir_data) { } ir_data = cache.readString(); cache.close(); -#else - ir_data = g_recv_ir_code_str; -#endif + + DEBUGF("received code : %s\n", ir_data.c_str()); return ir_data.length(); } diff --git a/esp8285/src/ir_drv_ctrl.h b/esp8285/src/ir_drv_ctrl.h index 95ed037..e938e56 100644 --- a/esp8285/src/ir_drv_ctrl.h +++ b/esp8285/src/ir_drv_ctrl.h @@ -54,7 +54,7 @@ int completeStudyIR(String &ir_data); void recvIR(); -bool saveReceived(decode_results &results); +bool saveReceived(String &results); bool removeReceived(); diff --git a/esp8285/src/iris_client.cpp b/esp8285/src/iris_client.cpp index 18d6706..dd2cb8b 100644 --- a/esp8285/src/iris_client.cpp +++ b/esp8285/src/iris_client.cpp @@ -57,11 +57,12 @@ static String buildConnect(); static String buildHeartBeat(); static void buildGeneralResponse(String notify_name); static void buildGeneralIndication(String notify_name); -static String buildTestResponse(); -static String buildStudyPreparedResponse(); -static String buildStudyCompletedIndication(String ir_data); -static String buildStudyErrorIndication(); -static String buildStudyCancelledResponse(); +static String buildTestResponse(int console_id); +static String buildStudyPreparedResponse(int console_id, String remote_index, int key_id, String key_name); +static String buildStudyCompletedIndication(String ir_data, + int console_id, String remote_index, int key_id, String key_name); +static String buildStudyErrorIndication(int console_id, String remote_index, int key_id, String key_name); +static String buildStudyCancelledResponse(int console_id, String remote_index, int key_id, String key_name); static int handleConnected(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); @@ -249,7 +250,7 @@ int processStatusChange(int status, int console_id, String remote_index, int key // enter into IR receive mode and send response updateIrisKitStatus(IRIS_KIT_STATUS_READY_TO_STUDY, console_id, remote_index, key_id, key_name); prepareStudyIR(); - String recvPreparedResponseData = buildStudyPreparedResponse(); + String recvPreparedResponseData = buildStudyPreparedResponse(console_id, remote_index, key_id, key_name); sendData(g_upstream_topic.c_str(), (uint8_t*) recvPreparedResponseData.c_str(), recvPreparedResponseData.length()); break; } @@ -260,9 +261,9 @@ int processStatusChange(int status, int console_id, String remote_index, int key String ir_data = ""; String studyCompletedIndicationData = ""; if (completeStudyIR(ir_data) > 0) { - studyCompletedIndicationData = buildStudyCompletedIndication(ir_data); + studyCompletedIndicationData = buildStudyCompletedIndication(ir_data, console_id, remote_index, key_id, key_name); } else { - studyCompletedIndicationData = buildStudyErrorIndication(); + studyCompletedIndicationData = buildStudyErrorIndication(console_id, remote_index, key_id, key_name); } 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); @@ -272,7 +273,7 @@ int processStatusChange(int status, int console_id, String remote_index, int key { // cancel IR receiving and reset cancelStudyIR(); - String studyCancelledResponseData = buildStudyCancelledResponse(); + String studyCancelledResponseData = buildStudyCancelledResponse(console_id, remote_index, key_id, key_name); sendData(g_upstream_topic.c_str(), (uint8_t*) studyCancelledResponseData.c_str(), studyCancelledResponseData.length()); resetIrisKitStatus(); break; @@ -281,7 +282,7 @@ int processStatusChange(int status, int console_id, String remote_index, int key { // send response for test notification updateIrisKitStatus(IRIS_KIT_STATUS_TEST, console_id, remote_index, key_id, key_name); - String testResponseData = buildTestResponse(); + String testResponseData = buildTestResponse(console_id); sendData(g_upstream_topic.c_str(), (uint8_t*) testResponseData.c_str(), testResponseData.length()); resetIrisKitStatus(); break; @@ -342,64 +343,75 @@ static String buildHeartBeat() { return heartBeatMessage; } -static void buildGeneralMessageBody(String notify_name, StaticJsonDocument<512> &json_doc) { +static void buildGeneralResponse(String notify_name, StaticJsonDocument<512> &json_doc, + int console_id, String remote_index, int key_id, String key_name) { 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["consoleId"] = console_id; + json_doc["remoteIndex"] = remote_index; + json_doc["keyId"] = key_id; + json_doc["keyName"] = 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); +static void buildGeneralIndication(String notify_name, StaticJsonDocument<2048> &json_doc, + int console_id, String remote_index, int key_id, String key_name) { + json_doc.clear(); + json_doc["productKey"] = g_product_key; + json_doc["deviceName"] = g_device_name; + json_doc["appId"] = g_app_id; + json_doc["consoleId"] = console_id; + json_doc["remoteIndex"] = remote_index; + json_doc["keyId"] = key_id; + json_doc["keyName"] = key_name; + json_doc["resp"] = String(notify_name); json_doc["eventName"] = String(EVENT_INDICATION); } -static String buildTestResponse() { +static String buildTestResponse(int console_id) { String testReponse = ""; - buildGeneralResponse(NOTIFY_RESP_TEST, mqtt_upstream_topic_rsp_doc); + buildGeneralResponse(NOTIFY_RESP_TEST, mqtt_upstream_topic_rsp_doc, console_id, "", 0, ""); serializeJson(mqtt_upstream_topic_rsp_doc, testReponse); return testReponse; } -static String buildStudyPreparedResponse() { +static String buildStudyPreparedResponse(int console_id, String remote_index, int key_id, String key_name) { String studyPreparedResponse = ""; - buildGeneralResponse(NOTIFY_STUDY_PREPARED, mqtt_upstream_topic_rsp_doc); + buildGeneralResponse(NOTIFY_STUDY_PREPARED, mqtt_upstream_topic_rsp_doc, + console_id, remote_index, key_id, key_name); serializeJson(mqtt_upstream_topic_rsp_doc, studyPreparedResponse); return studyPreparedResponse; } -static String buildStudyCompletedIndication(String ir_data) { +static String buildStudyCompletedIndication(String ir_data, + int console_id, String remote_index, int key_id, String key_name) { String studyCompletedIndication = ""; - buildGeneralIndication(NOTIFY_STUDY_COMPLETED, mqtt_upstream_topic_ind_doc); + buildGeneralIndication(NOTIFY_STUDY_COMPLETED, mqtt_upstream_topic_ind_doc, + console_id, remote_index, key_id, key_name); mqtt_upstream_topic_ind_doc["payload"] = ir_data; serializeJson(mqtt_upstream_topic_ind_doc, studyCompletedIndication); return studyCompletedIndication; } -static String buildStudyErrorIndication() { +static String buildStudyErrorIndication(int console_id, String remote_index, int key_id, String key_name) { String studyErrorIndication = ""; - buildGeneralIndication(NOTIFY_STUDY_ERROR, mqtt_upstream_topic_ind_doc); + buildGeneralIndication(NOTIFY_STUDY_ERROR, mqtt_upstream_topic_ind_doc, + console_id, remote_index, key_id, key_name); serializeJson(mqtt_upstream_topic_ind_doc, studyErrorIndication); return studyErrorIndication; } -static String buildStudyCancelledResponse() { +static String buildStudyCancelledResponse(int console_id, String remote_index, int key_id, String key_name) { String studyCancelledResponse = ""; - buildGeneralResponse(NOTIFY_STUDY_CANCELLED, mqtt_upstream_topic_rsp_doc); + buildGeneralResponse(NOTIFY_STUDY_CANCELLED, mqtt_upstream_topic_rsp_doc, + console_id, remote_index, key_id, key_name); serializeJson(mqtt_upstream_topic_rsp_doc, studyCancelledResponse); return studyCancelledResponse; diff --git a/esp8285/src/iris_kit.cpp b/esp8285/src/iris_kit.cpp index 342ca0e..cf7e867 100644 --- a/esp8285/src/iris_kit.cpp +++ b/esp8285/src/iris_kit.cpp @@ -100,7 +100,7 @@ void setup() { INFOF("██║██╔══██╗██║╚════██║\n"); INFOF("██║██║ ██║██║███████║\n"); INFOF("╚═╝╚═╝ ╚═╝╚═╝╚══════╝\n"); - INFOF("== IRIS Kit [1.3.1_r1] Powered by AliyunIoT ==\n"); + INFOF("== IRIS Kit [1.4.0_r1] Powered by AliyunIoT ==\n"); // try loading saved iriskit settings iriskit_settings.credential_token.clear();