@@ -22,54 +22,42 @@ config = {
22
22
"reconnect_sleep" : 5
23
23
} ;
24
24
25
- // Wraps data in ISCP container
26
- function iscp_message ( data ) {
27
- // ! = start character
28
- // 1 = destination (1 = receiver)
29
- // \x0D = end character (carriage return)
30
-
31
- return "!1" + data + "\x0D" ;
32
- }
33
-
34
- // Wraps ISCP message in eISCP packet for communicating over Ethernet
35
- function eiscp_packet ( data ) {
36
-
37
- return [
38
- "ISCP" , // magic
39
- "\x00\x00\x00\x10\x00\x00\x00" , // ? no clue
40
- ( + ( data . length + 3 ) ) . toString ( 16 ) , // data length in hex
41
- "\x01" , // version
42
- "\x00\x00\x00" , // reserved
43
- data
44
- ] . join ( '' ) ;
25
+ function eiscp_packet ( data , type ) {
26
+ /*
27
+ Wraps command in eISCP packet for communicating over Ethernet
28
+ type is device type where 1 is receiver and x is for the discovery broadcast
29
+ */
30
+ var iscp_msg = new Buffer ( "!" + ( type || "1" ) + data + "\x0D\x0a" ) ,
31
+ header = new Buffer ( [
32
+ 73 , 83 , 67 , 80 , // magic
33
+ 0 , 0 , 0 , 16 , // header size
34
+ 0 , 0 , 0 , 0 , // data size
35
+ 1 , // version
36
+ 0 , 0 , 0 // reserved
37
+ ] ) ;
38
+
39
+ header . writeUInt32BE ( iscp_msg . length , 8 ) ; // write data size to header
40
+
41
+ return Buffer . concat ( [
42
+ header ,
43
+ iscp_msg
44
+ ] ) ;
45
45
}
46
46
47
- // Exracts message from eISCP packet
48
47
function eiscp_packet_extract ( packet ) {
49
- var message , begin , end = - 1 ;
50
-
51
- begin = packet . indexOf ( "!1" ) + 2 ;
52
- // TODO: I've been getting some different end character so I'm just testing which one is used
53
- end = ( packet . indexOf ( "\r" ) !== - 1 ) ? packet . indexOf ( "\r" ) :
54
- ( packet . indexOf ( "\u001a" ) !== - 1 ) ? packet . indexOf ( "\u001a" ) :
55
- ( packet . indexOf ( "\u0019" ) !== - 1 ) ? packet . indexOf ( "\u0019" ) :
56
- - 1 ;
57
-
58
- message = packet . slice ( begin , end - 1 ) ;
59
-
60
- return message ;
48
+ /*
49
+ Exracts message from eISCP packet
50
+ Strip first 18 bytes and last 3 since that's only the header and end characters
51
+ */
52
+ return packet . toString ( 'ascii' , 18 , packet . length - 3 ) ;
61
53
}
62
54
63
55
// Syncronous queue which sends commands to device
64
56
send_queue = async . queue ( function ( data , callback ) {
65
57
66
- var packet ;
67
-
68
58
if ( self . is_connected ) {
69
- packet = eiscp_packet ( iscp_message ( data ) ) ;
70
-
71
- self . emit ( "debug" , util . format ( STRINGS . sent_command , config . host , config . port , data , packet ) ) ;
72
- eiscp . write ( packet ) ;
59
+ self . emit ( "debug" , util . format ( STRINGS . sent_command , config . host , config . port , data ) ) ;
60
+ eiscp . write ( eiscp_packet ( data ) ) ;
73
61
if ( typeof callback === 'function' ) {
74
62
callback ( { "result" : true , "msg" : "" } ) ;
75
63
}
@@ -257,7 +245,7 @@ self.discover = function () {
257
245
} ) ;
258
246
259
247
client . on ( "message" , function ( packet , rinfo ) {
260
- var message = eiscp_packet_extract ( packet . toString ( ) ) ,
248
+ var message = eiscp_packet_extract ( packet ) ,
261
249
command = message . slice ( 0 , 3 ) ,
262
250
data ;
263
251
if ( command === "ECN" ) {
@@ -281,8 +269,8 @@ self.discover = function () {
281
269
282
270
client . on ( "listening" , function ( ) {
283
271
client . setBroadcast ( true ) ;
284
- var buffer = new Buffer ( eiscp_packet ( '!xECNQSTN' ) ) ;
285
- self . emit ( "debug" , util . format ( STRINGS . sent_discovery , options . address , options . port , "!xECNQSTN" ) ) ;
272
+ var buffer = eiscp_packet ( 'ECNQSTN' , 'x' ) ;
273
+ self . emit ( "debug" , util . format ( STRINGS . sent_discovery , options . address , options . port ) ) ;
286
274
client . send ( buffer , 0 , buffer . length , options . port , options . address ) ;
287
275
timeout_timer = setTimeout ( close , options . timeout * 1000 ) ;
288
276
} ) ;
@@ -316,7 +304,10 @@ self.connect = function (options) {
316
304
return ;
317
305
}
318
306
319
- connection_properties = { "host" : config . host , "port" : config . port } ;
307
+ connection_properties = {
308
+ host : config . host ,
309
+ port : config . port
310
+ } ;
320
311
321
312
self . emit ( "debug" , util . format ( STRINGS . connecting , config . host , config . port ) ) ;
322
313
@@ -351,7 +342,7 @@ self.connect = function (options) {
351
342
352
343
eiscp . on ( 'data' , function ( data ) {
353
344
354
- var iscp_message = eiscp_packet_extract ( data . toString ( ) ) ,
345
+ var iscp_message = eiscp_packet_extract ( data ) ,
355
346
result = iscp_to_command ( iscp_message ) ;
356
347
357
348
result . iscp_command = iscp_message ;
@@ -424,4 +415,4 @@ self.get_command = function (command, callback) {
424
415
result . push ( val ) ;
425
416
}
426
417
callback ( result ) ;
427
- } ;
418
+ } ;
0 commit comments