forked from Nodejs-XPL/xpl-cc128
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpl-currentCost.js
138 lines (110 loc) · 4.02 KB
/
xpl-currentCost.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
var Xpl = require("xpl-api");
var commander = require('commander');
var serialport = require("serialport");
var CC128Serial = require('./lib/cc128-serial');
var os = require('os');
commander.version(require("./package.json").version).option(
"-s, --serialPort <path>", "Serial device path");
Xpl.fillCommander(commander);
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);
}
var sp = new serialport.SerialPort(commander.serialPort, {
baudrate : 57600,
databits : 8,
stopbits : 1,
parity : 'none',
rtscts : false,
parser : serialport.parsers.readline("\n")
});
sp.on("open", function(error) {
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 + "'");
sp.write(data, callback);
}, function(body, callback) {
xpl.sendXplTrig(body, callback);
}, {
// Configuration
}, function(error, cc128) {
if (error) {
console.log("Can not initialize CC128 engine ", error);
process.exit(3);
return;
}
sp.on('data', function(data) {
// console.log('data received: ' + data+"'");
cc128.processSerialData(data);
});
sp.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);