Skip to content

Commit b40df5b

Browse files
committed
Reformat code
1 parent dcb835c commit b40df5b

File tree

2 files changed

+147
-137
lines changed

2 files changed

+147
-137
lines changed

src/EasyMqtt.h

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,63 @@
11
#ifndef EasyMqtt_h
2-
#define EasyMqtt_h
2+
#define EasyMqtt_h
33

44
#include <ESP8266WiFi.h>
55
#include <PubSubClient.h>
66
#include "MqttMap.h"
77

88
class EasyMqtt : public MqttMap {
9-
private:
10-
WiFiClient wifiClient;
11-
PubSubClient mqttClient;
12-
13-
String deviceId = "EasyMqtt";
14-
const char* mqtt_username = "N/A";
15-
const char* mqtt_password = "N/A";
9+
private:
10+
WiFiClient wifiClient;
11+
PubSubClient mqttClient;
1612

17-
protected:
18-
virtual String getTopic() {
19-
return String("easyMqtt/") + deviceId;
20-
}
13+
String deviceId = "EasyMqtt";
14+
const char* mqtt_username = "N/A";
15+
const char* mqtt_password = "N/A";
2116

22-
MqttMap & getDiag() {
23-
return operator[]("diag");
24-
}
17+
protected:
18+
virtual String getTopic() {
19+
return String("easyMqtt/") + deviceId;
20+
}
2521

26-
/**
27-
* Handle connections to mqtt
28-
*/
29-
void mqttReconnect() {
30-
while (!mqttClient.connected()) {
31-
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
32-
Serial.println("MQTT connected");
33-
subscribe(mqttClient);
34-
getDiag()["log"].publish(mqttClient, "connected to MQTT");
35-
} else {
36-
Serial.print("failed, rc=");
37-
Serial.print(mqttClient.state());
38-
Serial.println(" try again in 5 seconds");
39-
delay(5000);
40-
}
41-
}
42-
}
22+
MqttMap & getDiag() {
23+
return operator[]("diag");
24+
}
4325

44-
public:
45-
EasyMqtt() : MqttMap("easyMqtt") {
46-
deviceId = String(ESP.getChipId());
26+
/**
27+
Handle connections to mqtt
28+
*/
29+
void mqttReconnect() {
30+
while (!mqttClient.connected()) {
31+
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
32+
Serial.println("MQTT connected");
33+
subscribe(mqttClient);
34+
getDiag()["log"].publish(mqttClient, "connected to MQTT");
35+
} else {
36+
Serial.print("failed, rc=");
37+
Serial.print(mqttClient.state());
38+
Serial.println(" try again in 5 seconds");
39+
delay(5000);
40+
}
41+
}
42+
}
43+
44+
public:
45+
EasyMqtt() : MqttMap("easyMqtt") {
46+
deviceId = String(ESP.getChipId());
4747

4848
getDiag().setInterval(30);
49-
getDiag()["mem"]["heap"] << [](){ return String(ESP.getFreeHeap()); };
50-
getDiag()["uptime"] << [](){ return String(millis()); };
51-
}
49+
getDiag()["mem"]["heap"] << []() {
50+
return String(ESP.getFreeHeap());
51+
};
52+
getDiag()["uptime"] << []() {
53+
return String(millis());
54+
};
55+
}
5256

53-
/**
54-
* Setup connection to wifi
55-
*/
56-
void wifi(const char* ssid, const char* password) {
57+
/**
58+
Setup connection to wifi
59+
*/
60+
void wifi(const char* ssid, const char* password) {
5761
WiFi.mode(WIFI_STA);
5862
WiFi.begin(ssid, password);
5963

@@ -68,30 +72,36 @@ class EasyMqtt : public MqttMap {
6872
Serial.print("Chip ID : ");
6973
Serial.println(ESP.getChipId());
7074

71-
// Setup wifi diag
72-
getDiag()["wifi"]["rssi"] << [](){ return String(WiFi.RSSI()); };
73-
getDiag()["wifi"]["ssid"] << [](){ return WiFi.SSID(); };
74-
}
75+
// Setup wifi diag
76+
getDiag()["wifi"]["rssi"] << []() {
77+
return String(WiFi.RSSI());
78+
};
79+
getDiag()["wifi"]["ssid"] << []() {
80+
return WiFi.SSID();
81+
};
82+
}
7583

76-
/**
77-
* Setup connection to mqtt
78-
*/
79-
void mqtt(const char* host, int port, const char* username, const char* password) {
80-
mqttClient.setClient(wifiClient);
81-
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) { callback(topic, payload, length); });
82-
mqttClient.setServer(host, port);
83-
mqtt_username = username;
84-
mqtt_password = password;
85-
}
84+
/**
85+
Setup connection to mqtt
86+
*/
87+
void mqtt(const char* host, int port, const char* username, const char* password) {
88+
mqttClient.setClient(wifiClient);
89+
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
90+
callback(topic, payload, length);
91+
});
92+
mqttClient.setServer(host, port);
93+
mqtt_username = username;
94+
mqtt_password = password;
95+
}
8696

87-
/**
88-
* Handle the normal loop
89-
*/
90-
void loop() {
91-
mqttReconnect();
92-
mqttClient.loop();
93-
MqttMap::loop(mqttClient);
94-
}
97+
/**
98+
Handle the normal loop
99+
*/
100+
void loop() {
101+
mqttReconnect();
102+
mqttClient.loop();
103+
MqttMap::loop(mqttClient);
104+
}
95105
};
96106

97107
#endif

src/MqttMap.h

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,143 @@
11
#ifndef MqttMap_h
2-
#define MqttMap_h
2+
#define MqttMap_h
33

44
#include <Arduino.h>
55
#include <PubSubClient.h>
66

77
class MqttMap {
8-
private:
9-
void (*outFunction)(String payload) = NULL;
10-
String (*inFunction)() = NULL;
8+
private:
9+
void (*outFunction)(String payload) = NULL;
10+
String (*inFunction)() = NULL;
1111

12-
const char* name = "N/A";
12+
const char* name = "N/A";
1313
int interval = -1;
1414
unsigned long lastUpdate = 0; // Last read of data
1515

1616
MqttMap* parent = NULL;
1717
MqttMap* next = NULL;
1818
MqttMap* children = NULL;
1919

20-
protected:
20+
protected:
2121
MqttMap(const char* _name, MqttMap& _parent) {
2222
name = _name;
2323
parent = &_parent;
2424
interval = -1;
2525
}
2626

2727
virtual String getTopic() {
28-
if(parent) {
28+
if (parent) {
2929
return parent->getTopic() + "/" + name;
3030
} else {
3131
return String(name);
3232
}
3333
}
3434

35-
public:
36-
MqttMap(const char* _name) {
37-
name = _name;
35+
public:
36+
MqttMap(const char* _name) {
37+
name = _name;
3838
interval = 5;
39-
}
39+
}
4040

41-
int getInterval() {
42-
if(interval < 0) {
41+
int getInterval() {
42+
if (interval < 0) {
4343
return parent->getInterval();
4444
}
4545
return interval;
4646
}
4747

48-
void setInterval(int _interval) {
49-
interval = _interval;
50-
}
48+
void setInterval(int _interval) {
49+
interval = _interval;
50+
}
5151

52-
void publish(PubSubClient& client, String message) {
52+
void publish(PubSubClient& client, String message) {
5353
String topic = getTopic();
54-
printf("publish[%s]: %s\n", topic.c_str(), message.c_str());
54+
// printf("publish[%s]: %s\n", topic.c_str(), message.c_str());
5555
client.publish(topic.c_str(), message.c_str());
5656
}
5757

58-
void callback(const char* _topic, uint8_t* payload, unsigned int length) {
59-
if(strcmp(getTopic().c_str(), _topic) == 0) {
60-
if(outFunction != NULL) {
61-
char _payload[length];
62-
for (int i=0;i<length;i++) {
63-
_payload[i] = (char)payload[i];
58+
void callback(const char* _topic, uint8_t* payload, unsigned int length) {
59+
if (strcmp(getTopic().c_str(), _topic) == 0) {
60+
if (outFunction != NULL) {
61+
String _payload = "";
62+
for (int i = 0; i < length; i++) {
63+
_payload += (char)payload[i];
6464
}
65-
printf("Found callback, %s: %s\n", _topic, _payload);
66-
outFunction(String(_payload));
65+
// printf("Found callback, %s: %s\n", _topic, _payload.c_str());
66+
outFunction(_payload);
6767
}
6868
} else {
6969
MqttMap* child = children;
70-
while(child != NULL) {
70+
while (child != NULL) {
7171
child->callback(_topic, payload, length);
7272
child = child->next;
7373
}
74-
}
75-
}
76-
77-
/**
78-
*
79-
*/
80-
void subscribe(PubSubClient& client) {
81-
if(outFunction != NULL) {
74+
}
75+
}
76+
77+
/**
78+
79+
*/
80+
void subscribe(PubSubClient& client) {
81+
if (outFunction != NULL) {
8282
String topic = getTopic();
8383
printf("Subscribe: %s\n", topic.c_str());
8484
client.subscribe(topic.c_str());
8585
}
8686
MqttMap* child = children;
87-
while(child != NULL) {
87+
while (child != NULL) {
8888
child->subscribe(client);
8989
child = child->next;
9090
}
91-
}
91+
}
92+
93+
/**
9294
93-
/**
94-
*
95-
*/
96-
void loop(PubSubClient& client) {
97-
if(inFunction != NULL) {
95+
*/
96+
void loop(PubSubClient& client) {
97+
if (inFunction != NULL) {
9898
unsigned long time = millis();
99-
if(time >= (lastUpdate + (getInterval() * 1000))) {
100-
lastUpdate = time;
101-
String value = inFunction();
102-
if(value != "") {
103-
publish(client, value);
104-
}
99+
if (time >= (lastUpdate + (getInterval() * 1000))) {
100+
lastUpdate = time;
101+
String value = inFunction();
102+
if (value != "") {
103+
publish(client, value);
104+
}
105105
}
106106
}
107107
MqttMap* child = children;
108-
while(child != NULL) {
109-
child->loop(client);;
110-
child = child->next;
111-
}
112-
}
113-
114-
/**
115-
*
116-
*/
117-
MqttMap & operator[](const char* name) {
118-
MqttMap * child = children;
119-
while(child != NULL) {
120-
if(strcmp(child->name, name) == 0) {
121-
return *child;
122-
}
123-
child = child->next;
108+
while (child != NULL) {
109+
child->loop(client);;
110+
child = child->next;
111+
}
112+
}
113+
114+
/**
115+
116+
*/
117+
MqttMap & operator[](const char* name) {
118+
MqttMap * child = children;
119+
while (child != NULL) {
120+
if (strcmp(child->name, name) == 0) {
121+
return *child;
124122
}
125-
MqttMap * oldChild = children;
126-
children = new MqttMap(name, *this);
127-
children->next = oldChild;
128-
return *children;
129-
}
123+
child = child->next;
124+
}
125+
MqttMap * oldChild = children;
126+
children = new MqttMap(name, *this);
127+
children->next = oldChild;
128+
return *children;
129+
}
130130

131131
/**
132-
* Read data from function and send it to mqtt
133-
*/
132+
Read data from function and send it to mqtt
133+
*/
134134
void operator<<(String (*inFunction)()) {
135135
MqttMap::inFunction = inFunction;
136136
}
137137

138138
/**
139-
* Handle data comming from mqtt
140-
*/
139+
Handle data comming from mqtt
140+
*/
141141
void operator>>(void (*outFunction)(String payload)) {
142142
MqttMap::outFunction = outFunction;
143143
}

0 commit comments

Comments
 (0)