Skip to content

Commit 9b6b8e5

Browse files
committed
Add BoardEvent.RECONNECT when device reconnected
1 parent f88b634 commit 9b6b8e5

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

examples/basic.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
// test: should not emit 'disconnect' again
6060
board.disconnect();
6161
});
62+
63+
board.on(webduino.BoardEvent.RECONNECT, function () {
64+
console.log("board reconnect");
65+
});
6266
</script>
6367
</head>
6468

examples/node/basic.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ board.on(webduino.BoardEvent.DISCONNECT, function () {
5858
// test: should not emit 'disconnect' again
5959
board.disconnect();
6060
});
61+
62+
board.on(webduino.BoardEvent.RECONNECT, function() {
63+
console.log("board reconnect");
64+
});

src/core/Board.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
READY: 'ready',
2828
ERROR: 'error',
2929
BEFOREDISCONNECT: 'beforeDisconnect',
30-
DISCONNECT: 'disconnect'
30+
DISCONNECT: 'disconnect',
31+
RECONNECT: 'reconnect'
3132
};
3233

3334
/**
@@ -98,6 +99,7 @@
9899

99100
this._initialVersionResultHandler = onInitialVersionResult.bind(this);
100101
this._openHandler = onOpen.bind(this);
102+
this._reOpenHandler = onReOpen.bind(this);
101103
this._messageHandler = onMessage.bind(this);
102104
this._errorHandler = onError.bind(this);
103105
this._closeHandler = onClose.bind(this);
@@ -125,6 +127,11 @@
125127
this.begin();
126128
}
127129

130+
function onReOpen() {
131+
this._logger.info("onReOpen", "Device re-online");
132+
this.emit(BoardEvent.RECONNECT);
133+
}
134+
128135
function onMessage(data) {
129136
try {
130137
this._logger.info('onMessage', data);
@@ -556,7 +563,7 @@
556563
proto.sendDigitalData = function (pin, value) {
557564
try {
558565
var portNum = Math.floor(pin / 8);
559-
566+
560567
if (value === Pin.HIGH) {
561568
// Set the bit
562569
this._digitalPort[portNum] |= (value << (pin % 8));
@@ -614,6 +621,7 @@
614621
trsp.on(TransportEvent.MESSAGE, this._messageHandler);
615622
trsp.on(TransportEvent.ERROR, this._errorHandler);
616623
trsp.on(TransportEvent.CLOSE, this._closeHandler);
624+
trsp.on(TransportEvent.REOPEN, this._reOpenHandler);
617625
this._transport = trsp;
618626
}
619627
};

src/core/Transport.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,40 @@
1313
proto;
1414

1515
var TransportEvent = {
16-
1716
/**
1817
* Fires when a transport is opened.
19-
*
18+
*
2019
* @event TransportEvent.OPEN
2120
*/
22-
OPEN: 'open',
21+
OPEN: "open",
2322

2423
/**
2524
* Fires when a transport receives a message.
26-
*
25+
*
2726
* @event TransportEvent.MESSAGE
2827
*/
29-
MESSAGE: 'message',
28+
MESSAGE: "message",
3029

3130
/**
3231
* Fires when a transport get an error.
33-
*
32+
*
3433
* @event TransportEvent.ERROR
3534
*/
36-
ERROR: 'error',
35+
ERROR: "error",
3736

3837
/**
3938
* Fires when a transport is closed.
40-
*
39+
*
4140
* @event TransportEvent.CLOSE
4241
*/
43-
CLOSE: 'close'
42+
CLOSE: "close",
43+
44+
/**
45+
* Fires when a transport is re-opened.
46+
*
47+
* @event TransportEvent.REOPEN
48+
*/
49+
REOPEN: "reopen"
4450
};
4551

4652
/**

src/transport/MqttTransport.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@
9797

9898
function detectStatusChange(self, newStatus, oldStatus) {
9999
if (newStatus === oldStatus) {
100+
if (newStatus === STATUS.OK) {
101+
// Device reconnected
102+
self.emit(TransportEvent.REOPEN);
103+
}
100104
return;
101105
}
102106

src/transport/NodeMqttTransport.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ function onMessage(topic, message) {
9999

100100
function detectStatusChange(self, newStatus, oldStatus) {
101101
if (newStatus === oldStatus) {
102+
if (newStatus === STATUS.OK) {
103+
// Device reconnected
104+
self.emit(TransportEvent.REOPEN);
105+
}
102106
return;
103107
}
104108

@@ -109,10 +113,7 @@ function detectStatusChange(self, newStatus, oldStatus) {
109113
}
110114
}
111115

112-
function onDisconnect(err) {
113-
if (err && !this._options.autoReconnect) {
114-
this.emit(TransportEvent.ERROR, err);
115-
}
116+
function onDisconnect() {
116117
if (this._client.disconnecting || !this._options.autoReconnect) {
117118
this._client.removeAllListeners();
118119
delete this._client;

0 commit comments

Comments
 (0)