-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxpl-currentCost.js
More file actions
144 lines (113 loc) · 4.29 KB
/
xpl-currentCost.js
File metadata and controls
144 lines (113 loc) · 4.29 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
const Xpl = require("xpl-api");
const commander = require('commander');
const SerialPort = require('serialport')
const os = require('os');
const CC128Serial = require('./lib/cc128-serial');
commander.version(require("./package.json").version);
commander.option("-s, --serialPort <path>", "Serial device path");
commander.option("-a, --deviceAliases <aliases>", "Devices aliases");
Xpl.fillCommander(commander);
commander.option("--heapDump", "Enable heap dump (require heapdump)");
commander.command('listSerialPort').description("List serial ports").action(
function() {
console.log("List serial ports:");
SerialPort.list(function(err, ports) {
if (err) {
console.log("List performs error : " + err);
process.exit(0);
return;
}
ports.forEach(function(port) {
console.log(" Port name='" + port.comName + "' pnpId='" +
port.pnpId + "' manufacturer='" + port.manufacturer + "'");
});
console.log("End of list");
});
});
commander
.command('start')
.description("Start processing CC128 datas")
.action(
function() {
console.log("Start");
if (!commander.serialPort) {
switch (os.platform()) {
case "win32":
commander.serialPort = "COM4";
break;
case "linux":
commander.serialPort = "/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0";
break;
}
console.log("Use default serial port : " + commander.serialPort);
}
commander.deviceAliases = Xpl
.loadDeviceAliases(commander.deviceAliases);
const port = new SerialPort(commander.serialPort, {
baudRate : 57600,
dataBits : 8,
stopBits : 1,
parity : 'none',
rtscts : false,
});
//const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
port.on("open", function(error) {
console.log('Parse opened');
try {
if (error) {
console.log("Can not open serial device '" +
commander.serialPort + "'", error);
process.exit(1);
return;
}
console.log("Serial device '" + commander.serialPort +
"' opened.");
if (!commander.xplSource) {
var hostName = os.hostname();
if (hostName.indexOf('.') > 0) {
hostName = hostName.substring(0, hostName.indexOf('.'));
}
commander.xplSource = "cc128." + hostName;
}
var xpl = new Xpl(commander);
xpl.on("error", function(error) {
console.log("XPL error", error);
});
xpl.bind(function(error) {
if (error) {
console.log("Can not open xpl bridge ", error);
process.exit(2);
return;
}
console.log("Xpl bind succeed ");
new CC128Serial(function(data, callback) {
// console.log("Write '" + data + "'");
port.write(data, callback);
}, function(body, callback) {
xpl.sendXplTrig(body, callback);
}, commander, function(error, cc128) {
if (error) {
console.log("Can not initialize CC128 engine ", error);
process.exit(3);
return;
}
port.on('data', function(data) {
// console.log('data received: ' + data+"'");
cc128.processSerialData(data);
});
port.on('close', function() {
console.log('close received: ' + data);
cc128.close();
xpl.close();
});
xpl.on("xpl:xpl-cmnd", function(message) {
cc128.processXplMessage(message);
});
});
});
} catch (x) {
console.log(x);
}
});
});
commander.parse(process.argv);