From 0de639dcb032250256a99f8acefb98f1e6b238a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anian=20B=C3=BChler?= <anian.buehler@reutlingen-university.de> Date: Tue, 19 Oct 2021 11:27:11 +0200 Subject: [PATCH] added publish methods for bool, int, onChange --- src/didacticNet.cpp | 104 ++++++++++++-------------------------------- src/didacticNet.h | 10 +++-- 2 files changed, 35 insertions(+), 79 deletions(-) diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index 1b69eb7..bb617b3 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -156,35 +156,7 @@ didacticPSNetClient::didacticPSNetClient(){ didacticPSNetClient::~didacticPSNetClient(){} int didacticPSNetClient::publish(char* topic, char* payload){ - int error = DN_ERROR_NO_ERROR; - - int topicLength = strlen(topic); - int payloadLength = strlen(payload); - _sendBufferMessage[0] = MSG_PRELIMITER; - _sendBufferMessage[1] = MSG_PUBLISH; - _sendBufferMessage[2+topicLength] = MSG_SEPARATOR; - _sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER; - _sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0'; - - //TODO: check - if(topicLength > MAX_LEN_TOPICS){ - topicLength = MAX_LEN_TOPICS; - error += DN_ERROR_TOPIC_LEN; - } - if(payloadLength > MAX_LEN_PAYLOAD){ - payloadLength = MAX_LEN_PAYLOAD; - error += DN_ERROR_PAYLOAD_LEN; - } - - for(int i = 0; i < topicLength; i++){ - _sendBufferMessage[2+i] = topic[i]; - } - for(int i = 0; i < payloadLength; i++){ - _sendBufferMessage[2+topicLength+1+i] = payload[i]; - } - - _dataToSend = true; - return error; + return publish(topic, strlen(topic), payload, strlen(payload)); } int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , int payloadLength){ @@ -217,46 +189,32 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i return error; } -int didacticPSNetClient::subscribe(char* topic){ - int error = DN_ERROR_NO_ERROR; - - int topicLength = strlen(topic); +//TODO: TEST TEST TEST +int didacticPSNetClient::publish(char* topic, int data){ + char sendPayload[MAX_LEN_PAYLOAD]; + itoa(data, sendPayload, 10); - if( topicLength > MAX_LEN_TOPICS){ - topicLength = MAX_LEN_TOPICS; - error = DN_ERROR_TOPIC_LEN; - } - - _sendBufferMessage[0] = MSG_PRELIMITER; - _sendBufferMessage[1] = MSG_SUBSCRIBE; - _sendBufferMessage[2+topicLength] = MSG_DELIMITER; - - int topicNumber = getTopicNr(topic); + return publish(topic, strlen(topic), sendPayload, strlen(sendPayload)); +} - if(topicNumber < 0){ - topicNumber = getFreeTopicNr(); - if(topicNumber < 0){ - topicNumber = 0; - } - for(int i = 0; i < topicLength; i++){ - _topic[topicNumber][i] = topic[i]; - _sendBufferMessage[2+i]= topic[i]; - } - _topic[topicNumber][topicLength] = '\0'; - _sendBufferMessage[2+topicLength+1]= '\0'; - _dataToSend = true; +int didacticPSNetClient::publishOnChange(char* topic, bool input){ + if(edgeDetected(input)){ + return publish(topic, (int)input); } - else{ - for(int i = 0; i < topicLength; i++){ - _sendBufferMessage[2+i]= topic[i]; - } - _sendBufferMessage[2+topicLength+1]= '\0'; - _dataToSend = true; - } - while(_dataToSend){ - handleNetwork(); + //TODO: change NO_ERROR to PUBLISHED in basic publish() + return DN_ERROR_NO_ERROR; +} + +int didacticPSNetClient::publishOnChange(char* topic, int input, int threshold){ + if(valueChanged(input, threshold)){ + return publish(topic, input); } - return error; + //TODO: change NO_ERROR to PUBLISHED in basic publish() + return DN_ERROR_NO_ERROR; +} + +int didacticPSNetClient::subscribe(char* topic){ + return subscribe(topic, strlen(topic)); } int didacticPSNetClient::subscribe(char* topic, int topicLength){ @@ -301,13 +259,7 @@ int didacticPSNetClient::subscribe(char* topic, int topicLength){ bool didacticPSNetClient::unsubscribe(char* topic){ - int topicNumber = getTopicNr(topic); - int topicLength = strlen(topic); - if(topicNumber >= 0){ - _topic[topicNumber][0]='\0'; - return true; - } - return false; + return unsubscribe(topic, strlen(topic)); } bool didacticPSNetClient::unsubscribe(char* topic, int topicLength){ @@ -362,9 +314,6 @@ int didacticPSNetClient::getFreeTopicNr() { return -1; } -//************************************************************************** -//LITTLE HELPERS FOR CLIENTS ;-) -//************************************************************************** int didacticPSNetClient::edgeDetected(bool edCurrentState){ static bool edLastState = false; int edEdge = 0; @@ -389,6 +338,9 @@ bool didacticPSNetClient::valueChanged(int vcValue, int vcThreshold){ return false; } +//************************************************************************** +//LITTLE HELPERS FOR CLIENTS ;-) +//************************************************************************* bool didacticPSNetClient::timeElapsed(long teDelayTime){ static long teLastTime = 0L; long currentTime = millis(); @@ -424,7 +376,7 @@ int didacticPSNetClient::readSerialData(Stream& rsStream, char* rsDataArray, cha //Broker //************************************************************************** didacticPSNetBroker::didacticPSNetBroker(){ - _intervalTime = INTERVAL_BROKER; + _intervalTime = INTERVAL_BROKER; } didacticPSNetBroker::~didacticPSNetBroker(){} diff --git a/src/didacticNet.h b/src/didacticNet.h index 13250d6..dfe7232 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -61,7 +61,7 @@ class didacticPSNet bool _dataToSend = false; // int Data to send for queue? unsigned long _waitingTime = 0L; - unsigned long _intervalTime; + unsigned long _intervalTime = 0L; int _currentTopicLength = 0; int _currentPayloadLength = 0; @@ -104,20 +104,24 @@ class didacticPSNetClient : public didacticPSNet int getTopicNr(char*); int getFreeTopicNr(); + int edgeDetected(bool); + bool valueChanged(int, int); + public: didacticPSNetClient(); ~didacticPSNetClient(); int publish(char*, char*); int publish(char*, int, char*, int); + int publish(char*, int); + int publishOnChange(char*, bool); + int publishOnChange(char*, int, int); int subscribe(char*); int subscribe(char*, int); bool unsubscribe(char*); bool unsubscribe(char*, int); //little helpers - int edgeDetected(bool); - bool valueChanged(int, int); bool timeElapsed(long); int readSerialData(Stream&, char*, char); }; -- GitLab