Skip to content

Commit ad4e8f0

Browse files
committed
overload publish and chat functions
1 parent 34bde07 commit ad4e8f0

File tree

2 files changed

+94
-20
lines changed

2 files changed

+94
-20
lines changed

MicroGear.cpp

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
2626
}
2727
else if (strcmp(rtopic,"&resetendpoint") == 0) {
2828
#ifdef DEBUG_H
29-
Serial.println("RESETTTT-EP");
29+
Serial.println("to reset endpoint");
3030
#endif
3131
if (mg) mg->resetEndpoint();
3232
}
@@ -89,7 +89,7 @@ int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
8989
}
9090

9191
void MicroGear::resetEndpoint() {
92-
writeEEPROM("",EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
92+
writeEEPROM("",EEPROM_ENDPOINTSOFFSET,1);
9393
}
9494

9595
void MicroGear::initEndpoint(Client *client, char* endpoint) {
@@ -562,9 +562,9 @@ bool MicroGear::connect(char* appid) {
562562
}
563563

564564
bool MicroGear::connected() {
565-
if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT;
566-
else return this->mqttclient->connected();
567-
//return this->sockclient->connected();
565+
//if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT;
566+
//else return this->mqttclient->connected();
567+
return this->sockclient->connected();
568568
}
569569

570570
void MicroGear::subscribe(char* topic) {
@@ -583,16 +583,48 @@ void MicroGear::unsubscribe(char* topic) {
583583
mqttclient->unsubscribe(top);
584584
}
585585

586-
void MicroGear::publish(char* topic, char* message) {
587-
publish(topic, message, false);
588-
}
589-
590-
void MicroGear::publish(char* topic, char* message, bool retained) {
586+
bool MicroGear::publish(char* topic, char* message, bool retained) {
591587
char top[MAXTOPICSIZE] = "/";
592588

593589
strcat(top,appid);
594590
strcat(top,topic);
595-
mqttclient->publish(top, message, retained);
591+
return mqttclient->publish(top, message, retained);
592+
}
593+
594+
bool MicroGear::publish(char* topic, char* message) {
595+
return publish(topic, message, false);
596+
}
597+
598+
bool MicroGear::publish(char* topic, double message, int n) {
599+
return publish(topic, message, n, false);
600+
}
601+
602+
bool MicroGear::publish(char* topic, double message, int n, bool retained) {
603+
char mstr[16];
604+
dtostrf(message,0,n,mstr);
605+
return publish(topic, mstr, retained);
606+
}
607+
608+
bool MicroGear::publish(char* topic, double message) {
609+
return publish(topic, message, 8, false);
610+
}
611+
612+
bool MicroGear::publish(char* topic, double message, bool retained) {
613+
return publish(topic, message, 8, retained);
614+
}
615+
616+
bool MicroGear::publish(char* topic, int message) {
617+
return publish(topic, message, 0, false);
618+
}
619+
620+
bool MicroGear::publish(char* topic, int message, bool retained) {
621+
return publish(topic, message, 0, retained);
622+
}
623+
624+
bool MicroGear::publish(char* topic, String message, bool retained) {
625+
char buff[MAXBUFFSIZE];
626+
message.toCharArray(buff,MAXBUFFSIZE);
627+
return publish(topic, buff, retained);
596628
}
597629

598630
/*
@@ -620,10 +652,38 @@ void MicroGear::setAlias(char* gearalias) {
620652
publish(top,"");
621653
}
622654

623-
void MicroGear::chat(char* targetgear, char* message) {
655+
bool MicroGear::chat(char* targetgear, char* message) {
656+
bool result;
624657
char top[MAXTOPICSIZE];
658+
625659
sprintf(top,"/%s/gearname/%s",appid,targetgear);
626-
mqttclient->publish(top, message);
660+
result = mqttclient->publish(top, message);
661+
mqttclient->loop();
662+
return result;
663+
}
664+
665+
bool MicroGear::chat(char* topic, double message, int n) {
666+
bool result;
667+
char mstr[16];
668+
669+
dtostrf(message,0,n,mstr);
670+
result = chat(topic, mstr);
671+
mqttclient->loop();
672+
return result;
673+
}
674+
675+
bool MicroGear::chat(char* topic, double message) {
676+
return chat(topic, message, 8);
677+
}
678+
679+
bool MicroGear::chat(char* topic, int message) {
680+
return chat(topic, message, 0);
681+
}
682+
683+
bool MicroGear::chat(char* topic, String message) {
684+
char buff[MAXBUFFSIZE];
685+
message.toCharArray(buff,MAXBUFFSIZE);
686+
return chat(topic, buff);
627687
}
628688

629689
int MicroGear::init(char* gearkey,char* gearsecret) {

MicroGear.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
#define GBPORT 1883
2121
#define GBSECUREPORT 8883
22-
//#define CAPORT 8079
23-
//#define CAFINGERPRINT "57 BA 02 8A 81 CB 6D D3 26 CE B7 21 7E A8 6C B5 DA D8 5A D0"
24-
2522
#define DEFAULTSECUREMODE false
2623

2724
#define MINBACKOFFTIME 10
2825
#define MAXBACKOFFTIME 10000
2926
#define MAXENDPOINTLENGTH 200
3027
#define MAXTOPICSIZE 128
28+
#define MAXBUFFSIZE 128
3129

3230
#define KEYSIZE 16
3331
#define TOKENSIZE 16
@@ -93,7 +91,7 @@ class MicroGear {
9391
bool clientReadln(Client*, char*, size_t);
9492
void syncTime(Client*, unsigned long*);
9593
void initEndpoint(Client*, char*);
96-
void getToken(char*, char*, char*, char*, char*);
94+
void getToken(char*, char*, char*, char*, char*);
9795

9896
public:
9997
int constate;
@@ -105,11 +103,27 @@ class MicroGear {
105103
void useTLS(bool);
106104
bool connect(char*);
107105
bool connected();
108-
void publish(char*, char*);
109-
void publish(char*, char*, bool);
106+
107+
bool publish(char*, char*);
108+
bool publish(char*, char*, bool);
109+
110+
bool publish(char*, double);
111+
bool publish(char*, double, bool);
112+
bool publish(char*, double, int);
113+
bool publish(char*, double, int, bool);
114+
bool publish(char*, int);
115+
bool publish(char*, int, bool);
116+
bool publish(char*, String);
117+
bool publish(char*, String, bool);
118+
119+
bool chat(char*, char*);
120+
bool chat(char*, int);
121+
bool chat(char*, double);
122+
bool chat(char*, double, int);
123+
bool chat(char*, String);
124+
110125
void subscribe(char*);
111126
void unsubscribe(char*);
112-
void chat(char*, char*);
113127
int state();
114128
void loop();
115129
void resetToken();

0 commit comments

Comments
 (0)