-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104.js
More file actions
275 lines (202 loc) · 6.25 KB
/
Copy path104.js
File metadata and controls
275 lines (202 loc) · 6.25 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var through = require('through');
var pserial = require('pinoccio-serial');
var outputParser = require('./lib/verbose-parser');
var json = require('./lib/json');
var version = require('./package.json').version;
module.exports = function(serialport,com,readycb){
var serial = pserial(serialport);
if(typeof options == 'function'){
readycb = options;
options = {};
}
if(typeof com == 'object'){
options = com;
com = options.com;
}
var out;
var closed;
var parser = outputParser();
parser.on('reboot',function(){
if(out.activated){
activateBridge(function(err){
if(err) console.log('failed to reactivate bridge');
})
}
})
parser.on('data',function(data){
//console.log('%%% PARSER DATA '+data);
// send out to event stream
handle(data);
// send to bridge.. the bridge can be moved out of this file.
});
serial.connect(com,handleConnection);
function handleConnection (err,scoutScript){
if(err) return out.emit('error');
scoutScript.on("error",function(err){
// something bad happened to serial.
console.log("something bad happened to the serial connection. =(");
console.log(err);
out.emit('error',err);
});
out.ready = true;
out.scoutScript = scoutScript;
activateBridge(function(err){
if(err) return out.emit('error',err);
out.activated = true;
out.emit('ready',scoutScript);
readycb(false,out);
readycb = noop;
});
// all serial output
scoutScript.on('log',function(data){
//console.log('sending log data to verbose parser!!'+data);
parser.write(data);
});
}
function activateBridge(cb){
out.bridgeCommand('hq.bridge();',function(err,data){
if(err) return out.emit('error',new Error('error starting bridge. '+err));
if(data && data.indexOf('unexpected number') > -1) {
return out.emit('error',new Error('scout requires the hq.bridge command please update firmware.'));
}
cb(err,data);
});
}
// from board
var handle = function(data){
var scout;
//{"type":"mesh","scoutid":2,"troopid":2,"routes":0,"channel":20,"rate":"250 kb/s","power":"3.5 dBm"}
if(out.mesh) {
scout = out.mesh.scoutid;
}
if(data && data.type == "token"){
out.token = data.token;
data['pinoccio-bridge'] = version+'';
out.sentToken = true;
} else if(out.token && !out.sentToken){
out.sentToken = true;
out.queue({type:"token",token:out.token,_v:version,bridge:version,scout:scout});
}
// add scout id and wrap with type report!
// make sure its a report!
if(data) {
if(data.type == 'reply') return out._handleReply(data);
out.queue(data);
}
}
out = through(function(data){
if(!data) return;
// command stream to board
if(data.type == 'command') {
if(!data.to || !data.command){
//return console.log('INVALID BRIDGE COMMAND!',data);
}
// support external data.timeout
var t = Date.now();
//console.log('sending command')
out.command(data.to,data.command,function(err,res){
//data.
data.type = "reply";
if(err) data.err = true;
data.reply = res != undefined?res:err+'';
data.from = data.to;
data.basetime = Date.now()-t;
data.end = true;
delete data.to;
// send replies back.
out.queue(data);
});
} else if(data.type == 'online') {
// noop.
} else {
//console.log('UNKNOWN bridge command!', data);
}
});
if(readycb) out.once('error',function(err){
readycb(err); readycb = noop;
})
out.ready = false;
var _id = 0;
var replyCbs = {};
out._handleReply = function(reply){
//console.log('_handleReply',reply);
if(!replyCbs[reply.id]) return;
var cb = replyCbs[reply.id];
if(!cb.reply) cb.reply = [];
cb.reply.push(reply.reply);
// im really done!
if(reply.err || reply.end){
reply.reply = cb.reply.join('');
} else {
return;
}
delete replyCbs[reply.id];
clearTimeout(cb.timer);
// TODO err,data from reply
cb(reply.err?reply.reply:false,reply.reply);
out.queue(reply);
}
// just expose command because you probably want to run commands from everywhere
out.command = function(scout,command,cb){
//if(out.mesh && out.mesh.scoutid == scout) {
//console.log('----------- short circuit bridge command!',scout,command);
//return out.bridgeCommand(command,cb);
//}
//console.log('send command!',scout,command);
var id = ++_id;
replyCbs[id] = cb;
var timeout = 10000;
cb.timer = setTimeout(function(){
var cb = replyCbs[id];
if(cb) {
delete replyCbs[id];
cb(false,{type:"reply",err:"base timeout in "+timeout+" ms",from:scout});
}
},10000);
command = {id:id,type:"command",to:scout,command:command};
command = "hq.bridge("+JSON.stringify(JSON.stringify(command)+"\n")+");";
//console.log('commanding> ',command);
out.bridgeCommand(command,function(err,data){
if(err) {
var cb = replyCbs[id];
if(!cb) return;
delete replyCbs[id];
cb(false,{type:"reply",id:id,err:err+'',from:scout})
}
});
}
out.bridgeCommand = function(command,cb){
//console.log('bridge command!',command);
command = command.trim();
var attempts = 0
, z = this
, run = function _scommand(){
z.scoutScript.command(command,function(err,data){
if(err) {
if(err.code == 'EWRITE' && ++attempts < 2) return _scommand();
}
var reply;
if(!err) {
var orig = data;
// remove command just in case it snuck in output.
if(data.indexOf(command) === 0) data = data.substr(data.indexOf(command));
var lines = data.split("\r\n");
reply = [];
for(var i =0;i<lines.length;++i){
if(lines[i].indexOf('[hq-bridge]') === 0) continue;
reply.push(lines[i]);
}
reply = reply.join("\r\n");
}
if(cb) cb(err,reply);
})
}
run();
}
out.close = function(fn){
closed = true;
this.scoutScript.close(fn);
}
return out;
}
function noop(){};