-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocketDevices.js
178 lines (164 loc) · 5.39 KB
/
websocketDevices.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var exports = module.exports = {};
var atob = require('atob');
var btoa = require('btoa');
var when = require('when');
var debug = require('debug')('eve-connector:websocket');
_deviceSockets = [];
var checkDeviceType = function(device)
{
if (!device)
throw ('device is undefined')
if ( device.type != 'websocket' )
throw (['Device type should be "websocket"', device])
if ( ! device.params )
throw (['Device params is empty', device])
if ( !device.params.ip )
throw (['You must provide ip param for websocket devices', device.params]);
if ( !device.params.port )
throw (['You must provide port param for websocket devices', device.params]);
}
var listDevices = function() {
return [];
}
var getDeviceSocket = function(device) {
debug('getDeviceSocket');
return when.promise(function(resolve, reject){
var connected = _deviceSockets.find(function(d){
return (d.ip == device.params.ip && d.port == device.params.port);
});
if (connected !== undefined){
resolve(connected.socket);
}
else {
connectDevice(device).then(
function(socket) { resolve(socket) },
function(error) { reject(error) }
);
}
});
}
var connectDevice = function(device, options) {
debug('connectDevice()');
options = options || {};
return when.promise(function(resolve, reject){
var url = 'ws://' + device.params.ip + ':' + device.params.port;
var deviceSocket = require('socket.io-client')(url, options);
deviceSocket.on('connect', function(){
debug('deviceSocket connected');
_deviceSockets.push({ip: device.params.ip, port: device.params.port, socket: deviceSocket});
resolve(deviceSocket);
});
deviceSocket.on('disconnect', function(){
debug('deviceSocket disconnected');
});
deviceSocket.on('connect_error', function(){
reject(new Error('Could not connect to websocket device at ' + url));
});
deviceSocket.on('error', function(err){
reject(err);
});
});
}
var isDeviceAvailable = function(device)
{
debug('isDeviceAvailable()');
checkDeviceType(device);
return when.promise(function(resolve, reject){
checkDeviceType(device);
var url = 'ws://' + device.params.ip + ':' + device.params.port;
var deviceSocket = require('socket.io-client')(url, {reconnection: false});
deviceSocket.on('connect', function(){
debug('connect success');
deviceSocket.disconnect();
resolve({available: true, device: device});
});
deviceSocket.on('connect_error', function(){
resolve({available: false, device: device});
});
});
}
var areDevicesAvailable = function(type, devicesList)
{
debug('areDevicesAvailable()');
var available = { type: type, params: []};
var checks = [];
devicesList.forEach(function(d){
var device = {type: type, params:{ip: d.ip, port: d.port}};
var check = when.promise(function(resolve, reject){
isDeviceAvailable(device).then(
function(res){
if ( res.available )
available.params.push({ip: d.ip, port: d.port});
resolve(available);
},
function(err){
reject(err);
}
);
});
checks.push(check);
});
return when.all(checks);
}
var sendData = function(device, data)
{
return when.promise(function(resolve, reject){
checkDeviceType(device);
getDeviceSocket(device)
.then(function(socket){
var bin = atob(data.toString());
socket.emit('serial', bin);
resolve('sent');
})
.catch(function(error){ reject(error) })
;
});
}
var readData = function(device, length)
{
return when.promise(function(resolve, reject){
checkDeviceType(device);
var ip = device.params.ip;
var port = device.params.port;
reject('readData() is not implemented yet for websocket devices');
});
}
var startPoll = function(device, socket)
{
debug('Start polling websocket device...', device);
return when.promise(function(resolve, reject){
checkDeviceType(device);
getDeviceSocket(device)
.then(function(deviceSocket){
deviceSocket.on('serial', function(data){
debug('got poll data', data);
socket.emit('websocketPoll', btoa(data));
});
resolve(true);
})
.catch(function(error){ reject(error) });
});
}
var stopPoll = function(device) {
checkDeviceType(device);
var ip = device.params.ip;
var port = device.params.port;
throw new Error('stopPoll() is not implemented yet for websocket devices');
}
var test = function(device)
{
checkDeviceType(device);
debug('test starting...');
setTimeout(function () {
//throw(['this is the error']);
debug('test end');
}, 1000);
}
exports.isDeviceAvailable = isDeviceAvailable;
exports.areDevicesAvailable = areDevicesAvailable;
exports.listDevices = listDevices;
exports.sendData = sendData;
exports.startPoll = startPoll;
exports.stopPoll = stopPoll;
exports.readData = readData;
exports.test = test;