rebuilt HTTP request module
This commit is contained in:
@@ -35,6 +35,8 @@ DeviceProperty PropertyMessageBuffer[MESSAGE_BUFFER_SIZE];
|
|||||||
#define SHA256HMAC_SIZE 32
|
#define SHA256HMAC_SIZE 32
|
||||||
#define DATA_CALLBACK_SIZE 20
|
#define DATA_CALLBACK_SIZE 20
|
||||||
|
|
||||||
|
#define MQTT_WAIT_GENERIC (10000)
|
||||||
|
|
||||||
#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
|
#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
|
||||||
#define ALINK_EVENT_BODY_FORMAT "{\"id\": \"123\",\"version\": \"1.0\",\"params\": %s,\"method\": \"thing.event.%s.post\"}"
|
#define ALINK_EVENT_BODY_FORMAT "{\"id\": \"123\",\"version\": \"1.0\",\"params\": %s,\"method\": \"thing.event.%s.post\"}"
|
||||||
|
|
||||||
@@ -129,7 +131,7 @@ int AliyunIoTSDK::mqttCheckConnect() {
|
|||||||
} else {
|
} else {
|
||||||
Serial.print("ERROR:\tMQTT Connect err: ");
|
Serial.print("ERROR:\tMQTT Connect err: ");
|
||||||
Serial.println(client->state());
|
Serial.println(client->state());
|
||||||
delay(10000);
|
delay(MQTT_WAIT_GENERIC);
|
||||||
connectRetry++;
|
connectRetry++;
|
||||||
Serial.print("INFO:\tretry: ");
|
Serial.print("INFO:\tretry: ");
|
||||||
Serial.println(connectRetry);
|
Serial.println(connectRetry);
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ platform = espressif8266
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
upload_speed = 115200
|
upload_speed = 115200
|
||||||
upload_port = COM5
|
upload_port = COM6
|
||||||
monitor_port = COM5
|
monitor_port = COM6
|
||||||
board_build.flash_mode = dout
|
board_build.flash_mode = dout
|
||||||
build_flags =
|
build_flags =
|
||||||
-Wno-unused-function
|
-Wno-unused-function
|
||||||
|
|||||||
@@ -29,9 +29,18 @@
|
|||||||
#include <WString.h>
|
#include <WString.h>
|
||||||
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
#include "IRbabyGlobal.h"
|
||||||
|
#include "IRbabySerial.h"
|
||||||
|
#include "IRbabyHttp.h"
|
||||||
|
|
||||||
#include "IRBabyIRIS.h"
|
#include "IRBabyIRIS.h"
|
||||||
|
|
||||||
|
extern StaticJsonDocument<1024> http_request_doc;
|
||||||
|
extern StaticJsonDocument<1024> http_response_doc;
|
||||||
|
|
||||||
char iris_credential_token[CREDENTIAL_MAX] = { 0 };
|
char iris_credential_token[CREDENTIAL_MAX] = { 0 };
|
||||||
|
char iris_server_address[URL_SHORT_MAX] = { 0 };
|
||||||
|
|
||||||
|
|
||||||
int getIRISKitVersion(char *buffer, int buffer_size) {
|
int getIRISKitVersion(char *buffer, int buffer_size) {
|
||||||
if (NULL == buffer) {
|
if (NULL == buffer) {
|
||||||
@@ -51,3 +60,77 @@ int getDeviceID(char* buffer, int buffer_size) {
|
|||||||
snprintf(buffer, buffer_size - 1, "%s", chipId.c_str());
|
snprintf(buffer, buffer_size - 1, "%s", chipId.c_str());
|
||||||
return strlen(buffer);
|
return strlen(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fetchIrisCredential(String credential_token,
|
||||||
|
String& product_key,
|
||||||
|
String& device_name,
|
||||||
|
String& device_secret) {
|
||||||
|
int ret = -1;
|
||||||
|
int tsi = -1;
|
||||||
|
bool protocol_prefix = false;
|
||||||
|
String fetch_credential_url;
|
||||||
|
String request_data = "";
|
||||||
|
String response_data = "";
|
||||||
|
|
||||||
|
http_error_t http_ret = HTTP_ERROR_GENERIC;
|
||||||
|
|
||||||
|
String device_id("IRbaby_");
|
||||||
|
if (NULL != strstr(iris_server_address, "http://")) {
|
||||||
|
protocol_prefix = true;
|
||||||
|
}
|
||||||
|
if (protocol_prefix) {
|
||||||
|
fetch_credential_url = String(iris_server_address);
|
||||||
|
} else {
|
||||||
|
fetch_credential_url = String("http://");
|
||||||
|
fetch_credential_url.concat(iris_server_address);
|
||||||
|
}
|
||||||
|
fetch_credential_url.concat(String(FETCH_CREDENTIAL_SUFFIX));
|
||||||
|
device_id.concat(String(ESP.getChipId(), HEX));
|
||||||
|
|
||||||
|
INFOF("fetch credential URL = %s\n", fetch_credential_url.c_str());
|
||||||
|
if (credential_token.isEmpty()) {
|
||||||
|
ERRORLN("credential token is empty");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
tsi = credential_token.indexOf(",");
|
||||||
|
if (-1 == tsi) {
|
||||||
|
ERRORLN("credential token format error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
product_key = credential_token.substring(0, tsi);
|
||||||
|
device_name = credential_token.substring(tsi + 1);
|
||||||
|
http_request_doc.clear();
|
||||||
|
http_request_doc["deviceID"] = device_id;
|
||||||
|
http_request_doc["credentialToken"] = credential_token;
|
||||||
|
serializeJson(http_request_doc, request_data);
|
||||||
|
|
||||||
|
http_ret = httpPost(fetch_credential_url, request_data, response_data);
|
||||||
|
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
} else {
|
||||||
|
INFOF("response invalid, code = %d\n", resultCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendIrisKitConnect() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendIrisKitHeartBeat() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -39,6 +39,7 @@
|
|||||||
#include "IRbaby.h"
|
#include "IRbaby.h"
|
||||||
|
|
||||||
#define CREDENTIAL_INIT_RETRY_MAX (3)
|
#define CREDENTIAL_INIT_RETRY_MAX (3)
|
||||||
|
#define SYSTEM_DELAY (2000)
|
||||||
|
|
||||||
// external variable declarations
|
// external variable declarations
|
||||||
extern char iris_server_address[];
|
extern char iris_server_address[];
|
||||||
@@ -74,7 +75,7 @@ void setup() {
|
|||||||
pinMode(0, OUTPUT);
|
pinMode(0, OUTPUT);
|
||||||
digitalWrite(0, LOW);
|
digitalWrite(0, LOW);
|
||||||
attachInterrupt(digitalPinToInterrupt(RESET_PIN), factoryReset, ONLOW);
|
attachInterrupt(digitalPinToInterrupt(RESET_PIN), factoryReset, ONLOW);
|
||||||
delay(3000);
|
delay(SYSTEM_DELAY);
|
||||||
|
|
||||||
Serial.clearWriteError();
|
Serial.clearWriteError();
|
||||||
INFOLN();
|
INFOLN();
|
||||||
@@ -161,7 +162,7 @@ void setup() {
|
|||||||
ERRORLN("retried fetch credential for 3 times, reset WiFi");
|
ERRORLN("retried fetch credential for 3 times, reset WiFi");
|
||||||
wifiReset();
|
wifiReset();
|
||||||
}
|
}
|
||||||
delay(2000);
|
delay(SYSTEM_DELAY);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
INFOF("credential get : %s\n", iris_credential_token);
|
INFOF("credential get : %s\n", iris_credential_token);
|
||||||
@@ -171,7 +172,7 @@ void setup() {
|
|||||||
|
|
||||||
saveSettings();
|
saveSettings();
|
||||||
|
|
||||||
delay(1000);
|
delay(SYSTEM_DELAY);
|
||||||
connectToAliyunIoT();
|
connectToAliyunIoT();
|
||||||
loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]);
|
loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]);
|
||||||
|
|
||||||
@@ -208,6 +209,6 @@ static void wifiReset() {
|
|||||||
WiFi.persistent(true);
|
WiFi.persistent(true);
|
||||||
WiFi.disconnect(true);
|
WiFi.disconnect(true);
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
delay(2000);
|
delay(SYSTEM_DELAY);
|
||||||
ESP.reset();
|
ESP.reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,3 @@ static void irisAlinkCallback(const char *topic, uint8_t *data, int length) {
|
|||||||
INFO(", length = ");
|
INFO(", length = ");
|
||||||
INFOLN(length);
|
INFOLN(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sendIrisKitHeartBeat() {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -34,115 +34,72 @@
|
|||||||
|
|
||||||
#include "IRbabyHttp.h"
|
#include "IRbabyHttp.h"
|
||||||
|
|
||||||
#define FETCH_CREDENTIAL_SUFFIX "/irext-collect/credentials/fetch_credential"
|
#define HTTP_REQUEST_MAX_RETRY (5)
|
||||||
#define LOAD_ALIOT_ACCOUNT_SUFFIX "/irext-collect/aliot/load_account"
|
#define HTTP_REQUEST_RETRY_INTERVAL (200)
|
||||||
#define DOWNLOAD_PREFIX "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/irda_"
|
|
||||||
#define DOWNLOAD_SUFFIX ".bin"
|
|
||||||
|
|
||||||
|
http_error_t httpPost(String url, String request_data, String& result) {
|
||||||
extern StaticJsonDocument<1024> http_request_doc;
|
http_error_t ret = HTTP_ERROR_GENERIC;
|
||||||
extern StaticJsonDocument<1024> http_response_doc;
|
|
||||||
|
|
||||||
|
|
||||||
char iris_server_address[URL_SHORT_MAX] = { 0 };
|
|
||||||
|
|
||||||
|
|
||||||
int fetchIrisCredential(String credential_token,
|
|
||||||
String& product_key,
|
|
||||||
String& device_name,
|
|
||||||
String& device_secret) {
|
|
||||||
int ret = -1;
|
|
||||||
bool protocol_prefix = false;
|
|
||||||
String fetch_credential_url;
|
|
||||||
|
|
||||||
String device_id("IRbaby_");
|
|
||||||
if (NULL != strstr(iris_server_address, "http://")) {
|
|
||||||
protocol_prefix = true;
|
|
||||||
}
|
|
||||||
if (protocol_prefix) {
|
|
||||||
fetch_credential_url = String(iris_server_address);
|
|
||||||
} else {
|
|
||||||
fetch_credential_url = String("http://");
|
|
||||||
fetch_credential_url.concat(iris_server_address);
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTPClient http_client;
|
|
||||||
int tsi = 0;
|
|
||||||
int response_code = 0;
|
int response_code = 0;
|
||||||
fetch_credential_url.concat(String(FETCH_CREDENTIAL_SUFFIX));
|
HTTPClient http_client;
|
||||||
device_id.concat(String(ESP.getChipId(), HEX));
|
http_client.begin(wifi_client, url);
|
||||||
|
|
||||||
INFOF("fetch credential URL = %s\n", fetch_credential_url.c_str());
|
|
||||||
if (credential_token.isEmpty()) {
|
|
||||||
ERRORLN("credential token is empty");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
tsi = credential_token.indexOf(",");
|
|
||||||
if (-1 == tsi) {
|
|
||||||
ERRORLN("credential token format error");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
product_key = credential_token.substring(0, tsi);
|
|
||||||
device_name = credential_token.substring(tsi + 1);
|
|
||||||
|
|
||||||
http_client.begin(wifi_client, fetch_credential_url);
|
|
||||||
http_client.addHeader("Content-Type", "application/json");
|
http_client.addHeader("Content-Type", "application/json");
|
||||||
http_request_doc.clear();
|
bool request_flag = false;
|
||||||
http_request_doc["deviceID"] = device_id;
|
|
||||||
http_request_doc["credentialToken"] = credential_token;
|
|
||||||
String request_data = "";
|
|
||||||
serializeJson(http_request_doc, request_data);
|
|
||||||
|
|
||||||
|
for (int http_retry = 0; http_retry < HTTP_REQUEST_MAX_RETRY; http_retry++) {
|
||||||
response_code = http_client.POST(request_data);
|
response_code = http_client.POST(request_data);
|
||||||
if (200 == response_code) {
|
if (HTTP_CODE_OK == response_code) {
|
||||||
INFOF("HTTP response code = %d\n", response_code);
|
INFOF("HTTP response OK : %d\n", response_code);
|
||||||
String payload = http_client.getString();
|
String payload = http_client.getString();
|
||||||
INFOF("HTTP response payload = %s\n", payload.c_str());
|
result = payload;
|
||||||
http_response_doc.clear();
|
request_flag = true;
|
||||||
if (OK == deserializeJson(http_response_doc, payload.c_str())) {
|
break;
|
||||||
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;
|
|
||||||
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;
|
|
||||||
} else {
|
} else {
|
||||||
INFOF("response invalid, code = %d\n", resultCode);
|
ERRORF("HTTP response ERROR : %d\n", response_code);
|
||||||
|
delay(HTTP_REQUEST_RETRY_INTERVAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (request_flag) {
|
||||||
|
ret = HTTP_ERROR_SUCCESS;
|
||||||
}
|
}
|
||||||
http_client.end();
|
http_client.end();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
http_error_t downLoadFile(String url, String file, String path) {
|
||||||
|
http_error_t ret = HTTP_ERROR_GENERIC;
|
||||||
|
HTTPClient http_client;
|
||||||
|
int response_code = 0;
|
||||||
|
String save_path = path + file;
|
||||||
|
File cache = LittleFS.open(save_path, "w");
|
||||||
|
bool download_flag = false;
|
||||||
|
|
||||||
|
if (cache) {
|
||||||
|
http_client.begin(wifi_client, url);
|
||||||
|
for (int http_retry = 0; http_retry < HTTP_REQUEST_MAX_RETRY; http_retry++) {
|
||||||
|
response_code = http_client.GET();
|
||||||
|
if (HTTP_CODE_OK == response_code) {
|
||||||
|
download_flag = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
ERRORF("HTTP response ERROR : %d\n", response_code);
|
||||||
|
delay(HTTP_REQUEST_RETRY_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (download_flag) {
|
||||||
|
http_client.writeToStream(&cache);
|
||||||
|
ret = HTTP_ERROR_SUCCESS;
|
||||||
|
DEBUGF("Download %s success\n", file.c_str());
|
||||||
|
} else {
|
||||||
|
LittleFS.remove(save_path);
|
||||||
|
ret = HTTP_ERROR_GENERIC;
|
||||||
|
ERRORF("Download %s failed\n", file.c_str());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret = HTTP_ERROR_LOCAL_SPACE;
|
||||||
|
ERRORLN("Don't have enough file zoom");
|
||||||
|
}
|
||||||
|
cache.close();
|
||||||
|
http_client.end();
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void downLoadFile(String file, String path) {
|
|
||||||
HTTPClient http_client;
|
|
||||||
String download_url = DOWNLOAD_PREFIX + file + DOWNLOAD_SUFFIX;
|
|
||||||
String save_path = path + file;
|
|
||||||
File cache = LittleFS.open(save_path, "w");
|
|
||||||
bool download_flag = false;
|
|
||||||
if (cache) {
|
|
||||||
http_client.begin(wifi_client, download_url);
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
if (http_client.GET() == HTTP_CODE_OK) {
|
|
||||||
download_flag = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
if (download_flag) {
|
|
||||||
http_client.writeToStream(&cache);
|
|
||||||
DEBUGF("Download %s success\n", file.c_str());
|
|
||||||
} else {
|
|
||||||
LittleFS.remove(save_path);
|
|
||||||
ERRORF("Download %s failed\n", file.c_str());
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
ERRORLN("Don't have enough file zoom");
|
|
||||||
cache.close();
|
|
||||||
http_client.end();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,13 +28,21 @@
|
|||||||
|
|
||||||
#define URL_SHORT_MAX (128)
|
#define URL_SHORT_MAX (128)
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
HTTP_ERROR_SUCCESS = 0,
|
||||||
|
HTTP_ERROR_RESPONSE = 1,
|
||||||
|
HTTP_ERROR_PAYLOAD = 2,
|
||||||
|
HTTP_ERROR_BUSINESS = 3,
|
||||||
|
HTTP_ERROR_LOCAL_SPACE = 4,
|
||||||
|
HTTP_ERROR_GENERIC = 7,
|
||||||
|
HTTP_ERROR_MAX = 15,
|
||||||
|
} http_error_t;
|
||||||
|
|
||||||
int fetchIrisCredential(String credential_token,
|
|
||||||
String& product_key,
|
|
||||||
String& device_name,
|
|
||||||
String& device_secret);
|
|
||||||
|
|
||||||
void downLoadFile(String file, String path);
|
// public function export
|
||||||
|
http_error_t httpPost(String url, String request_data, String& result);
|
||||||
|
|
||||||
|
http_error_t downLoadFile(String url, String file, String path);
|
||||||
|
|
||||||
|
|
||||||
#endif // IRBABY_HTTP_H
|
#endif // IRBABY_HTTP_H
|
||||||
@@ -28,6 +28,7 @@
|
|||||||
#include "IRbabyGlobal.h"
|
#include "IRbabyGlobal.h"
|
||||||
#include "IRbabySerial.h"
|
#include "IRbabySerial.h"
|
||||||
#include "IRbabyUserSettings.h"
|
#include "IRbabyUserSettings.h"
|
||||||
|
#include "IRbabyIRIS.h"
|
||||||
#include "IRbabyHttp.h"
|
#include "IRbabyHttp.h"
|
||||||
|
|
||||||
#include "IRbabyIR.h"
|
#include "IRbabyIR.h"
|
||||||
@@ -71,8 +72,10 @@ bool sendIR(String file_name) {
|
|||||||
|
|
||||||
void sendStatus(String file, t_remote_ac_status status) {
|
void sendStatus(String file, t_remote_ac_status status) {
|
||||||
String save_path = SAVE_PATH + file;
|
String save_path = SAVE_PATH + file;
|
||||||
|
String url = String(DOWNLOAD_PREFIX) + file + String(DOWNLOAD_SUFFIX);
|
||||||
|
|
||||||
if (!LittleFS.exists(save_path)) {
|
if (!LittleFS.exists(save_path)) {
|
||||||
downLoadFile(file, SAVE_PATH);
|
downLoadFile(url, file, SAVE_PATH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LittleFS.exists(save_path)) {
|
if (LittleFS.exists(save_path)) {
|
||||||
|
|||||||
@@ -26,8 +26,24 @@
|
|||||||
|
|
||||||
#define CREDENTIAL_MAX (40)
|
#define CREDENTIAL_MAX (40)
|
||||||
|
|
||||||
|
// web http call URL list
|
||||||
|
#define FETCH_CREDENTIAL_SUFFIX "/irext-collect/credentials/fetch_credential"
|
||||||
|
#define LOAD_ALIOT_ACCOUNT_SUFFIX "/irext-collect/aliot/load_account"
|
||||||
|
#define DOWNLOAD_PREFIX "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/irda_"
|
||||||
|
#define DOWNLOAD_SUFFIX ".bin"
|
||||||
|
|
||||||
|
|
||||||
int getIRISKitVersion(char *buffer, int buffer_size);
|
int getIRISKitVersion(char *buffer, int buffer_size);
|
||||||
|
|
||||||
int getDeviceID(char* buffer, int buffer_size);
|
int getDeviceID(char* buffer, int buffer_size);
|
||||||
|
|
||||||
|
int fetchIrisCredential(String credential_token,
|
||||||
|
String& product_key,
|
||||||
|
String& device_name,
|
||||||
|
String& device_secret);
|
||||||
|
|
||||||
|
void sendIrisKitConnect();
|
||||||
|
|
||||||
|
void sendIrisKitHeartBeat();
|
||||||
|
|
||||||
#endif // IRBABY_IRIS_H
|
#endif // IRBABY_IRIS_H
|
||||||
Reference in New Issue
Block a user