From c0037a7892fa60facbb92899dd9a86acb03f62df Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Tue, 30 Apr 2024 09:51:41 +0800 Subject: [PATCH] changed to Aliot shared instance --- .idea/.gitignore | 8 ++++++++ .idea/iris-kit.iml | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ .vscode/settings.json | 23 ----------------------- src/aliot_client.cpp | 1 - src/global.cpp | 1 + src/iot_hub.cpp | 14 +++++++++----- src/iris_client.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/iris_client.h | 12 ++++++++++++ 10 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/iris-kit.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml delete mode 100644 .vscode/settings.json diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/iris-kit.iml b/.idea/iris-kit.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/iris-kit.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b78e90c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e6de926..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "deque": "cpp", - "list": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "string_view": "cpp", - "initializer_list": "cpp", - "ranges": "cpp", - "memory": "cpp", - "random": "cpp", - "optional": "cpp", - "memory_resource": "cpp", - "system_error": "cpp", - "*.tcc": "cpp", - "functional": "cpp", - "string.h": "c", - "arduino.h": "c" - }, - "C_Cpp.errorSquiggles": "disabled" -} \ No newline at end of file diff --git a/src/aliot_client.cpp b/src/aliot_client.cpp index 1023e04..822d236 100644 --- a/src/aliot_client.cpp +++ b/src/aliot_client.cpp @@ -46,7 +46,6 @@ extern String g_downstream_topic; extern int g_mqtt_port; extern String g_aliot_region; -extern String g_aliot_instance_id; // private variable definitions diff --git a/src/global.cpp b/src/global.cpp index ac123f7..8064adf 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -32,6 +32,7 @@ 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; WiFiClient wifi_client; diff --git a/src/iot_hub.cpp b/src/iot_hub.cpp index b8c9b66..4898054 100644 --- a/src/iot_hub.cpp +++ b/src/iot_hub.cpp @@ -54,12 +54,12 @@ String g_upstream_topic = ""; String g_downstream_topic = ""; String g_aliot_region = "cn-shanghai"; -String g_aliot_instance_id = "iot-060a2sie"; int g_mqtt_port = 1883; int g_app_id = 0; mqtt_type_t g_mqtt_type = MQTT_TYPE_MAX; boolean g_subscribed = false; +unsigned long last_check_time = 0UL; // private variable definitions static bool downstream_topic_subscribed = false; @@ -135,10 +135,14 @@ void irextIoTKeepAlive() { emqxClientKeepAlive(); } - if (!g_mqtt_client.connected()) { - g_mqtt_client.unsubscribe(g_downstream_topic.c_str()); - g_subscribed = false; - connectToIrextIoT(); + unsigned long current_time = millis(); + + if (current_time - last_check_time > 10000) { + if (!g_mqtt_client.connected()) { + g_mqtt_client.unsubscribe(g_downstream_topic.c_str()); + g_subscribed = false; + connectToIrextIoT(); + } } } diff --git a/src/iris_client.cpp b/src/iris_client.cpp index 2064300..193728e 100644 --- a/src/iris_client.cpp +++ b/src/iris_client.cpp @@ -44,7 +44,8 @@ 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<1024> emit_code_doc; +extern StaticJsonDocument<2048> emit_code_doc; +extern StaticJsonDocument<1024> status_notify_doc; extern String g_product_key; extern String g_device_name; @@ -279,6 +280,20 @@ static String buildHeartBeat() { return heartBeatMessage; } +static String buildTestResponse() { + String testResponseBeatMessage = ""; + + 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["resp"] = String(NOTIFY_RESP_TEST); + serializeJson(iris_msg_doc, testResponseBeatMessage); + + return testResponseBeatMessage; +} + static int handleConnected(String product_key, String device_name, String content) { return 0; } @@ -304,6 +319,23 @@ static int handleEmit(String product_key, String device_name, String content) { } static int handleNotifyStatus(String product_key, String device_name, String content) { - INFOF("received emit code : %s, %s, %s\n", product_key.c_str(), device_name.c_str(), content.c_str()); + INFOF("received status change notification : %s, %s, %s\n", product_key.c_str(), device_name.c_str(), content.c_str()); + status_notify_doc.clear(); + if (DeserializationError::Ok == deserializeJson(status_notify_doc, content)) { + int key_id = status_notify_doc["keyId"]; + String key_name = status_notify_doc["keyName"]; + int status = status_notify_doc["status"]; + 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(); + sendData(g_upstream_topic.c_str(), (uint8_t*) testResponseData.c_str(), testResponseData.length()); + } + } else { + INFOF("deserialize failed\n"); + } + return 0; + // parse status + return 0; } \ No newline at end of file diff --git a/src/iris_client.h b/src/iris_client.h index 93ecf8d..53a3305 100644 --- a/src/iris_client.h +++ b/src/iris_client.h @@ -28,6 +28,15 @@ #define USER_NAME_MAX (64) #define PASSWORD_MAX (64) +typedef enum { + RECIPIENT_STATUS_IDLE = 0, + RECIPIENT_STATUS_READY_TO_STUDY = 1, + RECIPIENT_STATUS_STUDIED = 2, + RECIPIENT_STATUS_UPLOADED = 3, + RECIPIENT_STATUS_TEST = 10, + RECIPIENT_STATUS_MAX = 63, +} kit_status_t; + // web http call URL list #define GET_IRIS_KIT_ACCOUNT_SUFFIX "/irext-collect/credentials/auth_iris_kit" #define LOAD_ALIOT_ACCOUNT_SUFFIX "/irext-collect/aliot/load_account" @@ -41,6 +50,9 @@ // IRIS communication #define EVENT_NAME_CONNECT "__connect" #define EVENT_HEART_BEAT_REQ "__hb_request" +#define EVENT_NOTIFY_RESP "__notify_response" + +#define NOTIFY_RESP_TEST "test_ok" typedef int (*eventHandler)(String, String, String); typedef struct {