From 8e3d25fea54f9f1716c2714194cfe58013de7282 Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Wed, 23 Oct 2024 17:48:02 +0800 Subject: [PATCH] updated message handlers --- esp8285/src/{ir_emit.cpp => ir_drv_ctrl.cpp} | 43 +++++++++---- esp8285/src/{ir_emit.h => ir_drv_ctrl.h} | 16 +++-- esp8285/src/iris_client.cpp | 63 ++++++++++++++++---- esp8285/src/iris_client.h | 3 + esp8285/src/iris_kit.cpp | 9 ++- esp8285/src/user_settings.cpp | 2 +- 6 files changed, 105 insertions(+), 31 deletions(-) rename esp8285/src/{ir_emit.cpp => ir_drv_ctrl.cpp} (91%) rename esp8285/src/{ir_emit.h => ir_drv_ctrl.h} (87%) diff --git a/esp8285/src/ir_emit.cpp b/esp8285/src/ir_drv_ctrl.cpp similarity index 91% rename from esp8285/src/ir_emit.cpp rename to esp8285/src/ir_drv_ctrl.cpp index 427d296..bb2d553 100644 --- a/esp8285/src/ir_emit.cpp +++ b/esp8285/src/ir_drv_ctrl.cpp @@ -32,18 +32,22 @@ #include "http_client.h" #include "utils.h" -#include "ir_emit.h" +#include "ir_drv_ctrl.h" #define IR_SERIES_MAX (1024) #define IR_END_CODE (10000) const uint8_t k_timeout = 50; -// As this program is a special purpose capture/decoder, let us use a larger -// than normal buffer so we can handle Air Conditioner remote codes. +// as this program is a special purpose capture/decoder, let us use a larger +// than normal buffer so we can handle Air Conditioner remote codes const uint16_t k_capture_buffer_size = IR_SERIES_MAX; static IRsend * ir_send = nullptr; static IRrecv * ir_recv = nullptr; +int g_ready_to_study = 0; +int g_study_key_id = -1; +String g_study_key_name = ""; + bool sendIR(String file_name) { String save_path = SAVE_PATH + file_name; if (LittleFS.exists(save_path)) { @@ -61,7 +65,6 @@ bool sendIR(String file_name) { cache.readBytes((char *)data_buffer, cache.size()); ir_recv->disableIRIn(); ir_send->sendRaw(data_buffer, length, 38); - ir_recv->enableIRIn(); free(data_buffer); cache.close(); return true; @@ -84,7 +87,6 @@ bool emitIR(String timing) { ir_recv->disableIRIn(); INFOF("IR send raw : %d\n", parts_num); ir_send->sendRaw(series, parts_num + 1, 38); - ir_recv->enableIRIn(); } return true; } @@ -114,7 +116,6 @@ bool sendCommand(String file_name, int key) { } ir_recv->disableIRIn(); ir_send->sendRaw(user_data, data_length, 38); - ir_recv->enableIRIn(); ir_close(); free(user_data); free(content); @@ -151,7 +152,6 @@ void sendStatus(String file, t_remote_ac_status status) { DEBUGF("data_length = %d\n", data_length); ir_recv->disableIRIn(); ir_send->sendRaw(user_data, data_length, 38); - ir_recv->enableIRIn(); ir_close(); free(user_data); free(content); @@ -163,6 +163,29 @@ void sendStatus(String file, t_remote_ac_status status) { } } +void prepareRecvIR(int key_id, String key_name) { + enableIRIn(); + g_study_key_id = key_id; + g_study_key_name = key_name; + g_ready_to_study = 1; +} + +void cancelRecvIR() { + // called solicited + g_ready_to_study = 0; + g_study_key_name = ""; + g_study_key_id = -1; + disableIRIn(); +} + +void completedRecvIR(int key_id, String key_name) { + // called unsolicited + g_ready_to_study = 0; + g_study_key_name = ""; + g_study_key_id = -1; + disableIRIn(); +} + void recvIR() { decode_results results; if (ir_recv->decode(&results)) { @@ -223,13 +246,13 @@ void loadIRPin(uint8_t send_pin, uint8_t recv_pin) { DEBUGF("Load IR send pin at %d\n", send_pin); ir_send->begin(); ir_recv = new IRrecv(recv_pin, k_capture_buffer_size, k_timeout, true); - enableIR(); + disableIRIn(); } -void disableIR() { +void disableIRIn() { ir_recv->disableIRIn(); } -void enableIR() { +void enableIRIn() { ir_recv->enableIRIn(); } \ No newline at end of file diff --git a/esp8285/src/ir_emit.h b/esp8285/src/ir_drv_ctrl.h similarity index 87% rename from esp8285/src/ir_emit.h rename to esp8285/src/ir_drv_ctrl.h index 011c5b2..27e3de5 100644 --- a/esp8285/src/ir_emit.h +++ b/esp8285/src/ir_drv_ctrl.h @@ -21,8 +21,8 @@ * SOFTWARE. */ -#ifndef IRIS_KIT_EMIT_H -#define IRIS_KIT_EMIT_H +#ifndef IR_DRV_CTRL_H +#define IR_DRV_CTRL_H #include #include @@ -32,9 +32,9 @@ void loadIRPin(uint8_t send_pin, uint8_t recv_pin); -void enableIR(); +void enableIRIn(); -void disableIR(); +void disableIRIn(); bool downloadBin(int remote_id); @@ -48,10 +48,16 @@ bool sendCommand(String file_name, int key); void sendStatus(String file_name, t_remote_ac_status status); +void prepareRecvIR(int key_id, String key_name); + +void cancelRecvIR(); + +void completedRecvIR(int key_id, String key_name); + void recvIR(); bool saveIR(String file_name); void initAC(String); -#endif // IRIS_KIT_EMIT_H \ No newline at end of file +#endif // IR_DRV_CTRL_H \ No newline at end of file diff --git a/esp8285/src/iris_client.cpp b/esp8285/src/iris_client.cpp index c8f8e91..fbcc4a9 100644 --- a/esp8285/src/iris_client.cpp +++ b/esp8285/src/iris_client.cpp @@ -35,7 +35,7 @@ #include "serials.h" #include "iot_hub.h" #include "http_client.h" -#include "ir_emit.h" +#include "ir_drv_ctrl.h" #include "iris_client.h" @@ -66,6 +66,7 @@ static int handleConnected(String product_key, String device_name, String conten static int handleHartBeat(String product_key, String device_name, String content); static int handleEmit(String product_key, String device_name, String content); static int handleNotifyStatus(String product_key, String device_name, String content); +static int processStatusChange(int status, int console_id, int key_id, String key_name); static int hb_count = 0; @@ -280,9 +281,8 @@ static String buildHeartBeat() { return heartBeatMessage; } -static String buildTestResponse(int console_id) { - String testResponseBeatMessage = ""; - +static String buildGeneralResponse(int console_id, String notify_payload) { + String notification = ""; iris_msg_doc.clear(); iris_msg_doc["eventName"] = String(EVENT_NOTIFY_RESP); iris_msg_doc["productKey"] = g_product_key; @@ -290,9 +290,21 @@ static String buildTestResponse(int console_id) { iris_msg_doc["appId"] = g_app_id; iris_msg_doc["consoleId"] = console_id; iris_msg_doc["resp"] = String(NOTIFY_RESP_TEST); - serializeJson(iris_msg_doc, testResponseBeatMessage); + serializeJson(iris_msg_doc, notify_payload); - return testResponseBeatMessage; + return notification; +} + +static String buildTestResponse(int console_id) { + return buildGeneralResponse(console_id, NOTIFY_RESP_TEST); +} + +static String buildRecvPreparedResponse(int console_id) { + return buildGeneralResponse(console_id, NOTIFY_RECV_PREPARED); +} + +static String buildStudyCancelledResponse(int console_id) { + return buildGeneralResponse(console_id, NOTIFY_STUDY_CANCELLED); } static int handleConnected(String product_key, String device_name, String content) { @@ -328,16 +340,43 @@ static int handleNotifyStatus(String product_key, String device_name, String con int status = status_notify_doc["status"]; int console_id = status_notify_doc["consoleId"]; INFOF("will enter status : %d for %s(%d)\n", status, key_name.c_str(), key_id); - if (RECIPIENT_STATUS_TEST == status) { - // send response for test notification - String testResponseData = buildTestResponse(console_id); - sendData(g_upstream_topic.c_str(), (uint8_t*) testResponseData.c_str(), testResponseData.length()); - } + + processStatusChange(status, console_id, key_id, key_name); } else { INFOF("deserialize failed\n"); } return 0; - // parse status +} + +static int processStatusChange(int status, int console_id, int key_id, String key_name) { + switch(status) { + case RECIPIENT_STATUS_TEST: + { + // send response for test notification + String testResponseData = buildTestResponse(console_id); + sendData(g_upstream_topic.c_str(), (uint8_t*) testResponseData.c_str(), testResponseData.length()); + break; + } + case RECIPIENT_STATUS_READY_TO_STUDY: + { + // enter into IR receive mode and send response + prepareRecvIR(key_id, key_name); + String recvPreparedResponseData = buildRecvPreparedResponse(console_id); + sendData(g_upstream_topic.c_str(), (uint8_t*) recvPreparedResponseData.c_str(), recvPreparedResponseData.length()); + break; + } + case RECIPIENT_STATUS_CANCEL_STUDY: + { + cancelRecvIR(); + String studyCancelledResponseData = buildStudyCancelledResponse(console_id); + sendData(g_upstream_topic.c_str(), (uint8_t*) studyCancelledResponseData.c_str(), studyCancelledResponseData.length()); + break; + } + default: + { + break; + } + } return 0; } \ No newline at end of file diff --git a/esp8285/src/iris_client.h b/esp8285/src/iris_client.h index 53a3305..a87728e 100644 --- a/esp8285/src/iris_client.h +++ b/esp8285/src/iris_client.h @@ -33,6 +33,7 @@ typedef enum { RECIPIENT_STATUS_READY_TO_STUDY = 1, RECIPIENT_STATUS_STUDIED = 2, RECIPIENT_STATUS_UPLOADED = 3, + RECIPIENT_STATUS_CANCEL_STUDY = 4, RECIPIENT_STATUS_TEST = 10, RECIPIENT_STATUS_MAX = 63, } kit_status_t; @@ -53,6 +54,8 @@ typedef enum { #define EVENT_NOTIFY_RESP "__notify_response" #define NOTIFY_RESP_TEST "test_ok" +#define NOTIFY_RECV_PREPARED "recv_prepared" +#define NOTIFY_STUDY_CANCELLED "study_cancelled" typedef int (*eventHandler)(String, String, String); typedef struct { diff --git a/esp8285/src/iris_kit.cpp b/esp8285/src/iris_kit.cpp index 1123b1e..267f052 100644 --- a/esp8285/src/iris_kit.cpp +++ b/esp8285/src/iris_kit.cpp @@ -29,7 +29,7 @@ #include #include "defines.h" -#include "ir_emit.h" +#include "ir_drv_ctrl.h" #include "iot_hub.h" #include "http_client.h" #include "global.h" @@ -53,6 +53,7 @@ extern String g_device_token; extern String g_mqtt_client_id; extern String g_mqtt_password; extern int g_app_id; +extern int g_ready_to_study; // public variable definitions const unsigned long utcOffsetInMilliSeconds = 3600 * 1000; @@ -223,11 +224,13 @@ void setup() { connectToIrextIoT(); iotCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, checkIrisIoT); - disableIRTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableIR); + disableIRTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableIRIn); } void loop() { - recvIR(); + if (g_ready_to_study) { + recvIR(); + } irextIoTKeepAlive(); yield(); } diff --git a/esp8285/src/user_settings.cpp b/esp8285/src/user_settings.cpp index 13917a3..8082c46 100644 --- a/esp8285/src/user_settings.cpp +++ b/esp8285/src/user_settings.cpp @@ -27,7 +27,7 @@ #include "defines.h" #include "global.h" #include "serials.h" -#include "ir_emit.h" +#include "ir_drv_ctrl.h" #include "user_settings.h"