Skip to content

Commit

Permalink
Merge pull request #233 from blinker-iot/dev_2.0
Browse files Browse the repository at this point in the history
update codes, upgrade BLINKER OTA codes
  • Loading branch information
i3water authored Nov 23, 2018
2 parents f26ca3b + 766c50d commit d672490
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 55 deletions.
117 changes: 111 additions & 6 deletions src/Blinker/BlinkerApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "utility/BlinkerUtility.h"
#endif

#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || defined(BLINKER_AT_MQTT)
#include "utility/BlinkerOTA.h"
#endif


// enum b_widgettype_t {
// W_BUTTON,
Expand Down Expand Up @@ -77,13 +81,15 @@ static class BlinkerTimingTimer * timingTask[BLINKER_TIMING_TIMER_SIZE];
#endif


#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || defined(BLINKER_AT_MQTT)
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || defined(BLINKER_AT_MQTT)
static class BlinkerAUTO * _AUTO[2];
static class BlinkerData * _Data[BLINKER_MAX_BLINKER_DATA_SIZE];
// #endif

// #if defined(BLINKER_MQTT) || defined(BLINKER_PRO)
static class BlinkerBridge * _Bridge[BLINKER_MAX_BRIDGE_SIZE];

BlinkerOTA _OTA;
#endif

#if defined(BLINKER_WIFI)
Expand Down Expand Up @@ -1292,21 +1298,68 @@ class BlinkerApi
#endif

#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || defined(BLINKER_AT_MQTT)
void loadOTA()
{
if (_OTA.loadOTACheck())
{
if (!_OTA.loadVersion())
{
_OTA.saveVersion();
}
}
}

void ota()
{
String otaData = checkOTA();

if (otaData != BLINKER_CMD_FALSE)
{
DynamicJsonBuffer jsonBuffer;
JsonObject& otaJson = jsonBuffer.parseObject(otaData);

if (!otaJson.success())
{
BLINKER_ERR_LOG_ALL("check ota data error");
return;
}

String otaHost = otaJson["host"];
String otaUrl = otaJson["url"];
String otaFp = otaJson["fingerprint"];

_OTA.config(otaHost, otaUrl, otaFp);

_OTA.update();
}
}

String checkOTA()
{
String data = "/ota/upgrade?deviceName=" + \
STRING_format(static_cast<Proto*>(this)->_deviceName);

return blinkServer(BLINKER_CMD_OTA_NUMBER, data);
}

template<typename T>
bool configUpdate(const T& msg) {
bool configUpdate(const T& msg)
{
String _msg = STRING_format(msg);

String data = "{\"deviceName\":\"" + STRING_format(static_cast<Proto*>(this)->_deviceName) + "\"" + \
",\"key\":\"" + STRING_format(static_cast<Proto*>(this)->_authKey) + "\"" + \
",\"config\":\"" + _msg + "\"}";

if (_msg.length() > 256) {
if (_msg.length() > 256)
{
return false;
}
return (blinkServer(BLINKER_CMD_CONFIG_UPDATE_NUMBER, data) == BLINKER_CMD_FALSE) ? false:true;
}

String configGet() {
String configGet()
{
String data = "/pull_userconfig?deviceName=" + STRING_format(static_cast<Proto*>(this)->_deviceName) + \
"&key=" + STRING_format(static_cast<Proto*>(this)->_authKey);

Expand Down Expand Up @@ -2412,7 +2465,7 @@ class BlinkerApi
if (state.length()) {
// _fresh = true;
if (state == BLINKER_CMD_VERSION) {
static_cast<Proto*>(this)->print(BLINKER_CMD_VERSION, BLINKER_VERSION);
static_cast<Proto*>(this)->print(BLINKER_CMD_VERSION, BLINKER_OTA_VERSION_CODE);
_fresh = true;
}
}
Expand Down Expand Up @@ -2900,7 +2953,8 @@ class BlinkerApi


#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || defined(BLINKER_AT_MQTT)
bool autoManager(const JsonObject& data) {
bool autoManager(const JsonObject& data)
{
// String set;
bool isSet = false;
bool isAuto = false;
Expand Down Expand Up @@ -4255,6 +4309,8 @@ class BlinkerApi
return BLINKER_CMD_FALSE;
}
break;
case BLINKER_CMD_OTA_NUMBER :
break;
default :
return BLINKER_CMD_FALSE;
}
Expand Down Expand Up @@ -4482,6 +4538,17 @@ class BlinkerApi

client_s.print(client_msg);

BLINKER_LOG_ALL(BLINKER_F("client_msg: "), client_msg);
break;
case BLINKER_CMD_OTA_NUMBER :
url = "/api/v1/user/device" + msg;

client_msg = STRING_format("GET " + url + " HTTP/1.1\r\n" +
"Host: " + host + ":" + STRING_format(httpsPort) + "\r\n" +
"Connection: close\r\n\r\n");

client_s.print(client_msg);

BLINKER_LOG_ALL(BLINKER_F("client_msg: "), client_msg);
break;
default :
Expand Down Expand Up @@ -4760,6 +4827,22 @@ class BlinkerApi

BLINKER_LOG_ALL(BLINKER_F("_dataGet: "), _dataGet);

break;
case BLINKER_CMD_OTA_NUMBER :
if (data_rp.success()) {
uint16_t msg_code = data_rp[BLINKER_CMD_MESSAGE];
if (msg_code != 1000) {
String _detail = data_rp[BLINKER_CMD_DETAIL];
BLINKER_ERR_LOG(_detail);
}
else {
String _dataGet_ = data_rp[BLINKER_CMD_DETAIL];
_dataGet = _dataGet_;
}
}

BLINKER_LOG_ALL(BLINKER_F("_dataGet: "), _dataGet);

break;
default :
return BLINKER_CMD_FALSE;
Expand Down Expand Up @@ -4890,6 +4973,12 @@ class BlinkerApi
case BLINKER_CMD_AUTO_PULL_NUMBER :
url_iot = String(host) + "/api/v1/user/device" + msg;

http.begin(url_iot);
httpCode = http.GET();
break;
case BLINKER_CMD_OTA_NUMBER :
url_iot = String(host) + "/api/v1/user/device" + msg;

http.begin(url_iot);
httpCode = http.GET();
break;
Expand Down Expand Up @@ -5148,6 +5237,22 @@ class BlinkerApi

BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);

break;
case BLINKER_CMD_OTA_NUMBER :
if (data_rp.success()) {
uint16_t msg_code = data_rp[BLINKER_CMD_MESSAGE];
if (msg_code != 1000) {
String _detail = data_rp[BLINKER_CMD_DETAIL];
BLINKER_ERR_LOG(_detail);
}
else {
String _payload = data_rp[BLINKER_CMD_DETAIL];
payload = _payload;
}
}

BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);

break;
default :
return BLINKER_CMD_FALSE;
Expand Down
37 changes: 32 additions & 5 deletions src/Blinker/BlinkerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@

#define BLINKER_SERIAL_8O2 8 << 4 | 2 << 2 | 1

#define BLINKER_EEP_ADDR_SERIALCFG 2432
// #define BLINKER_EEP_ADDR_SERIALCFG 2432

#define BLINKER_SERIALCFG_SIZE 4
// #define BLINKER_SERIALCFG_SIZE 4

#endif

Expand Down Expand Up @@ -993,6 +993,8 @@

#define BLINKER_CMD_AUTO_PULL_NUMBER 13

#define BLINKER_CMD_OTA_NUMBER 14

#define BLINKER_CMD_DEFAULT_NUMBER 0

#endif
Expand Down Expand Up @@ -1152,11 +1154,11 @@
#define BLINKER_PRO_VERSION "1.0.0"
#endif

#define BLINKER_PRO_VERSION_CODE B00000001
#define BLINKER_OTA_VERSION_CODE B00000001

#define BLINKER_PRO_OTA_START B01010011
#define BLINKER_OTA_START B01010011

#define BLINKER_PRO_OTA_CLEAR B00000001
#define BLINKER_OTA_CLEAR B00000001

#define BLINKER_CHECK_AUTH_TIME 120000UL

Expand Down Expand Up @@ -1192,6 +1194,27 @@

#define BLINKER_OTA_CHECK_SIZE 1

#elif (defined(BLINKER_WIFI) || defined(BLINKER_MQTT) \
|| defined(BLINKER_AT_MQTT))

#ifndef BLINKER_OTA_VERSION_CODE

#define BLINKER_OTA_VERSION_CODE "0.1.0"

#endif

#define BLINKER_OTA_START B01010011

#define BLINKER_OTA_CLEAR B00000001

#define BLINKER_EEP_ADDR_OTA_INFO 2436

#define BLINKER_OTA_INFO_SIZE 11

#define BLINKER_EEP_ADDR_OTA_CHECK (BLINKER_EEP_ADDR_OTA_INFO + BLINKER_OTA_INFO_SIZE)

#define BLINKER_OTA_CHECK_SIZE 1

#endif

#if defined(ESP8266) || defined(ESP32)
Expand Down Expand Up @@ -1293,6 +1316,10 @@

// 56

#define BLINKER_EEP_ADDR_SERIALCFG 2432

#define BLINKER_SERIALCFG_SIZE 4

#endif

#endif
4 changes: 4 additions & 0 deletions src/Blinker/BlinkerProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,8 @@ void BlinkerProtocol<Transp>::run()
strcpy(_authKey, conn.key().c_str());
strcpy(_deviceName, conn.deviceName().c_str());
_proStatus = PRO_DEV_INIT_SUCCESS;

BApi::loadOTA();
}
}
else {
Expand Down Expand Up @@ -2787,6 +2789,8 @@ void BlinkerProtocol<Transp>::run()
if (conn.init() && BApi::ntpInit()) {
_isInit =true;
_disconnectTime = millis();

BApi::loadOTA();

BLINKER_LOG_ALL(BLINKER_F("MQTT conn init success"));
}
Expand Down
Loading

0 comments on commit d672490

Please sign in to comment.