1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/coloz-blinker-library

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
BlinkerMQTTAIR202.h 22 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
某10 Отправлено 6 лет назад 4bf677d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
#ifndef BLINKER_MQTT_AIR202_H
#define BLINKER_MQTT_AIR202_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_MQTT;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_MQTT;
// #endif
#define BLINKER_MQTT_AIR202_DEFAULT_TIMEOUT 5000UL
#define BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE 1024
enum air202_mqtt_status_t
{
mqtt_init,
mqtt_init_success,
mqtt_set,
mqtt_set_ok,
mqtt_set_connect_ok,
mqtt_connect,
mqtt_connect_ok,
mqtt_connect_success,
mqtt_set_sub_topic,
mqtt_set_sub_ok,
mqtt_set_sub_success,
mqtt_set_pub,
mqtt_set_pub_ok,
mqtt_set_pub_success
};
class BlinkerMQTTAIR202
{
public :
BlinkerMQTTAIR202(Stream& s, bool isHardware,
const char * server, uint16_t port,
const char * cid, const char * user,
const char * pass, blinker_callback_t func)
{
stream = &s; isHWS = isHardware;
servername = server; portnum = port;
clientid = cid; username = user;
password = pass; listenFunc = func;
}
~BlinkerMQTTAIR202() { flush(); }
int connect();
int connected();
int disconnect();
void subscribe(const char * topic);
int publish(const char * topic, const char * msg);
int readSubscription(uint16_t time_out = 1000);
char* lastRead;
const char* subTopic;
// char streamData[1024];
char* streamData;
void flush()
{
if(isFresh) free(streamData);
if(isRead) free(lastRead);
isFresh = false;
isRead = false;
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char* streamData;
bool isFresh = false;
bool isHWS = false;
bool isConnected = false;
bool isFreshSub = false;
bool isRead = false;
const char * servername;
uint16_t portnum;
const char * clientid;
const char * username;
const char * password;
uint32_t mqtt_time;
uint32_t connect_time;
uint16_t _mqttTimeout = 5000;
uint32_t _debug_time;
air202_mqtt_status_t mqtt_status;
bool streamAvailable();
void streamPrint(const String & s)
{
BLINKER_LOG_ALL(s);
stream->println(s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
int BlinkerMQTTAIR202::connect()
{
streamPrint(STRING_format(BLINKER_CMD_CSTT_REQ) +
"=\"" + BLINKER_CMD_CMNET + "\"");
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt cstt success"));
// mqtt_status = mqtt_init_success;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_CIICR_REQ));
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt ciicr success"));
// mqtt_status = mqtt_init_success;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
"=\"" + clientid + "\",\"" + username +
"\",\"" + password + "\"");
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt init success"));
mqtt_status = mqtt_init_success;
break;
}
}
}
if (mqtt_status != mqtt_init_success) return false;
streamPrint(STRING_format(BLINKER_CMD_SSLMIPSTART) +
"=\"" + servername + "\"," + STRING_format(portnum));
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_SSLMIPSTART) +
// "=\"" + servername + "\"," + STRING_format(portnum));
mqtt_status = mqtt_set;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set ok"));
mqtt_status = mqtt_set_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_CONNECT_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set connect ok, can connect now"));
mqtt_status = mqtt_set_connect_ok;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MCONNECT_REQ) +
"=1,300");
mqtt_status = mqtt_connect;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set connect, get ok, wait connact"));
mqtt_status = mqtt_connect_ok;
break;
}
}
}
if (mqtt_status != mqtt_connect_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt connacted"));
mqtt_status = mqtt_connect_success;
break;
}
}
}
if (mqtt_status != mqtt_connect_success) return false;
isConnected = true;
// return true;
streamPrint(STRING_format(BLINKER_CMD_MSUB_REQ) +
"=\"" + subTopic + "\",0");
mqtt_status = mqtt_set_sub_topic;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set sub ok"));
mqtt_status = mqtt_set_sub_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_sub_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_SUBACK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set sub success"));
mqtt_status = mqtt_set_sub_success;
break;
}
}
}
if (mqtt_status != mqtt_set_sub_success) return false;
return true;
}
int BlinkerMQTTAIR202::connected()
{
// if (isConnected && millis() - connect_time <= 30000)
// {
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (millis() - _debug_time >= 2000)
// {
// _debug_time = millis();
// BLINKER_LOG_ALL(BLINKER_F("== connected, readSubscription =="));
// }
// if (streamAvailable())
// {
// BLINKER_LOG_ALL(BLINKER_F("== connected available readSubscription =="));
// _masterAT = new BlinkerMasterAT();
// _masterAT->update(STRING_format(streamData));
// if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_MQTTSTATUS)
// {
// if(_masterAT->getParam(0).toInt() == 1)
// {
// free(_masterAT);
// connect_time = millis();
// isConnected = true;
// return true;
// }
// else
// {
// BLINKER_LOG_ALL("mqtt not connected!");
// isConnected = false;
// free(_masterAT);
// return false;
// }
// }
// else if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_MSUB)
// {
// String subData = String(streamData).substring(
// _masterAT->getParam(0).length() +
// _masterAT->getParam(1).length() +
// 10);
// BLINKER_LOG_ALL("sub data: ", subData);
// if (isFreshSub) free(lastRead);
// lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
// strcpy(lastRead, subData.c_str());
// isFreshSub = true;
// connect_time = millis();
// free(_masterAT);
// return true;
// }
// free(_masterAT);
// BLINKER_LOG_ALL("== free connected at master ==");
// }
// }
// return true;
// }
// else
{
if (isFreshSub)
{
connect_time = millis();
return true;
}
if (!isConnected || millis() - connect_time >= 15000)
{
streamPrint(STRING_format(BLINKER_CMD_MQTTSTATU_REQ));
// connect_time = millis();
// }
mqtt_time = millis();
uint8_t status_get = 0;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== connected available =="));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MQTTSTATUS)
{
if(_masterAT->getParam(0).toInt() == 1)
{
free(_masterAT);
connect_time = millis();
isConnected = true;
return true;
}
else
{
BLINKER_LOG_ALL("mqtt not connected!");
isConnected = false;
free(_masterAT);
return false;
}
}
else if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MSUB)
{
String subData = String(streamData).substring(
_masterAT->getParam(0).length() +
_masterAT->getParam(1).length() +
10);
BLINKER_LOG_ALL("sub data: ", subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
strcpy(lastRead, subData.c_str());
isFreshSub = true;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
BLINKER_LOG_ALL("== free at master, return ==");
return true;
}
free(_masterAT);
BLINKER_LOG_ALL("== free connected at master ==");
}
}
}
}
return isConnected;
// return status_get;
}
int BlinkerMQTTAIR202::disconnect()
{
streamPrint(STRING_format(BLINKER_CMD_MDISCONNECT_REQ));
mqtt_time = millis();
uint8_t status_get = 0;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// return true;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MIPCLOSE_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// return true;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_CIPSHUT_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
return true;
// break;
}
}
}
return false;
}
void BlinkerMQTTAIR202::subscribe(const char * topic)
{
subTopic = topic;
// streamPrint(STRING_format(BLINKER_CMD_MPUB_REQ) +
// "=\"" + topic + "\",0");
// mqtt_status = mqtt_set_sub_topic;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set sub ok"));
// mqtt_status = mqtt_set_sub_ok;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_sub_ok) return false;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_SUBACK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set sub success"));
// mqtt_status = mqtt_set_sub_success;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_sub_success) return false;
// return true;
}
int BlinkerMQTTAIR202::publish(const char * topic, const char * msg)
{
streamPrint(STRING_format(BLINKER_CMD_MPUB_REQ) +
"=\"" + topic + "\",0,0,\"" + msg + "\"");
mqtt_status = mqtt_set_pub;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set pub ok"));
mqtt_status = mqtt_set_pub_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_pub_ok) return false;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_PUBACK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set pub success"));
// mqtt_status = mqtt_set_pub_success;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_pub_success) return false;
return true;
}
int BlinkerMQTTAIR202::readSubscription(uint16_t time_out)
{
// if (millis() - _debug_time >= 2000)
// {
// _debug_time = millis();
// BLINKER_LOG_ALL(BLINKER_F("== readSubscription =="));
// }
if (isFreshSub)
{
isFreshSub = false;
return true;
}
else
{
// return false;
// }
mqtt_time = millis();
while(millis() - mqtt_time < time_out)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("readSubscription"));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MSUB)
{
String subData = String(streamData).substring(
_masterAT->getParam(0).length() +
_masterAT->getParam(1).length() +
8);
BLINKER_LOG_ALL(BLINKER_F("mqtt sub data: "), subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
strcpy(lastRead, subData.c_str());
isFreshSub = false;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return false;
}
}
bool BlinkerMQTTAIR202::streamAvailable()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_MQTT->isListening())
// {
// SSerial_MQTT->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE);
// if (!isFresh) streamData = (char*)malloc(BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE*sizeof(char));
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0) streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // streamData = "";
// // memset(streamData, '\0', 1024);
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData);
// // int16_t dNum = strlen(streamData);
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // streamData = stream->readStringUntil('\n');
// // streamData[streamData.length()-1] = '\0';
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial TEST: "), stream->readStringUntil('\n'));
// // stream->flush();
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[strlen(streamData) - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// // free(streamData);
// return false;
// }
}
else
{
return false;
}
}
#endif

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/coloz-blinker-library.git
git@gitlife.ru:oschina-mirror/coloz-blinker-library.git
oschina-mirror
coloz-blinker-library
coloz-blinker-library
master