-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteBox.js
95 lines (83 loc) · 2.45 KB
/
RemoteBox.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
const { SerialPort } = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline');
class RemoteBox {
constructor(port, baudRate) {
this.port = new SerialPort({ path: port, baudRate: baudRate });
this.parser = this.port.pipe(new ReadlineParser({ delimiter: '\r' }));
this.AntennaStatus = [];
this.port.on('error', (err) => {
console.log(err);
});
}
sendCommand(command) {
const self = this;
console.log(`Voy a enviar el comando: ${command}`)
return new Promise(function (resolve, reject) {
self._writeAndRead(`${command}\n`)
.then((response) => resolve(response.trim()))
.catch((error) => reject(error));
});
}
_writeAndRead(data) {
const self = this;
console.log("_write and read " + data);
return new Promise(function (resolve, reject) {
const dataListener = (responseData) => {
self.parser.removeListener('data', dataListener);
resolve(responseData);
};
self.parser.on('data', dataListener);
self.port.write(data, (err) => {
if (err) {
self.parser.removeListener('data', dataListener);
reject(err);
}
});
});
}
getInfo() {
//Todo: return this.sendCommand('O');
}
getAntennaStatus() {
const self = this;
return new Promise(function (resolve, reject) {
self.sendCommand('S')
.then((response) => {
console.log(response)
self.AntennaStatus = self._parseAntennaStatus(response);
resolve(self._parseAntennaStatus(response));
})
.catch((error) => {
console.error('Error in getAntennaStatus:', error);
reject(error);
});
});
}
setDebugMessages(enabled) {
//Todo
//const value = enabled ? 1 : 0;
//return this.sendCommand(`X${value}`);
}
getAntennaInfo() {
//Todo: return this.sendCommand('FI');
}
setRadioAntenna(trx, n, on) {
const value = on ? 1 : 0;
return this.sendCommand(`${trx}R${n}${value}`);
}
_parseAntennaStatus(response) {
console.log(`Response ${response}`);
try {
const matches = response.match(/lSW\d+:<([\d,]+)>/);
console.log(matches)
if (!matches) {
throw new Error('Invalid antenna status message');
}
const statuses = matches[1].split(',').map((s) => parseInt(s, 10));
return statuses;
} catch (error) {
throw new Error('Invalid antenna status message');
}
}
}
module.exports = RemoteBox;