1
1
function Module ( ) {
2
2
this . sp = null ;
3
3
this . sensorTypes = {
4
- ALIVE :0 ,
5
- DIGITAL :1 ,
6
- ANALOG :2 ,
7
- DHT_PIN :3 ,
8
- LCD :4 ,
9
- CO2 : 5 ,
10
- OLED : 6 ,
11
- TIMER :8 ,
12
- READ_BLUETOOTH : 9 ,
13
- WRITE_BLUETOOTH : 10 ,
4
+ ALIVE : 0 ,
5
+ DIGITAL : 1 ,
6
+ ANALOG : 2 ,
7
+ DHT_PIN : 3 ,
8
+ LCD : 4 ,
9
+ PWM : 5 ,
10
+ SERVO_PIN : 6 ,
11
+ PULSEIN : 7 ,
12
+ ULTRASONIC : 8 ,
13
+ TIMER : 9 ,
14
+ WRITE_SEG : 10 ,
15
+ READ_SEG : 13 ,
16
+ READ_BLUETOOTH : 11 ,
17
+ WRITE_BLUETOOTH : 12 ,
18
+ GAS : 14
14
19
} ;
15
20
this . actionTypes = {
16
21
GET :1 ,
17
22
SET :2 ,
18
23
MODULE :3 ,
19
- RESET :4 ,
24
+ RESET :4
20
25
} ;
21
26
this . sensorValueSize = {
22
27
FLOAT :2 ,
23
28
SHORT :3 ,
24
- STRING :4 ,
29
+ STRING :4
25
30
} ;
26
31
this . digitalPortTimeList = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
27
32
this . sensorData = {
33
+ PULSEIN : { } ,
34
+ ULTRASONIC : 0 ,
28
35
DIGITAL : {
29
36
'0' : 0 ,
30
37
'1' : 0 ,
@@ -50,14 +57,12 @@ function Module() {
50
57
'5' : 0 ,
51
58
} ,
52
59
DHT_PIN : 0 ,
53
- CO2 : 0 ,
54
60
TIMER : 0 ,
55
61
READ_BLUETOOTH : 0 ,
62
+ READ_SEG : 0 ,
56
63
} ;
57
- this . defaultOutput = {
58
- } ;
59
- this . recentCheckData = {
60
- } ;
64
+ this . defaultOutput = { } ;
65
+ this . recentCheckData = { } ;
61
66
this . sendBuffers = [ ] ;
62
67
this . lastTime = 0 ;
63
68
this . lastSendTime = 0 ;
@@ -72,7 +77,8 @@ Module.prototype.setSerialPort = function (sp) {
72
77
this . sp = sp ;
73
78
} ;
74
79
Module . prototype . requestInitialData = function ( ) {
75
- return this . makeSensorReadBuffer ( this . sensorTypes . ANALOG , 0 ) ;
80
+ //return this.makeSensorReadBuffer(this.sensorTypes.ANALOG, 0);
81
+ return true ;
76
82
} ;
77
83
Module . prototype . checkInitialData = function ( data , config ) {
78
84
return true ;
@@ -153,24 +159,33 @@ Module.prototype.handleLocalData = function(data) {
153
159
154
160
switch ( type ) {
155
161
case self . sensorTypes . DIGITAL : {
156
- self . sensorData . DIGITAL [ port ] = value ;
162
+ self . sensorData . DIGITAL [ port ] = value ;
157
163
break ;
158
164
}
159
165
case self . sensorTypes . ANALOG : {
160
- self . sensorData . ANALOG [ port ] = value ;
166
+ self . sensorData . ANALOG [ port ] = value ;
161
167
break ;
162
168
}
163
-
164
169
case self . sensorTypes . DHT_PIN : {
165
- self . sensorData . DHT_PIN = value ;
170
+ self . sensorData . DHT_PIN = value ;
171
+ break ;
172
+ }
173
+ case self . sensorTypes . PULSEIN : {
174
+ self . sensorData . PULSEIN [ port ] = value ;
175
+ break ;
176
+ }
177
+ case self . sensorTypes . ULTRASONIC : {
178
+ self . sensorData . ULTRASONIC = value ;
166
179
break ;
167
180
}
168
-
169
181
case self . sensorTypes . READ_BLUETOOTH : {
170
182
self . sensorData . READ_BLUETOOTH = value ;
171
183
break ;
172
184
}
173
-
185
+ case self . sensorTypes . READ_SEG : {
186
+ self . sensorData . READ_SEG = value ;
187
+ break ;
188
+ }
174
189
case self . sensorTypes . TIMER : {
175
190
self . sensorData . TIMER = value ;
176
191
break ;
@@ -202,105 +217,81 @@ Module.prototype.requestLocalData = function() {
202
217
Module . prototype . makeSensorReadBuffer = function ( device , port , data ) {
203
218
let buffer ;
204
219
const dummy = new Buffer ( [ 10 ] ) ;
205
- if ( device == this . sensorTypes . DHT_PIN ) {
206
- buffer = new Buffer ( [ 255 , 85 , 7 , sensorIdx , this . actionTypes . GET , device , port , 10 ] ) ;
220
+ if ( device == this . sensorTypes . DIGITAL ) {
221
+ if ( ! data ) {
222
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port , 0 , 10 ] ) ;
223
+ } else {
224
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port , data , 10 ] ) ;
225
+ }
226
+ }
227
+ /*
228
+ else if(device == this.sensorTypes.ANALOG) {
229
+ if (!data) {
230
+ buffer = new Buffer([255, 85, 6, sensorIdx, this.actionTypes.GET, device, port, 0, 10]);
231
+ } else {
232
+ buffer = new Buffer([255, 85, 6, sensorIdx, this.actionTypes.GET, device, port, data, 10]);
233
+ }
234
+ }
235
+ */
236
+ else if ( device == this . sensorTypes . DHT_PIN ) {
237
+ //buffer = new Buffer([255, 85, 6, sensorIdx, this.actionTypes.GET, device, port, 10]);
238
+
239
+ if ( ! data ) {
240
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port , 0 , 10 ] ) ;
241
+ } else {
242
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port , data , 10 ] ) ;
243
+ }
244
+
245
+ }
246
+ else if ( device == this . sensorTypes . ULTRASONIC ) {
247
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port [ 0 ] , port [ 1 ] , 10 ] ) ;
207
248
}
208
249
else if ( device == this . sensorTypes . READ_BLUETOOTH ) {
209
250
buffer = new Buffer ( [ 255 , 85 , 5 , sensorIdx , this . actionTypes . GET , device , port , 10 ] ) ;
210
251
}
252
+ else if ( device == this . sensorTypes . READ_SEG ) {
253
+ if ( ! data ) {
254
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . GET , device , port , 0 , 10 ] ) ;
255
+ } else {
256
+ buffer = new Buffer ( [ 255 , 85 , 5 , sensorIdx , this . actionTypes . GET , device , port , 10 ] ) ;
257
+ }
258
+
259
+ }
211
260
else if ( ! data ) {
212
261
buffer = new Buffer ( [ 255 , 85 , 5 , sensorIdx , this . actionTypes . GET , device , port , 10 ] ) ;
262
+
213
263
}
214
264
else {
215
265
value = new Buffer ( 2 ) ;
216
266
value . writeInt16LE ( data ) ;
217
267
buffer = new Buffer ( [ 255 , 85 , 7 , sensorIdx , this . actionTypes . GET , device , port , 10 ] ) ;
218
- buffer = Buffer . concat ( [ buffer , value , dummy ] ) ;
268
+
269
+ buffer = Buffer . concat ( [ buffer , value , dummy ] ) ;
270
+
219
271
}
220
272
221
273
sensorIdx ++ ;
222
274
if ( sensorIdx > 254 ) {
223
275
sensorIdx = 0 ;
224
276
}
225
-
277
+
226
278
return buffer ;
227
279
} ;
228
280
Module . prototype . makeOutputBuffer = function ( device , port , data ) {
229
281
let buffer ;
230
282
const value = new Buffer ( 2 ) ;
231
283
const dummy = new Buffer ( [ 10 ] ) ;
232
284
switch ( device ) {
285
+ case this . sensorTypes . SERVO_PIN :
286
+ case this . sensorTypes . PWM :
233
287
case this . sensorTypes . DIGITAL : {
234
288
value . writeInt16LE ( data ) ;
235
- buffer = new Buffer ( [
236
- 255 ,
237
- 85 ,
238
- 6 ,
239
- sensorIdx ,
240
- this . actionTypes . SET ,
241
- device ,
242
- port ,
243
- ] ) ;
289
+ buffer = new Buffer ( [ 255 , 85 , 6 , sensorIdx , this . actionTypes . SET , device , port , ] ) ;
244
290
buffer = Buffer . concat ( [ buffer , value , dummy ] ) ;
245
291
break ;
246
-
247
- }
248
- case this . sensorTypes . WRITE_BLUETOOTH : {
249
- var text0 = new Buffer ( 2 ) ;
250
- var text1 = new Buffer ( 2 ) ;
251
- var text2 = new Buffer ( 2 ) ;
252
- var text3 = new Buffer ( 2 ) ;
253
- var text4 = new Buffer ( 2 ) ;
254
- var text5 = new Buffer ( 2 ) ;
255
- var text6 = new Buffer ( 2 ) ;
256
- var text7 = new Buffer ( 2 ) ;
257
- var text8 = new Buffer ( 2 ) ;
258
- var text9 = new Buffer ( 2 ) ;
259
- var text10 = new Buffer ( 2 ) ;
260
- var text11 = new Buffer ( 2 ) ;
261
- var text12 = new Buffer ( 2 ) ;
262
- var text13 = new Buffer ( 2 ) ;
263
- var text14 = new Buffer ( 2 ) ;
264
- var text15 = new Buffer ( 2 ) ;
265
- if ( $ . isPlainObject ( data ) ) {
266
- text0 . writeInt16LE ( data . text0 ) ;
267
- text1 . writeInt16LE ( data . text1 ) ;
268
- text2 . writeInt16LE ( data . text2 ) ;
269
- text3 . writeInt16LE ( data . text3 ) ;
270
- text4 . writeInt16LE ( data . text4 ) ;
271
- text5 . writeInt16LE ( data . text5 ) ;
272
- text6 . writeInt16LE ( data . text6 ) ;
273
- text7 . writeInt16LE ( data . text7 ) ;
274
- text8 . writeInt16LE ( data . text8 ) ;
275
- text9 . writeInt16LE ( data . text9 ) ;
276
- text10 . writeInt16LE ( data . text10 ) ;
277
- text11 . writeInt16LE ( data . text11 ) ;
278
- text12 . writeInt16LE ( data . text12 ) ;
279
- text13 . writeInt16LE ( data . text13 ) ;
280
- text14 . writeInt16LE ( data . text14 ) ;
281
- text15 . writeInt16LE ( data . text15 ) ;
282
- } else {
283
- text0 . writeInt16LE ( 0 ) ;
284
- text1 . writeInt16LE ( 0 ) ;
285
- text2 . writeInt16LE ( 0 ) ;
286
- text3 . writeInt16LE ( 0 ) ;
287
- text4 . writeInt16LE ( 0 ) ;
288
- text5 . writeInt16LE ( 0 ) ;
289
- text6 . writeInt16LE ( 0 ) ;
290
- text7 . writeInt16LE ( 0 ) ;
291
- text8 . writeInt16LE ( 0 ) ;
292
- text9 . writeInt16LE ( 0 ) ;
293
- text10 . writeInt16LE ( 0 ) ;
294
- text11 . writeInt16LE ( 0 ) ;
295
- text12 . writeInt16LE ( 0 ) ;
296
- text13 . writeInt16LE ( 0 ) ;
297
- text14 . writeInt16LE ( 0 ) ;
298
- text15 . writeInt16LE ( 0 ) ;
299
- }
300
- buffer = new Buffer ( [ 255 , 85 , 36 , sensorIdx , this . actionTypes . MODULE , device , port ] ) ;
301
- buffer = Buffer . concat ( [ buffer , text0 , text1 , text2 , text3 , text4 , text5 , text6 , text7 , text8 , text9 , text10 , text11 , text12 , text13 , text14 , text15 , dummy ] ) ;
302
- break ;
303
292
}
293
+ case this . sensorTypes . WRITE_SEG :
294
+ case this . sensorTypes . WRITE_BLUETOOTH :
304
295
case this . sensorTypes . LCD : {
305
296
var text0 = new Buffer ( 2 ) ;
306
297
var text1 = new Buffer ( 2 ) ;
@@ -356,70 +347,29 @@ Module.prototype.makeOutputBuffer = function(device, port, data) {
356
347
buffer = new Buffer ( [ 255 , 85 , 36 , sensorIdx , this . actionTypes . MODULE , device , port ] ) ;
357
348
buffer = Buffer . concat ( [ buffer , text0 , text1 , text2 , text3 , text4 , text5 , text6 , text7 , text8 , text9 , text10 , text11 , text12 , text13 , text14 , text15 , dummy ] ) ;
358
349
break ;
359
- }
360
- case this . sensorTypes . OLED : {
361
-
362
- const coordinate_x = new Buffer ( 2 ) ;
363
- const coordinate_y = new Buffer ( 2 ) ;
350
+ }
351
+ /*
352
+ case this.sensorTypes.SEG: {
364
353
var text0 = new Buffer(2);
365
354
var text1 = new Buffer(2);
366
355
var text2 = new Buffer(2);
367
356
var text3 = new Buffer(2);
368
- var text4 = new Buffer ( 2 ) ;
369
- var text5 = new Buffer ( 2 ) ;
370
- var text6 = new Buffer ( 2 ) ;
371
- var text7 = new Buffer ( 2 ) ;
372
- var text8 = new Buffer ( 2 ) ;
373
- var text9 = new Buffer ( 2 ) ;
374
- var text10 = new Buffer ( 2 ) ;
375
- var text11 = new Buffer ( 2 ) ;
376
- var text12 = new Buffer ( 2 ) ;
377
- var text13 = new Buffer ( 2 ) ;
378
- var text14 = new Buffer ( 2 ) ;
379
- var text15 = new Buffer ( 2 ) ;
380
357
if ($.isPlainObject(data)) {
381
- coordinate_x . writeInt16LE ( data . value0 ) ;
382
- coordinate_y . writeInt16LE ( data . value1 ) ;
383
- text0 . writeInt16LE ( data . text0 ) ;
384
- text1 . writeInt16LE ( data . text1 ) ;
385
- text2 . writeInt16LE ( data . text2 ) ;
386
- text3 . writeInt16LE ( data . text3 ) ;
387
- text4 . writeInt16LE ( data . text4 ) ;
388
- text5 . writeInt16LE ( data . text5 ) ;
389
- text6 . writeInt16LE ( data . text6 ) ;
390
- text7 . writeInt16LE ( data . text7 ) ;
391
- text8 . writeInt16LE ( data . text8 ) ;
392
- text9 . writeInt16LE ( data . text9 ) ;
393
- text10 . writeInt16LE ( data . text10 ) ;
394
- text11 . writeInt16LE ( data . text11 ) ;
395
- text12 . writeInt16LE ( data . text12 ) ;
396
- text13 . writeInt16LE ( data . text13 ) ;
397
- text14 . writeInt16LE ( data . text14 ) ;
398
- text15 . writeInt16LE ( data . text15 ) ;
358
+ text0.writeInt16LE(data.text0);
359
+ text1.writeInt16LE(data.text1);
360
+ text2.writeInt16LE(data.text2);
361
+ text3.writeInt16LE(data.text3);
399
362
} else {
400
- coordinate_x . writeInt16LE ( 0 ) ;
401
- coordinate_y . writeInt16LE ( 0 ) ;
402
- text0 . writeInt16LE ( 0 ) ;
403
- text1 . writeInt16LE ( 0 ) ;
404
- text2 . writeInt16LE ( 0 ) ;
405
- text3 . writeInt16LE ( 0 ) ;
406
- text4 . writeInt16LE ( 0 ) ;
407
- text5 . writeInt16LE ( 0 ) ;
408
- text6 . writeInt16LE ( 0 ) ;
409
- text7 . writeInt16LE ( 0 ) ;
410
- text8 . writeInt16LE ( 0 ) ;
411
- text9 . writeInt16LE ( 0 ) ;
412
- text10 . writeInt16LE ( 0 ) ;
413
- text11 . writeInt16LE ( 0 ) ;
414
- text12 . writeInt16LE ( 0 ) ;
415
- text13 . writeInt16LE ( 0 ) ;
416
- text14 . writeInt16LE ( 0 ) ;
417
- text15 . writeInt16LE ( 0 ) ;
363
+ text0.writeInt16LE(0);
364
+ text1.writeInt16LE(0);
365
+ text2.writeInt16LE(0);
366
+ text3.writeInt16LE(0);
418
367
}
419
- buffer = new Buffer ( [ 255 , 85 , 40 , sensorIdx , this . actionTypes . MODULE , device , port ] ) ;
420
- buffer = Buffer . concat ( [ buffer , coordinate_x , coordinate_y , text0 , text1 , text2 , text3 , text4 , text5 , text6 , text7 , text8 , text9 , text10 , text11 , text12 , text13 , text14 , text15 , dummy ] ) ;
368
+ buffer = new Buffer([255, 85, 36 , sensorIdx, this.actionTypes.MODULE, device, port]);
369
+ buffer = Buffer.concat([buffer, text0, text1, text2, text3, dummy]);
421
370
break;
422
371
}
372
+ */
423
373
}
424
374
return buffer ;
425
375
} ;
@@ -460,6 +410,7 @@ Module.prototype.handleRemoteData = function(handler) {
460
410
data : dataObj . data ,
461
411
} ;
462
412
buffer = Buffer . concat ( [ buffer , self . makeSensorReadBuffer ( key , dataObj . port , dataObj . data ) ] ) ;
413
+
463
414
}
464
415
}
465
416
} ) ;
@@ -493,13 +444,14 @@ Module.prototype.disconnect = function(connect) {
493
444
const self = this ;
494
445
connect . close ( ) ;
495
446
if ( self . sp ) {
496
- delete self . sp ;
447
+ delete self . sp ;
497
448
}
498
449
} ;
499
450
500
451
Module . prototype . reset = function ( ) {
501
452
this . lastTime = 0 ;
502
453
this . lastSendTime = 0 ;
454
+ this . sensorData . PULSEIN = { } ;
503
455
} ;
504
456
505
457
module . exports = new Module ( ) ;
0 commit comments