Skip to content

Commit

Permalink
Merge pull request #21 from electricimp/develop
Browse files Browse the repository at this point in the history
Introduced errorPolicy and waitPolicy arguments for the ConnectionManager constructor
  • Loading branch information
Pavel Petroshenko authored Dec 5, 2017
2 parents 8ca2840 + adc7cb0 commit 984df49
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .imptest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"modelId": "yAGfbguy7_mj" /* B */,
"modelId": "Ih8znqaeDT2G" /* D */,
"devices": [
"23236c6938a609ee" /* B */
"23604c058fb7bdee" /* D */
],
"agentFile": false,
"deviceFile": false,
Expand Down
20 changes: 11 additions & 9 deletions ConnectionManager.lib.nut
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class ConnectionManager {
static VERSION = "2.0.0";
static VERSION = "2.1.0";

static BLINK_ALWAYS = 0;
static BLINK_NEVER = 1;
Expand Down Expand Up @@ -57,20 +57,22 @@ class ConnectionManager {

constructor(settings = {}) {
// Grab settings
_checkTimeout = ("checkTimeout" in settings) ? settings.checkTimeout : 5;
_connectTimeout = ("connectTimeout" in settings) ? settings.connectTimeout : 60;
_stayConnected = ("stayConnected" in settings) ? settings.stayConnected : false;
_blinkupBehavior = ("blinkupBehavior" in settings) ? settings.blinkupBehavior : BLINK_ON_DISCONNECT;
_retryOnTimeout = ("retryOnTimeout" in settings) ? settings.retryOnTimeout : true;
local startBehavior = ("startBehavior" in settings) ? settings.startBehavior : START_NO_ACTION;
local ackTimeout = ("ackTimeout" in settings) ? settings.ackTimeout : 1;
_checkTimeout = ("checkTimeout" in settings) ? settings.checkTimeout : 5;
_connectTimeout = ("connectTimeout" in settings) ? settings.connectTimeout : 60;
_stayConnected = ("stayConnected" in settings) ? settings.stayConnected : false;
_blinkupBehavior = ("blinkupBehavior" in settings) ? settings.blinkupBehavior : BLINK_ON_DISCONNECT;
_retryOnTimeout = ("retryOnTimeout" in settings) ? settings.retryOnTimeout : true;
local startBehavior = ("startBehavior" in settings) ? settings.startBehavior : START_NO_ACTION;
local errorPolicy = ("errorPolicy" in settings) ? settings.errorPolicy : RETURN_ON_ERROR;
local waitPolicy = ("waitPolicy" in settings) ? settings.waitPolicy : WAIT_TIL_SENT;
local ackTimeout = ("ackTimeout" in settings) ? settings.ackTimeout : 1;

// Initialize the onConnected task queue and logs
_queue = [];
_logs = [];

// Set the timeout policy + disconnect if required
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, ackTimeout);
server.setsendtimeoutpolicy(errorPolicy, waitPolicy, ackTimeout);

switch (startBehavior) {
case START_NO_ACTION:
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ConnectionManager 2.0.0
# ConnectionManager 2.1.0

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

Expand All @@ -20,10 +20,12 @@ The ConnectionManager class can be instantiated with an optional table of settin
| *blinkupBehavior* | BLINK_ON_DISCONNECT | See below |
| *checkTimeout* | 5 | Changes how often the ConnectionManager checks the connection state (online / offline). |
| *connectTimeout* | 60 | Float. Maximum time (in seconds) allowed for the imp to connect to the server before timing out. |
| *errorPolicy* | RETURN_ON_ERROR | The disconnection handling policy: either SUSPEND_ON_ERROR, RETURN_ON_ERROR or RETURN_ON_ERROR_NO_DISCONNECT. |
| *waitPolicy* | WAIT_TIL_SENT | The successful transmission criterion: either WAIT_TIL_SENT or WAIT_FOR_ACK. |
| *ackTimeout* | 1 | Float. Maximum time (in seconds) allowed for the server to acknowledge receipt of data. |

```squirrel
#require "ConnectionManager.lib.nut:2.0.0"
#require "ConnectionManager.lib.nut:2.1.0"
// Instantiate ConnectionManager so BlinkUp is always enabled,
// and we automatically agressively try to reconnect on disconnect
Expand Down
8 changes: 4 additions & 4 deletions example/ConnectionManager.example.nut
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#require "ConnectionManager.lib.nut:2.0.0"

#require "ConnectionManager.lib.nut:2.1.0"

// Instantiate ConnectionManager so BlinkUp is always enabled,
// and starts connected.
Expand All @@ -35,9 +35,9 @@ cm <- ConnectionManager({

// Set the timeout behaviour after failing to connect for 90 seconds.
cm.onTimeout(function() {
// Go to sleep for 10 minutes
// Go to sleep for 10 minutes
server.sleepfor(600);
});

// Set the recommended buffer size
// Set the recommended buffer size
imp.setsendbuffersize(8096);

0 comments on commit 984df49

Please sign in to comment.