added upstream MQTT messages
This commit is contained in:
@@ -31,17 +31,34 @@
|
||||
#include "defines.h"
|
||||
#include "IRbabyGlobal.h"
|
||||
#include "IRbabySerial.h"
|
||||
#include "IRbabyAlink.h"
|
||||
#include "IRbabyHttp.h"
|
||||
|
||||
#include "IRBabyIRIS.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 String g_product_key;
|
||||
extern String g_device_name;
|
||||
extern String g_upstream_topic;
|
||||
extern int g_app_id;
|
||||
|
||||
char iris_credential_token[CREDENTIAL_MAX] = { 0 };
|
||||
char iris_server_address[URL_SHORT_MAX] = { 0 };
|
||||
|
||||
|
||||
|
||||
// private function declarations
|
||||
static String buildConnect();
|
||||
|
||||
static String buildHeartBeat();
|
||||
|
||||
|
||||
// public function definitions
|
||||
int getIRISKitVersion(char *buffer, int buffer_size) {
|
||||
if (NULL == buffer) {
|
||||
return -1;
|
||||
@@ -64,17 +81,17 @@ int getDeviceID(char* buffer, int buffer_size) {
|
||||
int fetchIrisCredential(String credential_token,
|
||||
String& product_key,
|
||||
String& device_name,
|
||||
String& device_secret) {
|
||||
String& device_secret,
|
||||
int& app_id) {
|
||||
int ret = -1;
|
||||
int tsi = -1;
|
||||
bool protocol_prefix = false;
|
||||
String fetch_credential_url;
|
||||
String request_data = "";
|
||||
String response_data = "";
|
||||
|
||||
String device_id("IRbaby_");
|
||||
http_error_t http_ret = HTTP_ERROR_GENERIC;
|
||||
|
||||
String device_id("IRbaby_");
|
||||
if (NULL != strstr(iris_server_address, "http://")) {
|
||||
protocol_prefix = true;
|
||||
}
|
||||
@@ -109,12 +126,11 @@ int fetchIrisCredential(String credential_token,
|
||||
if (HTTP_ERROR_SUCCESS == http_ret) {
|
||||
http_response_doc.clear();
|
||||
if (OK == deserializeJson(http_response_doc, response_data.c_str())) {
|
||||
String ds = "";
|
||||
int resultCode = http_response_doc["status"]["code"];
|
||||
if (0 == resultCode) {
|
||||
INFOLN("response valid, try getting entity");
|
||||
ds = (String) http_response_doc["entity"];
|
||||
device_secret = ds;
|
||||
device_secret = (String) http_response_doc["entity"]["deviceSecret"];
|
||||
app_id = (int) http_response_doc["entity"]["appId"];
|
||||
INFOF("HTTP response deserialized, PK = %s, DN = %s, DS = %s\n",
|
||||
product_key.c_str(), device_name.c_str(), device_secret.c_str());
|
||||
ret = 0;
|
||||
@@ -128,9 +144,38 @@ int fetchIrisCredential(String credential_token,
|
||||
}
|
||||
|
||||
void sendIrisKitConnect() {
|
||||
|
||||
String connectMessage = buildConnect();
|
||||
sendRawData(g_upstream_topic.c_str(), (uint8_t*) connectMessage.c_str(), connectMessage.length());
|
||||
}
|
||||
|
||||
void sendIrisKitHeartBeat() {
|
||||
String heartBeatMessage = buildHeartBeat();
|
||||
sendRawData(g_upstream_topic.c_str(), (uint8_t*) heartBeatMessage.c_str(), heartBeatMessage.length());
|
||||
}
|
||||
|
||||
|
||||
// private function definitions
|
||||
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;
|
||||
}
|
||||
@@ -48,6 +48,7 @@ extern char iris_credential_token[];
|
||||
extern String g_product_key;
|
||||
extern String g_device_name;
|
||||
extern String g_device_secret;
|
||||
extern int g_app_id;
|
||||
|
||||
|
||||
// public variable definitions
|
||||
@@ -153,7 +154,8 @@ void setup() {
|
||||
if (0 == fetchIrisCredential(iris_credential_token,
|
||||
g_product_key,
|
||||
g_device_name,
|
||||
g_device_secret)) {
|
||||
g_device_secret,
|
||||
g_app_id)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,10 @@ String g_product_key = "";
|
||||
String g_device_name = "";
|
||||
String g_device_secret = "";
|
||||
String g_region_id = "cn-shanghai";
|
||||
String g_upstream_topic = "";
|
||||
int g_app_id = 0;
|
||||
|
||||
static AliyunIoTSDK iot;
|
||||
static char IRIS_UPSTREAM_TOPIC[TOPIC_NAME_MAX] = { 0 };
|
||||
static ep_state_t endpoint_state = FSM_IDLE;
|
||||
|
||||
static void registerCallback();
|
||||
@@ -55,8 +56,7 @@ void connectToAliyunIoT() {
|
||||
iot.begin(wifi_client, g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(),
|
||||
g_region_id.c_str());
|
||||
INFOLN("Aliyun IoT connect done");
|
||||
snprintf(IRIS_UPSTREAM_TOPIC, TOPIC_NAME_MAX - 1, "/%s/%s/user/iris/upstream",
|
||||
g_product_key.c_str(), g_device_name.c_str());
|
||||
g_upstream_topic = g_product_key + "/" + g_device_name + "/user/iris/upstream";
|
||||
registerCallback();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include "IRbabyGlobal.h"
|
||||
#include "defines.h"
|
||||
|
||||
StaticJsonDocument<1024> iris_msg_doc;
|
||||
StaticJsonDocument<1024> iris_ind_doc;
|
||||
StaticJsonDocument<1024> recv_msg_doc;
|
||||
StaticJsonDocument<1024> send_msg_doc;
|
||||
StaticJsonDocument<1024> http_request_doc;
|
||||
|
||||
@@ -33,6 +33,10 @@
|
||||
#define DOWNLOAD_SUFFIX ".bin"
|
||||
|
||||
|
||||
// IRIS communication
|
||||
#define EVENT_NAME_CONNECT "__connect"
|
||||
#define EVENT_HEART_BEAT_REQ "__hb_request"
|
||||
|
||||
int getIRISKitVersion(char *buffer, int buffer_size);
|
||||
|
||||
int getDeviceID(char* buffer, int buffer_size);
|
||||
@@ -40,7 +44,8 @@ int getDeviceID(char* buffer, int buffer_size);
|
||||
int fetchIrisCredential(String credential_token,
|
||||
String& product_key,
|
||||
String& device_name,
|
||||
String& device_secret);
|
||||
String& device_secret,
|
||||
int& app_id);
|
||||
|
||||
void sendIrisKitConnect();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user