Skip to content

Commit 3171534

Browse files
author
Pavel Petroshenko
authored
Merge pull request #17 from electricimp/develop
Develop
2 parents 92bd929 + 4b61d71 commit 3171534

File tree

4 files changed

+102
-42
lines changed

4 files changed

+102
-42
lines changed

Diff for: ConnectionManager.lib.nut

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
// Copyright (c) 2015-2017 Electric Imp
2-
// This file is licensed under the MIT License
3-
// http://opensource.org/licenses/MIT
1+
// MIT License
2+
3+
// Copyright 2015-2017 Electric Imp
4+
5+
// SPDX-License-Identifier: MIT
6+
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
425

526
class ConnectionManager {
6-
static version = [1,1,0];
27+
static VERSION = "2.0.0";
728

829
static BLINK_ALWAYS = 0;
930
static BLINK_NEVER = 1;
1031
static BLINK_ON_CONNECT = 2;
1132
static BLINK_ON_DISCONNECT = 3;
1233
static FLUSH_TIMEOUT = 30;
34+
static START_NO_ACTION = 0;
35+
static START_CONNECTED = 1;
36+
static START_DISCONNECTED = 2;
1337

1438
// Settings
1539
_connectTimeout = null;
@@ -38,8 +62,7 @@ class ConnectionManager {
3862
_stayConnected = ("stayConnected" in settings) ? settings.stayConnected : false;
3963
_blinkupBehavior = ("blinkupBehavior" in settings) ? settings.blinkupBehavior : BLINK_ON_DISCONNECT;
4064
_retryOnTimeout = ("retryOnTimeout" in settings) ? settings.retryOnTimeout : true;
41-
local startDisconnected = ("startDisconnected" in settings) ? settings.startDisconnected : false;
42-
local startConnected = ("startConnected" in settings) ? settings.startConnected : false;
65+
local startBehavior = ("startBehavior" in settings) ? settings.startBehavior : START_NO_ACTION;
4366
local ackTimeout = ("ackTimeout" in settings) ? settings.ackTimeout : 1;
4467

4568
// Initialize the onConnected task queue and logs
@@ -49,13 +72,18 @@ class ConnectionManager {
4972
// Set the timeout policy + disconnect if required
5073
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, ackTimeout);
5174

52-
if (startDisconnected) {
53-
// Disconnect if required
54-
server.disconnect();
55-
_connected = false;
56-
} else if (startConnected) {
57-
// Start connecting if they ask for it
58-
imp.wakeup(0, connect.bindenv(this));
75+
switch (startBehavior) {
76+
case START_NO_ACTION:
77+
// Do nothing
78+
break;
79+
case START_CONNECTED:
80+
// Start connecting if they ask for it
81+
imp.wakeup(0, connect.bindenv(this));
82+
break;
83+
case START_DISCONNECTED:
84+
// Disconnect if required
85+
imp.wakeup(0, disconnect.bindenv(this));
86+
break;
5987
}
6088

6189
// Get the initial state and set BlinkUp accordingly
@@ -159,7 +187,7 @@ class ConnectionManager {
159187
// If we're connecting / disconnecting, try again in 0.5 seconds
160188
if (_connecting) { return false; }
161189

162-
// if we're already disconnected: invoke the onDisconnectedFlow and return
190+
// If we're already disconnected: invoke the onDisconnectedFlow and return
163191
if (!_connected) {
164192
_onDisconnectedFlow(true);
165193
return true;

Diff for: LICENSE

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
The MIT License (MIT)
1+
MIT License
22

3-
Copyright (c) 2015-2017 Electric Imp
3+
Copyright 2015-2017 Electric Imp
4+
5+
SPDX-License-Identifier: MIT
46

57
Permission is hereby granted, free of charge, to any person obtaining a copy
68
of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +11,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
911
copies of the Software, and to permit persons to whom the Software is
1012
furnished to do so, subject to the following conditions:
1113

12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
1416

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
OTHER DEALINGS IN THE SOFTWARE.

Diff for: README.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# ConnectionManager 1.1.0
1+
# ConnectionManager 2.0.0
22

33
The ConnectionManager class is an Electric Imp device-side library aimed at simplifying connect and disconnect flows.
44

55
**Note** If you are using the ConnectionManager class in your model, you should ensure that you *never* call [**server.connect()**](https://electricimp.com/docs/api/server/connect/) or [**server.disconnect()**](https://electricimp.com/docs/api/server/disconnect/) in your application code. Instead you should use the ConnectionManager’s *connect()* and *disconnect()* methods.
66

7-
**To add this library to your project, add** `#require "ConnectionManager.lib.nut:1.1.0"` **to the top of your device code.**
7+
**To add this library to your project, add** `#require "ConnectionManager.lib.nut:2.0.0"` **to the top of your device code.**
88

99
## Class Usage
1010

@@ -14,8 +14,7 @@ The ConnectionManager class can be instantiated with an optional table of settin
1414

1515
| key | default | notes |
1616
| ----------------- | ------------------- | ----- |
17-
| *startConnected* | `false` | When set to `true` the device immediately connects |
18-
| *startDisconnected* | `false` | When set to `true` the device immediately disconnects |
17+
| *startupBehavior* | START_NO_ACTION | See below |
1918
| *stayConnected* | `false` | When set to `true` the device will aggressively attempt to reconnect when disconnected |
2019
| *retryOnTimeout* | `true` | When set to `true` the device will attempt to connect again if it times out. |
2120
| *blinkupBehavior* | BLINK_ON_DISCONNECT | See below |
@@ -24,7 +23,7 @@ The ConnectionManager class can be instantiated with an optional table of settin
2423
| *ackTimeout* | 1 | Float. Maximum time (in seconds) allowed for the server to acknowledge receipt of data. |
2524

2625
```squirrel
27-
#require "ConnectionManager.lib.nut:1.1.0"
26+
#require "ConnectionManager.lib.nut:2.0.0"
2827
2928
// Instantiate ConnectionManager so BlinkUp is always enabled,
3029
// and we automatically agressively try to reconnect on disconnect
@@ -38,10 +37,11 @@ imp.setsendbuffersize(8096);
3837
```
3938

4039
**Note** We’ve found setting the buffer size to 8096 to be very helpful in many applications using the ConnectionManager class, though your application may require a different buffer size.
41-
42-
#### ackTimeout
43-
44-
This value is passed into the imp API method [**server.setsendtimeoutpolicy()**](https://electricimp.com/docs/api/server/setsendtimeoutpolicy/), overriding any value your code may have already set in a separate call to that method (or overridden by a subsequent call your code makes). We recommend that if you make use of ConnectionManager, you ensure that you **never** call [**server.setsendtimeoutpolicy()**](https://electricimp.com/docs/api/server/setsendtimeoutpolicy/) in your application code.
40+
#### startupBehavior
41+
The startupBehavior flag modifies what action the ConnectionManager takes when initialized.
42+
- *ConnectionManager.START_DO_NOTHING* will take no action after being initialized. This is the default value.
43+
- *ConnectionManager.START_CONNECTED* will try to connect after being initialized.
44+
- *ConnectionManager.START_DISCONNECTED* will disconnect after being initialized.
4545

4646
#### blinkupBehavior
4747

@@ -54,6 +54,11 @@ The blinkupBehavior flag modifies when the ConnectionManager enables the BlinkUp
5454

5555
**Note** impOS™ *always* enables the BlinkUp circuit for the first 60 seconds after a cold boot to ensure the imp never enters an unrecoverable state. As a result, regardless of what *blinkupBehavior* flag is set, the imp will enable the BlinkUp circuit for 60 seconds after a cold boot.
5656

57+
#### ackTimeout
58+
59+
This value is passed into the imp API method [**server.setsendtimeoutpolicy()**](https://electricimp.com/docs/api/server/setsendtimeoutpolicy/), overriding any value your code may have already set in a separate call to that method (or overridden by a subsequent call your code makes). We recommend that if you make use of ConnectionManager, you ensure that you **never** call [**server.setsendtimeoutpolicy()**](https://electricimp.com/docs/api/server/setsendtimeoutpolicy/) in your application code.
60+
61+
5762
## Class Methods
5863

5964
### setBlinkUpBehavior(*blinkupBehaviorFlag*)
@@ -65,7 +70,7 @@ The *setBlinkUpBehavior()* method changes the class’ BlinkUp behavior (see [bl
6570
cm.setBlinkUpBehavior(ConnectionManager.BLINK_ON_CONNECT);
6671
```
6772

68-
## isConnected()
73+
### isConnected()
6974

7075
The *isConnected()* method returns the value of ConnectionManager’s internal connected state flag (whether or not we are connected). This flag is updated every five seconds, or as set by the *checkTimeout* setting in the constructor.
7176

@@ -79,7 +84,7 @@ if (!cm.isConnected()) {
7984
}
8085
```
8186

82-
## onDisconnect(*callback*)
87+
### onDisconnect(*callback*)
8388

8489
The *onDisconnect()* method assigns a callback function to the onDisconnect event. The onDisconnect event will fire every time the connection state changes from online to offline, or when the ConnectionManager’s *disconnect()* method is called (even if the device is already disconnected).
8590

@@ -97,7 +102,7 @@ cm.onDisconnect(function(expected) {
97102
});
98103
```
99104

100-
## onConnect(*callback*)
105+
### onConnect(*callback*)
101106

102107
The *onConnect()* method assigns a callback method to the onConnect event. The onConnect event will fire every time the connection state changes from offline to online, or when the ConnectionManager’s *connect()* method is called (even if the device is already connected).
103108

@@ -110,7 +115,7 @@ cm.onConnect(function() {
110115
});
111116
```
112117

113-
## onTimeout(callback)
118+
### onTimeout(callback)
114119

115120
The *onTimeout* method assigns a callback method to the onTimeout event. The onTimeout event will fire every time the device attempts to connect but does not succeed.
116121

@@ -123,7 +128,7 @@ cm.onTimeout(function() {
123128
});
124129
```
125130

126-
## onNextConnect(*callback*)
131+
### onNextConnect(*callback*)
127132

128133
The *onNextConnect()* method queues a task (the callback) to run the next time the device connects. If the imp is already connected, the callback will be invoked immediately.
129134

@@ -150,7 +155,7 @@ function poll() {
150155

151156
**Note** If the imp enters a deep sleep or performs a cold boot, the task queue will be cleared.
152157

153-
## connectFor(*callback*)
158+
### connectFor(*callback*)
154159

155160
The *connectFor()* method tells the imp to connect, run the callback method, then disconnect when complete. If the imp is already connected, the callback will be invoked immediately, and the imp will disconnect upon completion.
156161

Diff for: example/ConnectionManager.example.nut

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
#require "ConnectionManager.lib.nut:1.1.0"
1+
// MIT License
2+
3+
// Copyright 2015-2017 Electric Imp
4+
5+
// SPDX-License-Identifier: MIT
6+
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
26+
#require "ConnectionManager.lib.nut:2.0.0"
227

328
// Instantiate ConnectionManager so BlinkUp is always enabled,
429
// and starts connected.
530
cm <- ConnectionManager({
6-
"startConnected": true,
31+
"startupBehavior": START_CONNECTED,
732
"connectTimeout": 90,
833
"blinkupBehavior": ConnectionManager.BLINK_ALWAYS
934
});

0 commit comments

Comments
 (0)