forked from normen/maxcube2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxcube-lowlevel.js
109 lines (87 loc) · 2.76 KB
/
maxcube-lowlevel.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var net = require('net');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Promise = require('bluebird');
function MaxCubeLowLevel(ip, port){
this.ip = ip;
this.port = port;
this.socket = new net.Socket();
this.isConnected = false;
process.on('uncaughtException', (err) => {
console.error('Uncaught exception error: ', err);
});
initSocket.call(this);
}
util.inherits(MaxCubeLowLevel, EventEmitter);
function initSocket () {
var self = this;
var previousPacketData = '';
this.socket.on('data', function(dataBuff) {
var dataStr = dataBuff.toString('utf-8');
if (!dataStr.endsWith('\r\n')) {
previousPacketData = dataStr;
return;
}
// Delete timeout after data has been received
self.socket.setTimeout( 0 );
dataStr = previousPacketData + dataStr;
previousPacketData = '';
// multiple commands possible
var commandArr = dataStr.split("\r\n");
commandArr.forEach(function (command) {
if (command) {
var commandType = command.substr(0, 1);
var payload = command.substring(2) + "\r\n"; // reappend delimiter
self.emit('command', { type: commandType, payload: payload });
}
});
});
// If send has been called with timeout, this event will be emitted if there is no activity on the stream for the configured timeout
this.socket.on( 'timeout', function() {
console.error( 'Maxcube: Connection timed out.' );
// Timeout indicates a problem with the Cube, so emit error
self.emit( 'error', new Error( 'Maxcube: Connection timed out.' ) );
} );
this.socket.on('close', function() {
self.isConnected = false;
self.emit('closed');
});
this.socket.on('error', function(err) {
console.error(err);
self.emit('error', err);
});
}
function connect () {
var self = this;
if (self.isConnected) {
self.connectionPromise = Promise.resolve();
} else if (!(self.connectionPromise && self.connectionPromise.isPending())) {
self.connectionPromise = new Promise(function(resolve, reject) {
self.socket.connect(self.port, self.ip, function() {
self.isConnected = true;
self.emit('connected');
resolve();
});
});
}
return self.connectionPromise;
}
function close () {
if (this.socket) {
this.socket.destroy();
}
}
// Can be called with and without timeout
function send( dataStr, timeout=0 ) {
this.socket.write(dataStr);
// Setting timeout to 0 disables the timeout
this.socket.setTimeout( timeout );
}
function isConnected () {
return this.isConnected;
}
MaxCubeLowLevel.prototype.connect = connect;
MaxCubeLowLevel.prototype.close = close;
MaxCubeLowLevel.prototype.send = send;
MaxCubeLowLevel.prototype.isConnected = isConnected;
module.exports = MaxCubeLowLevel;