reformated code

This commit is contained in:
strawmanbobi
2024-02-11 12:42:50 +08:00
parent 0cc18473dd
commit c9d70c68c7
55 changed files with 1236 additions and 165 deletions

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

99
src/emq_client.cpp Normal file
View File

@@ -0,0 +1,99 @@
/**
*
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "defines.h"
#include "serials.h"
#include "iot_hub.h"
#include "ir_baby.h"
#include "emq_client.h"
// external variable declarations
extern String g_mqtt_server;
extern String g_product_key;
extern String g_device_name;
extern String g_device_secret;
extern String g_mqtt_client_id;
extern String g_mqtt_user_name;
extern String g_mqtt_password;
extern String g_upstream_topic;
extern String g_downstream_topic;
extern int g_mqtt_port;
// private variable definitions
static bool force_disconnected = false;
// private function declarations
static void irisIrextIoTCallback(char *topic, uint8_t *data, uint32_t length);
// public function definitions
int connectToEMQXBroker(PubSubClient &mqtt_client) {
int retry_times = 0;
mqtt_client.setBufferSize(2048);
mqtt_client.setServer(g_mqtt_server.c_str(), g_mqtt_port);
mqtt_client.setCallback(irisIrextIoTCallback);
force_disconnected = false;
while (!force_disconnected && !mqtt_client.connected() && retry_times < MQTT_RETRY_MAX) {
INFOF("Connecting to MQTT Broker as %s.....\n", g_mqtt_client_id.c_str());
if (mqtt_client.connect(g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.c_str())) {
INFOF("Connected to MQTT broker\n");
mqtt_client.subscribe(g_downstream_topic.c_str());
} else {
ERRORF("Failed to connect to MQTT broker, rc = %d\n", mqtt_client.state());
INFOF(" try again in 5 seconds\n");
retry_times++;
delay(MQTT_RETRY_DELAY);
}
}
if (mqtt_client.connected()) {
INFOF("IRext IoT connect done\n");
return 0;
} else {
ERRORF("IRext IoT failed to connect\n");
return -1;
}
}
int disconnectFromEMQXBroker(PubSubClient &mqtt_client) {
force_disconnected = true;
mqtt_client.disconnect();
return 0;
}
// private function definitions
static void irisIrextIoTCallback(char *topic, uint8_t *data, uint32_t length) {
INFOF("downstream message received, topic = %s, length = %d\n", topic, length);
if (NULL != g_downstream_topic.c_str() && 0 == strcmp(topic, g_downstream_topic.c_str())) {
handleIrisKitMessage((const char*) data, length);
}
}

34
src/emq_client.h Normal file
View File

@@ -0,0 +1,34 @@
/**
*
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Arduino.h>
#include <PubSubClient.h>
#ifndef IRIS_KIT_EMQ_CLIENT_H
#define IRIS_KIT_EMQ_CLIENT_H
int connectToEMQXBroker(PubSubClient &mqtt_client);
int disconnectFromEMQXBroker(PubSubClient &mqtt_client);
#endif // IRIS_KIT_EMQ_CLIENT_H

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -21,9 +21,10 @@
* SOFTWARE.
*/
#include "IRISKitGlobal.h"
#include "defines.h"
#include "global.h"
StaticJsonDocument<1024> iris_msg_doc;
StaticJsonDocument<1024> iris_ind_doc;
StaticJsonDocument<1024> recv_msg_doc;
@@ -43,4 +44,4 @@ uint8_t ir_receive_pin = R_IR;
uint8_t rf315_receive_pin = R_315;
uint8_t rf433_send_pin = T_433;
uint8_t rf433_receive_pin = R_433;
#endif
#endif

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -21,10 +21,10 @@
* SOFTWARE.
*/
#include "IRISKitGlobal.h"
#include "IRISKitSerials.h"
#include "global.h"
#include "serials.h"
#include "IRISKitHA.h"
#include "high_availability.h"
void returnACStatus(String filename, t_remote_ac_status ac_status) {
send_msg_doc.clear();

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -30,10 +30,10 @@
#include "defines.h"
#include "IRISKitGlobal.h"
#include "IRISKitSerials.h"
#include "global.h"
#include "serials.h"
#include "IRISKitHttp.h"
#include "http_client.h"
#define HTTP_REQUEST_MAX_RETRY (5)
#define HTTP_REQUEST_RETRY_INTERVAL (200)

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -23,21 +23,15 @@
#include <Arduino.h>
#include <WString.h>
#include <PubSubClient.h>
#include "IRISKitSerials.h"
#include "IRISKitIoT.h"
#include "IRISKitIRbaby.h"
#include "IRISKitGlobal.h"
#include "serials.h"
#include "iot_hub.h"
#include "ir_baby.h"
#include "global.h"
#include "IRISKit.h"
#include "emq_client.h"
#define IRIS_KIT_PK_DEV "a1WlzsJh50b"
#define IRIS_KIT_PK_REL "a1ihYt1lqGH"
#define TOPIC_DOWNSTREAM_DEV "/user/iris_kit_downstream_dev"
#define TOPIC_UPSTREAM_DEV "/user/iris_kit_upstream_dev"
#define TOPIC_DOWNSTREAM_REL "/user/iris_kit_downstream"
#define TOPIC_UPSTREAM_REL "/user/iris_kit_upstream"
#include "iris_kit.h"
// external variable declarations
@@ -66,8 +60,6 @@ static ep_state_t endpoint_state = FSM_IDLE;
// private function declarations
static int connectToMQTTBroker();
static void irisIrextIoTCallback(char *topic, uint8_t *data, uint32_t length);
static int iot_retry = 0;
@@ -98,10 +90,7 @@ int connectToIrextIoT() {
g_mqtt_server.c_str(), g_mqtt_port,
g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.length());
mqtt_client.setBufferSize(2048);
mqtt_client.setServer(g_mqtt_server.c_str(), g_mqtt_port);
mqtt_client.setCallback(irisIrextIoTCallback);
conn_ret = connectToMQTTBroker();
conn_ret = connectToEMQXBroker(mqtt_client);
if (0 != conn_ret) {
ERRORLN("Something may went wrong with your credential, please retry connect to Wifi...");
@@ -117,7 +106,7 @@ int connectToIrextIoT() {
void irextIoTKeepAlive() {
if (!mqtt_client.connected()) {
connectToMQTTBroker();
connectToEMQXBroker(mqtt_client);
}
mqtt_client.loop();
}
@@ -136,36 +125,3 @@ void checkIrextIoT() {
sendIrisKitHeartBeat();
}
}
// private function definitions
static int connectToMQTTBroker() {
int retry_times = 0;
while (!mqtt_client.connected() && retry_times < MQTT_RETRY_MAX) {
INFOF("Connecting to MQTT Broker as %s.....\n", g_mqtt_client_id.c_str());
if (mqtt_client.connect(g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.c_str())) {
INFOF("Connected to MQTT broker\n");
mqtt_client.subscribe(g_downstream_topic.c_str());
} else {
ERRORF("Failed to connect to MQTT broker, rc = %d\n", mqtt_client.state());
INFOF(" try again in 5 seconds\n");
retry_times++;
delay(MQTT_RETRY_DELAY);
}
}
if (mqtt_client.connected()) {
INFOF("IRext IoT connect done\n");
return 0;
} else {
ERRORF("IRext IoT failed to connect\n");
return -1;
}
}
static void irisIrextIoTCallback(char *topic, uint8_t *data, uint32_t length) {
INFOF("downstream message received, topic = %s, length = %d\n", topic, length);
if (NULL != g_downstream_topic.c_str() && 0 == strcmp(topic, g_downstream_topic.c_str())) {
handleIrisKitMessage((const char*) data, length);
}
}

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,6 +26,13 @@
#include <stdint.h>
#define IRIS_KIT_PK_DEV "a1WlzsJh50b"
#define IRIS_KIT_PK_REL "a1ihYt1lqGH"
#define TOPIC_DOWNSTREAM_DEV "/user/iris_kit_downstream_dev"
#define TOPIC_UPSTREAM_DEV "/user/iris_kit_upstream_dev"
#define TOPIC_DOWNSTREAM_REL "/user/iris_kit_downstream"
#define TOPIC_UPSTREAM_REL "/user/iris_kit_upstream"
typedef enum {
FSM_IDLE = 0,
FSM_CONNECTED = 1,

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -31,13 +31,13 @@
#include "defines.h"
#include "IRISKitGlobal.h"
#include "IRISKitSerials.h"
#include "IRISKitIoT.h"
#include "IRISKitHttp.h"
#include "IRISKitIR.h"
#include "global.h"
#include "serials.h"
#include "iot_hub.h"
#include "http_client.h"
#include "ir_emit.h"
#include "IRISKitIRbaby.h"
#include "ir_baby.h"
extern StaticJsonDocument<1024> http_request_doc;
@@ -104,12 +104,12 @@ String getDeviceID() {
return device_id;
}
int authIrisKitAccount(String credential_token,
String password,
String& product_key,
String& device_name,
String& device_secret,
int& app_id) {
int registerIrisKit(String credential_token,
String password,
String& product_key,
String& device_name,
String& device_secret,
int& app_id) {
int ret = -1;
int tsi = -1;
bool protocol_prefix = false;

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ int getIRISKitVersion(char *buffer, int buffer_size);
String getDeviceID();
int authIrisKitAccount(String credential_token,
int registerIrisKit(String credential_token,
String password,
String& product_key,
String& device_name,

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -25,14 +25,14 @@
#include "defines.h"
#include "IRISKitGlobal.h"
#include "IRISKitSerials.h"
#include "IRISKitUserSettings.h"
#include "IRISKitIRbaby.h"
#include "IRISKitHttp.h"
#include "IRISKitUtils.h"
#include "global.h"
#include "serials.h"
#include "user_settings.h"
#include "ir_baby.h"
#include "http_client.h"
#include "utils.h"
#include "IRISKitIR.h"
#include "ir_emit.h"
#define IR_SERIES_MAX (1024)
#define IR_END_CODE (10000)

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Author: Strawmanbobi and Caffreyfans
*
@@ -28,16 +28,16 @@
#include <Ticker.h>
#include "defines.h"
#include "IRISKitIR.h"
#include "IRISKitIoT.h"
#include "IRISKitHttp.h"
#include "IRISKitGlobal.h"
#include "IRISKitIRbaby.h"
#include "IRISKitSerials.h"
#include "IRISKitUserSettings.h"
#include "IRISKitUtils.h"
#include "ir_emit.h"
#include "iot_hub.h"
#include "http_client.h"
#include "global.h"
#include "ir_baby.h"
#include "serials.h"
#include "user_settings.h"
#include "utils.h"
#include "IRISKit.h"
#include "iris_kit.h"
// external variable declarations
extern char iris_server_address[];
@@ -160,7 +160,7 @@ void setup() {
do {
if (WiFi.status() == WL_CONNECTED) {
if (0 == authIrisKitAccount(iris_credential_token,
if (0 == registerIrisKit(iris_credential_token,
iris_password,
g_product_key,
g_device_name,

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -25,11 +25,11 @@
#include <WiFiManager.h>
#include "defines.h"
#include "IRISKitGlobal.h"
#include "IRISKitSerials.h"
#include "IRISKitIR.h"
#include "global.h"
#include "serials.h"
#include "ir_emit.h"
#include "IRISKitUserSettings.h"
#include "user_settings.h"
#define FILE_GENERIC_CONFIG "config"

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
#include <ArduinoJson.h>
#include "IRISKit.h"
#include "iris_kit.h"
#include "ir_ac_control.h"

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
#include <string.h>
#include <MD5Builder.h>
#include "IRISKitUtils.h"
#include "utils.h"
// public variable definitions
MD5Builder md5_builder;

View File

@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2020-2022 IRbaby-IRext
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal