Skip to content

Commit e7840da

Browse files
committed
Refactoring writeProperties/writeStdout/writeShadowOut into a single method since they share most of the code except for a topic parameter which is an additional parameter to the new function 'write'
1 parent 3b28cc2 commit e7840da

File tree

3 files changed

+14
-52
lines changed

3 files changed

+14
-52
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void ArduinoIoTCloudTCP::update() {
166166
if(checkCloudConnection() != ArduinoIoTConnectionStatus::CONNECTED) return;
167167

168168
if(_mqtt_data_request_retransmit && (_mqtt_data_len > 0)) {
169-
writeProperties(_mqtt_data_buf, _mqtt_data_len);
169+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
170170
_mqtt_data_request_retransmit = false;
171171
}
172172

@@ -255,7 +255,7 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud() {
255255
_mqtt_data_len = length;
256256
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
257257
/* Transmit the properties to the MQTT broker */
258-
writeProperties(_mqtt_data_buf, _mqtt_data_len);
258+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
259259
}
260260
}
261261

@@ -264,7 +264,7 @@ void ArduinoIoTCloudTCP::requestLastValue() {
264264
// [{0: "r:m", 3: "getLastValues"}] = 81 A2 00 63 72 3A 6D 03 6D 67 65 74 4C 61 73 74 56 61 6C 75 65 73
265265
// Use http://cbor.me to easily generate CBOR encoding
266266
const uint8_t CBOR_REQUEST_LAST_VALUE_MSG[] = { 0x81, 0xA2, 0x00, 0x63, 0x72, 0x3A, 0x6D, 0x03, 0x6D, 0x67, 0x65, 0x74, 0x4C, 0x61, 0x73, 0x74, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x73 };
267-
writeShadowOut(CBOR_REQUEST_LAST_VALUE_MSG, sizeof(CBOR_REQUEST_LAST_VALUE_MSG));
267+
write(_shadowTopicOut, CBOR_REQUEST_LAST_VALUE_MSG, sizeof(CBOR_REQUEST_LAST_VALUE_MSG));
268268
}
269269

270270
NetworkConnectionState ArduinoIoTCloudTCP::checkPhyConnection()
@@ -340,52 +340,16 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection()
340340
return _iotStatus;
341341
}
342342

343-
int ArduinoIoTCloudTCP::writeProperties(const byte data[], int length) {
344-
if (!_mqttClient->beginMessage(_dataTopicOut, length, false, 0)) {
345-
return 0;
346-
}
347-
348-
if (!_mqttClient->write(data, length)) {
349-
return 0;
350-
}
351-
352-
if (!_mqttClient->endMessage()) {
353-
return 0;
354-
}
355-
356-
return 1;
357-
}
358-
359-
int ArduinoIoTCloudTCP::writeStdout(const byte data[], int length) {
360-
if (!_mqttClient->beginMessage(_stdoutTopic, length, false, 0)) {
361-
return 0;
362-
}
363-
364-
if (!_mqttClient->write(data, length)) {
365-
return 0;
366-
}
367-
368-
if (!_mqttClient->endMessage()) {
369-
return 0;
370-
}
371-
372-
return 1;
373-
}
374-
375-
int ArduinoIoTCloudTCP::writeShadowOut(const byte data[], int length) {
376-
if (!_mqttClient->beginMessage(_shadowTopicOut, length, false, 0)) {
377-
return 0;
378-
}
379-
380-
if (!_mqttClient->write(data, length)) {
381-
return 0;
382-
}
383-
384-
if (!_mqttClient->endMessage()) {
385-
return 0;
343+
int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const length)
344+
{
345+
if (_mqttClient->beginMessage(topic, length, false, 0)) {
346+
if (_mqttClient->write(data, length)) {
347+
if (_mqttClient->endMessage()) {
348+
return 1;
349+
}
350+
}
386351
}
387-
388-
return 1;
352+
return 0;
389353
}
390354

391355
/******************************************************************************

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass {
132132
void requestLastValue();
133133
NetworkConnectionState checkPhyConnection();
134134
ArduinoIoTConnectionStatus checkCloudConnection();
135-
int writeStdout(const byte data[], int length);
136-
int writeProperties(const byte data[], int length);
137-
int writeShadowOut(const byte data[], int length);
135+
int write(String const topic, byte const data[], int const length);
138136
};
139137

140138
/******************************************************************************

src/CloudSerial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void CloudSerialClass::flush() {
7676
out[length++] = _txBuffer.read_char();
7777
}
7878

79-
ArduinoCloud.writeStdout(out, length);
79+
ArduinoCloud.write(ArduinoCloud._stdoutTopic, out, length);
8080
}
8181

8282
size_t CloudSerialClass::write(const uint8_t data) {

0 commit comments

Comments
 (0)