-
Notifications
You must be signed in to change notification settings - Fork 8
/
Sam.js
78 lines (63 loc) · 2.02 KB
/
Sam.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const net = require('net'),
util = require('util'),
events = require('events'),
uuid = require('uuid');
const i2putil = require("./i2putil");
const LineProtocol = require("./LineProtocol");
module.exports = function() {
var self = this;
LineProtocol.call(self);
self.on('data-line', self.handleLine.bind(self));
self.on('cmdHelloReply', self.handleCmdHelloReply.bind(self));
}
util.inherits(module.exports, LineProtocol);
module.exports.prototype.options = {
host: 'localhost',
port: 7656
};
module.exports.prototype.sendCmd = function (cmd, args) {
var self = this;
cmd = cmd.concat();
for (var key in args) {
if (args[key] == undefined || args[key] == null) {
throw {
args: args,
key: key,
toString: function () {
return "Value can not be undefined or null for " + this.key + " in " + JSON.stringify(this.args);
}
}
}
cmd.push(key + "=" + args[key].toString());
}
cmd = cmd.join(' ');
if (self.debug) console.log(self.localAddress + ":" + self.localPort + ": SEND: " + cmd);
self.writeLine(cmd)
}
module.exports.prototype.handleLine = function (line) {
var self = this;
if (self.debug) console.log(self.localAddress + ":" + self.localPort + ': RECEIVED: ' + line);
var items = line.match(/[^ "]*="(.*)"|([^ "]+)/g);
var cmd = items.slice(0, 2);
var args = {};
items.slice(2).map(function (arg) {
var pos = arg.indexOf("=");
var key = arg.slice(0, pos);
var value = arg.slice(pos + 1);
args[key] = value;
});
if (args.RESULT != undefined && args.RESULT != 'OK') {
console.error(["ERROR", self, {cmd:cmd, args:args}]);
self.emit('error', {cmd:cmd, args:args});
} else {
self.emit('cmd' + cmd.map(i2putil.strToTitle).join(""), args);
}
}
module.exports.prototype.handleConnected = function () {
var self = this;
self.sendCmd(['HELLO', 'VERSION'], {MIN:'2.0', MAX:'4.0'});
}
module.exports.prototype.handleCmdHelloReply = function (data) {
var self = this;
self.VERSION = data.VERSION;
}