forked from Tesla-UFMG/Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial-handler.js
More file actions
329 lines (268 loc) · 11.6 KB
/
Copy pathserial-handler.js
File metadata and controls
329 lines (268 loc) · 11.6 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const SerialPort = require('serialport');
const fs = require('fs');
const constants = require('./components/constants.js');
const Delimiter = require('@serialport/parser-delimiter');
const ApiModeParser = require('./api-mode-parser.js');
const frameHandler = require('./components/frame-handler.js')
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
class SerialHandler {
constructor(objectCreatorInstance) {
this.objectCreatorInstance = objectCreatorInstance;
this.ttl_port = false;
this.idsGone = [];
this.lastDatalogDate = false;
this.currentDatalogPath = false;
this.parseData = this.parseData.bind(this);
this.requestRSSI = this.requestRSSI.bind(this);
this.rssiRequestMessage = this.rssiRequestMessage.bind(this);
this.payloadMessage = this.payloadMessage.bind(this);
this.allowDatalog = true;
this.shouldCreateNewDatalogFile = true;
this.receivedRSSI = undefined;
this.lastSentRSSI = undefined;
}
async openPort() {
// this.rssiRequestMessage();
// this.payloadMessage();
console.log("Esperando conexão da porta...");
let found = false;
let targetPort = constants.port.portName;
let ignorePorts = [];
while (!found) {
try {
ignorePorts = fs.readFileSync('./'+constants.port.ignorePortsFile, 'utf8').split('\n');
} catch (e) {
console.log('Erro lendo arquivo '+constants.port.ignorePortsFile+': '+e);
}
await sleep(1000);
const ports = await SerialPort.list();
if (ports.length == 0)
continue;
for(let i = 0; i<ports.length; i++) {
if (typeof targetPort !== 'undefined' && targetPort !== ""){
if (ports[i].comName !== targetPort)
continue;
}
if (ignorePorts.includes(ports[i].comName))
continue;
console.log("Tentando porta: "+ports[i].comName);
this.ttl_port = new SerialPort(ports[i].comName, { baudRate: 115200, autoOpen: false })
let opened = false;
let error;
//abre e fecha a porta para garantir que ela é capaz de ser aberta
this.ttl_port.open(async err => {
error = err;
if (!err) {
let closed = false;
this.ttl_port.close(() => closed = true);
while(!closed)
await sleep(5);
}
opened = true;
});
//await new Promise(resolve => )
while(!opened)
await sleep(10);
//se tiver erro, fecha a porta e a seta como nulo
if (error) {
console.log("erro ao abrir a porta "+this.ttl_port.path+": "+error);
this.ttl_port = false
} else {
found = true;
break;
}
}
}
}
async initSerialPort() {
await this.openPort();
this.ttl_port.open(err => {});
const _this = this;
this.ttl_port.on('open', function () {
console.log('Porta '+_this.ttl_port.path+' aberta');
_this.objectCreatorInstance.setPortConnected(true);
//so ativa a rotina de medir a intensidade de sinal se estiver rodando em API mode
if (constants.port.operationType == constants.port.OperationsType.API_BYTES
|| constants.port.operationType == constants.port.OperationsType.API_STRING)
_this.rssiInterval = setTimeout(_this.requestRSSI, 500);
_this.ttl_port.on("close", () => {
console.log("port closed");
_this.objectCreatorInstance.setPortConnected(false);
_this.ttl_port = false;
clearInterval(_this.rssiInterval);
parser = false;
_this.initSerialPort();
})
})
let parser;
switch (constants.port.operationType) {
case constants.port.OperationsType.API_BYTES:
case constants.port.OperationsType.API_STRING:
parser = new ApiModeParser();
break;
default:
parser = new Delimiter({delimiter: '\n'});
break;
}
this.ttl_port.pipe(parser);
parser.on('data', this.parseData)
}
parseData(data) {
//console.log('new data');
//console.log(newData);
let newData = [];
let id;
let datalogSafe = true;
switch (constants.port.operationType) {
case constants.port.OperationsType.BYTES:
id = (data[1] << 8) + data[0];
for (let i = 2; i < data.length; i+=2)
newData.push((data[i+1] << 8) + data[i]);
break;
case constants.port.OperationsType.STRING:
const d = data.toString().split('\t');
id = d[0];
newData = d.slice(1);
break;
case constants.port.OperationsType.API_BYTES:
case constants.port.OperationsType.API_STRING:
const frame = frameHandler.parseFrame(data);
switch (frame.frame.frameType) {
case 0x90:
const rfData = frame.frame.rfData;
if (constants.port.operationType === constants.port.OperationsType.API_BYTES) {
id = (rfData[1] << 8) + rfData[0];
for (let i = 2; i < rfData.length; i+=2)
newData.push((rfData[i+1] << 8) + rfData[i]);
} else {
const dat = rfData.toString().replace('\n','').split('\t');
id = dat[0];
newData = dat.slice(1);
}
if (id == 10) {//COLOCAR ID DA MENSAGEM DE GUILHERME
console.log("\nTime request received!")
if(newData[0] == 1) {
//MANDAR MENSAGEM DE HORA E MINUTO
let now = new Date()
const data = [0, 0, 0, 1, 0, now.getHours(), 0, now.getMinutes()]
console.log("Time request: "+now.getHours()+":"+now.getMinutes());
const message = frameHandler.generateFrame(0x10, 0x01, 0, 0, data)
this.ttl_port.write(message);
}
}
break;
case 0x97:
datalogSafe = false;
id = 901;
this.receivedRSSI = true;
//0x04: TX Failure, xbee não está conectado
if (frame.frame.commandStatus == 0x04) {
newData.push(-1)
} else {
newData = frame.frame.commandData;
}
break;
}
break;
}
if (datalogSafe) {
if (this.idsGone.includes(id)) {
this.writeDatalog()
this.idsGone = [];
} else {
this.idsGone.push(id);
}
}
this.objectCreatorInstance.treatInfo(id, newData).then( () => {
// var now = new Date().getMilliseconds();
// console.log(now - lastEscrito);
// lastEscrito = now;
});
// var now = new Date().getMilliseconds();
// console.log(now - lastEscrito);
// lastEscrito = now;
// console.log(recentInformation.getInfo());
}
rssiRequestMessage() {
const frameID = 0x01;
const commOpt = 0x02;
const atCommand = Buffer.from("DB", "ascii");
let frame = frameHandler.generateFrame(frameHandler.frameTypes.remoteAtCommand, frameID, commOpt, atCommand);
// 7E 00 0F 17 01 00 13 A2 00 41 93 2D C6 FF FE 02 44 42 E6
this.ttl_port.write(frame);
}
payloadMessage() {
const frameID = 0x23;
const broadcastRadius = 0x00;
const opt = 0x00;
const payload = Buffer.from("ping", "ascii");
let frame = frameHandler.generateFrame(frameHandler.frameTypes.requestTxMessage, frameID, broadcastRadius, opt, payload);
// 7E 00 0F 17 01 00 13 A2 00 41 93 2D C6 FF FE 02 44 42 E6
this.ttl_port.write(frame);
}
delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
async requestRSSI() {
if (!(this.receivedRSSI || typeof (this.receivedRSSI) === 'undefined'
|| new Date().getTime() - this.lastSentRSSI > 10000))
return;
this.receivedRSSI = false;
this.payloadMessage();
this.delay(100);
this.rssiRequestMessage();
this.delay(200);
this.payloadMessage();
this.delay(100);
this.rssiRequestMessage();
this.lastSentRSSI = new Date().getTime();
// this.rssiRequestMessage();
this.rssiInterval = setTimeout(this.requestRSSI, 500);
}
createNewDatalogFile() {
this.shouldCreateNewDatalogFile = true;
}
startDatalog() {
this.allowDatalog = true;
}
stopDatalog() {
this.allowDatalog = false;
}
writeDatalog() {
if (constants.datalog.shouldWrite && this.allowDatalog) {
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'};
const now = new Date();
let fileDestination;
if (!this.lastDatalogDate || now.getTime()-this.lastDatalogDate.getTime() > constants.datalog.timeout) {
const localeStr = now.toLocaleDateString('pt-BR', options);
const replaced = localeStr.replace(new RegExp("/", "g"),'-').replace(new RegExp(':', 'g'), '.');//꞉
fileDestination = constants.datalog.filepath + replaced + '.txt';
this.currentDatalogPath = fileDestination;
}
else
fileDestination = this.currentDatalogPath;
this.lastDatalogDate = now;
let datalogData = this.objectCreatorInstance.buildDatalog();
this.invokeWritting(fileDestination, datalogData);
}
}
async invokeWritting(fileDestination, datalogData) {
fs.appendFile(fileDestination, datalogData, err => {
if(err)
console.error('Erro ao fazer datalog: '+err);
});
}
listSerialPorts() {
SerialPort.list(function (err, ports) {
ports.forEach(function(port) {
console.log(port.comName);
console.log(' - pnpId: ' + port.pnpId);
console.log(' - manufacturer: ' + port.manufacturer);
console.log(' - serialNumber: ' + port.serialNumber);
console.log(' - vendorId: ' + port.vendorId);
console.log(' - productId: ' + port.productId);
});
});
}
}
module.exports = SerialHandler;