Skip to content

Commit 2460912

Browse files
committed
Merge branch 'property-sendingInterval'
2 parents 22b924c + 12e457b commit 2460912

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

examples/send-interval.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<html>
2+
3+
<head>
4+
<script src="../dist/webduino-all.js"></script>
5+
<script>
6+
7+
(async function () {
8+
var board, led, vv;
9+
var tt = 0;
10+
11+
window.sboard = board = new webduino.WebArduino('O9Qq');
12+
13+
board.on('ready', async () => {
14+
board.systemReset();
15+
board.samplingInterval = 250;
16+
board.sendingInterval = 50;
17+
led = new webduino.module.Led(board, board.getDigitalPin(13));
18+
vv = 0;
19+
20+
for (var count = 0; count < 100; count++) {
21+
if (vv == 0) {
22+
vv = 1;
23+
led.on(() => msg(`${tt++}_light`));
24+
} else {
25+
vv = 0;
26+
led.off(() => msg(`${tt++}_dark`));
27+
}
28+
await delay(0.02);
29+
}
30+
await delay(0.5);
31+
msg(`end, ${tt}`);
32+
console.log('end');
33+
});
34+
35+
function delay(t) {
36+
return new Promise(function (resolve, reject) {
37+
setTimeout(function () {
38+
resolve();
39+
}, 1000 * t);
40+
});
41+
}
42+
43+
function msg(val) {
44+
console.log('TCL: msg -> val', val);
45+
document.getElementById("demo-area-01-show").innerHTML = val;
46+
}
47+
48+
})();
49+
</script>
50+
</head>
51+
52+
<body>
53+
<div>start</div>
54+
<div id='demo-area-01-show'></div>
55+
</body>
56+
57+
</html>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"license": "MIT",
1313
"scripts": {
1414
"lint": "eslint src/**/*.js",
15+
"dev": "gulp watch",
1516
"build": "./node_modules/.bin/gulp"
1617
},
1718
"devDependencies": {

src/core/Board.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
this._transport = null;
9797
this._pinStateEventCenter = new EventEmitter();
9898
this._logger = new Logger('Board');
99+
this._sendingInterval = 0;
100+
this._sendingRec = [];
99101

100102
this._initialVersionResultHandler = onInitialVersionResult.bind(this);
101103
this._openHandler = onOpen.bind(this);
@@ -211,6 +213,16 @@
211213
}
212214
},
213215

216+
sendingInterval: {
217+
get: function () {
218+
return this._sendingInterval;
219+
},
220+
set: function (interval) {
221+
if (typeof interval !== 'number') return;
222+
this._sendingInterval = interval < 0 ? 0: interval;
223+
}
224+
},
225+
214226
isReady: {
215227
get: function () {
216228
return this._isReady;
@@ -865,7 +877,24 @@
865877
};
866878

867879
proto.send = function (data) {
868-
this.isConnected && this._transport.send(data);
880+
if (!this.isConnected) return;
881+
if (this.sendingInterval === 0) {
882+
this._transport.send(data);
883+
return;
884+
}
885+
886+
var idx = this._sendingRec.findIndex(function (val) {
887+
return val.value.toString() === data.toString();
888+
});
889+
if (idx !== -1) {
890+
if (Date.now() - this._sendingRec[idx].timestamp < this.sendingInterval) return;
891+
this._sendingRec.splice(idx, 1);
892+
}
893+
this._sendingRec.push({
894+
value: data.slice(),
895+
timestamp: Date.now()
896+
});
897+
this._transport.send(data);
869898
};
870899

871900
proto.close = function (callback) {

0 commit comments

Comments
 (0)