Skip to content

Commit 564ded8

Browse files
committed
add retry
1 parent 0d14d57 commit 564ded8

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ int ArduinoIoTCloudLPWAN::connected()
5252
return state;
5353
}
5454

55-
int ArduinoIoTCloudLPWAN::begin(LPWANConnectionHandler& connection)
55+
int ArduinoIoTCloudLPWAN::begin(LPWANConnectionHandler& connection, bool retry)
5656
{
5757
_connection = &connection;
5858
_connection->init();
59+
_retryEnable = retry;
60+
_maxNumRetry = 5;
61+
_intervalRetry = 1000;
5962
Thing.begin();
6063
return 1;
6164
}
@@ -166,18 +169,37 @@ void ArduinoIoTCloudLPWAN::printDebugInfo() {
166169

167170

168171
int ArduinoIoTCloudLPWAN::writeProperties(const byte data[], int length) {
169-
_connection->write(data, length);
170-
172+
int retcode = _connection->write(data, length);
173+
int i = 0;
174+
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
175+
delay(_intervalRetry);
176+
retcode = _connection->write(data, length);
177+
i++;
178+
}
179+
171180
return 1;
172181
}
173182

174183
int ArduinoIoTCloudLPWAN::writeStdout(const byte data[], int length) {
175-
_connection->write(data, length);
184+
int retcode = _connection->write(data, length);
185+
int i = 0;
186+
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
187+
delay(_intervalRetry);
188+
retcode = _connection->write(data, length);
189+
i++;
190+
}
191+
176192
return 1;
177193
}
178194

179195
int ArduinoIoTCloudLPWAN::writeShadowOut(const byte data[], int length) {
180-
_connection->write(data, length);
196+
int retcode = _connection->write(data, length);
197+
int i = 0;
198+
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
199+
delay(_intervalRetry);
200+
retcode = _connection->write(data, length);
201+
i++;
202+
}
181203
return 1;
182204
}
183205

src/ArduinoIoTCloudLPWAN.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,33 @@ class ArduinoIoTCloudLPWAN : public ArduinoIoTCloudClass {
4141
void update(CallbackFunc onSyncCompleteCallback) __attribute__((deprecated));
4242
void connectionCheck();
4343
void printDebugInfo();
44-
int begin(LPWANConnectionHandler& connection);
44+
int begin(LPWANConnectionHandler& connection, bool retry = false);
4545
inline LPWANConnectionHandler* getConnection() {
4646
return _connection;
4747
}
48+
bool isRetryEnabled() {
49+
return _retryEnable;
50+
}
51+
52+
void enableRetry(bool val) {
53+
_retryEnable = val;
54+
}
55+
56+
int getMaxRetry() {
57+
return _maxNumRetry;
58+
}
4859

60+
void setMaxRetry(int val) {
61+
_maxNumRetry = val;
62+
}
63+
64+
long getIntervalRetry() {
65+
return _intervalRetry;
66+
}
67+
68+
void setIntervalRetry(long val) {
69+
_intervalRetry = val;
70+
}
4971

5072
protected:
5173
int writeStdout(const byte data[], int length);
@@ -55,6 +77,9 @@ class ArduinoIoTCloudLPWAN : public ArduinoIoTCloudClass {
5577
private:
5678
LPWANConnectionHandler* _connection;
5779
void sendPropertiesToCloud();
80+
bool _retryEnable;
81+
int _maxNumRetry;
82+
long _intervalRetry;
5883

5984
};
6085

0 commit comments

Comments
 (0)