Skip to content

Commit 3b28cc2

Browse files
committed
Refector topic string creation into small inline methods
1 parent 5631899 commit 3b28cc2

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP():
7171
_shadowTopicOut(""),
7272
_shadowTopicIn(""),
7373
_dataTopicOut(""),
74-
_dataTopicIn(""),
75-
_otaTopic("")
74+
_dataTopicIn("")
7675
{}
7776

7877
ArduinoIoTCloudTCP::~ArduinoIoTCloudTCP() {
@@ -118,7 +117,12 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) {
118117
_mqttClient->setConnectionTimeout(1500);
119118
_mqttClient->setId(_device_id.c_str());
120119

121-
mqttClientBegin();
120+
_stdinTopic = getTopic_stdin();
121+
_stdoutTopic = getTopic_stdout();
122+
_shadowTopicOut = getTopic_shadowout();
123+
_shadowTopicIn = getTopic_shadowin();
124+
_dataTopicOut = getTopic_dataout();
125+
_dataTopicIn = getTopic_datain();
122126

123127
Thing.begin();
124128
Thing.registerGetTimeCallbackFunc(getTime);
@@ -336,21 +340,6 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection()
336340
return _iotStatus;
337341
}
338342

339-
void ArduinoIoTCloudTCP::mqttClientBegin() {
340-
// MQTT topics definition
341-
_stdoutTopic = "/a/d/" + _device_id + "/s/o";
342-
_stdinTopic = "/a/d/" + _device_id + "/s/i";
343-
if (_thing_id == "") {
344-
_dataTopicIn = "/a/d/" + _device_id + "/e/i";
345-
_dataTopicOut = "/a/d/" + _device_id + "/e/o";
346-
} else {
347-
_dataTopicIn = "/a/t/" + _thing_id + "/e/i";
348-
_dataTopicOut = "/a/t/" + _thing_id + "/e/o";
349-
_shadowTopicIn = "/a/t/" + _thing_id + "/shadow/i";
350-
_shadowTopicOut = "/a/t/" + _thing_id + "/shadow/o";
351-
}
352-
}
353-
354343
int ArduinoIoTCloudTCP::writeProperties(const byte data[], int length) {
355344
if (!_mqttClient->beginMessage(_dataTopicOut, length, false, 0)) {
356345
return 0;

src/ArduinoIoTCloudTCP.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,20 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass {
118118
String _shadowTopicIn;
119119
String _dataTopicOut;
120120
String _dataTopicIn;
121-
String _otaTopic;
121+
122+
inline String getTopic_stdin () const { return String("/a/d/" + _device_id + "/s/i"); }
123+
inline String getTopic_stdout () const { return String("/a/d/" + _device_id + "/s/o"); }
124+
inline String getTopic_shadowout() const { return (_thing_id.length() == 0) ? String("") : String("/a/t/" + _thing_id + "/shadow/o"); }
125+
inline String getTopic_shadowin () const { return (_thing_id.length() == 0) ? String("") : String("/a/t/" + _thing_id + "/shadow/i"); }
126+
inline String getTopic_dataout () const { return (_thing_id.length() == 0) ? String("/a/d/" + _device_id + "/e/o") : String("/a/t/" + _thing_id + "/e/o"); }
127+
inline String getTopic_datain () const { return (_thing_id.length() == 0) ? String("/a/d/" + _device_id + "/e/i") : String("/a/t/" + _thing_id + "/e/i"); }
122128

123129
static void onMessage(int length);
124130
void handleMessage(int length);
125131
void sendPropertiesToCloud();
126132
void requestLastValue();
127133
NetworkConnectionState checkPhyConnection();
128134
ArduinoIoTConnectionStatus checkCloudConnection();
129-
void mqttClientBegin();
130135
int writeStdout(const byte data[], int length);
131136
int writeProperties(const byte data[], int length);
132137
int writeShadowOut(const byte data[], int length);

0 commit comments

Comments
 (0)