Skip to content
This repository was archived by the owner on Apr 29, 2018. It is now read-only.

Commit 7420b79

Browse files
committed
Changed the eiscp_packet function to use buffers instead. Should be faster but code looks a bit better. [:
1 parent f2464d2 commit 7420b79

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

eiscp.js

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,42 @@ config = {
2222
"reconnect_sleep": 5
2323
};
2424

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+
]);
4545
}
4646

47-
// Exracts message from eISCP packet
4847
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);
6153
}
6254

6355
// Syncronous queue which sends commands to device
6456
send_queue = async.queue(function (data, callback) {
6557

66-
var packet;
67-
6858
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));
7361
if (typeof callback === 'function') {
7462
callback({ "result": true, "msg": "" });
7563
}
@@ -257,7 +245,7 @@ self.discover = function () {
257245
});
258246

259247
client.on("message", function (packet, rinfo) {
260-
var message = eiscp_packet_extract(packet.toString()),
248+
var message = eiscp_packet_extract(packet),
261249
command = message.slice(0, 3),
262250
data;
263251
if (command === "ECN") {
@@ -281,8 +269,8 @@ self.discover = function () {
281269

282270
client.on("listening", function () {
283271
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));
286274
client.send(buffer, 0, buffer.length, options.port, options.address);
287275
timeout_timer = setTimeout(close, options.timeout * 1000);
288276
});
@@ -316,7 +304,10 @@ self.connect = function (options) {
316304
return;
317305
}
318306

319-
connection_properties = {"host": config.host, "port": config.port};
307+
connection_properties = {
308+
host: config.host,
309+
port: config.port
310+
};
320311

321312
self.emit("debug", util.format(STRINGS.connecting, config.host, config.port));
322313

@@ -351,7 +342,7 @@ self.connect = function (options) {
351342

352343
eiscp.on('data', function (data) {
353344

354-
var iscp_message = eiscp_packet_extract(data.toString()),
345+
var iscp_message = eiscp_packet_extract(data),
355346
result = iscp_to_command(iscp_message);
356347

357348
result.iscp_command = iscp_message;
@@ -424,4 +415,4 @@ self.get_command = function (command, callback) {
424415
result.push(val);
425416
}
426417
callback(result);
427-
};
418+
};

strings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"disconnected": "INFO (1003) Disconnected from %s:%s",
55

66
"received_data": "DEBUG (2001) Recevied data from %s:%s - %j",
7-
"sent_command": "DEBUG (2002) Sent command to %s:%s - %s (packet: %s)",
7+
"sent_command": "DEBUG (2002) Sent command to %s:%s - %s",
88
"received_discovery": "DEBUG (2003) Received discovery packet from %s:%s (%j)",
9-
"sent_discovery": "DEBUG (2004) Sent broadcast discovery packet to %s:%s (packet: %s)",
9+
"sent_discovery": "DEBUG (2004) Sent broadcast discovery packet to %s:%s",
1010

1111
"server_error": "ERROR (3001) Server error on %s:%s - %s",
1212
"send_not_connected": "ERROR (3002) Not connected, can't send data: %j",
@@ -15,4 +15,4 @@
1515
"cmd_not_exist": "ERROR (3102) Command %s does not exist in zone %s",
1616
"arg_not_exist": "ERROR (3103) Argument %s does not exist in command %s",
1717
"cmd_parse_error": "ERROR (3104) Command and arguments provided could not be parsed"
18-
}
18+
}

0 commit comments

Comments
 (0)