-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
167 lines (124 loc) · 3.39 KB
/
index.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
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
/* eslint no-console : 0 */
/* eslint no-global-assign : 0 */
app_path = __dirname;
app_name = 'bmwi';
app_intf = process.argv[2] || process.env.BMWI_INTERFACE || 'ibus';
process.title = app_name + '@' + app_intf;
terminating = false;
// node-bmw libraries
bitmask = require('bitmask');
bus = require('bus');
hex = require('hex');
json = require('json');
log = require('log-output');
num = require('num');
socket = require('socket');
// Class/event-based modules
update = new (require('update'))();
// Render serial port options object
function serialOpts(baudRate, parity) {
// DBUS+IBUS+KBUS : 9600 8e1
return {
autoOpen : false,
rtscts : true,
// rtscts : config.options.ctsrts_retry[app_intf],
baudRate,
parity,
};
} // serialOpts(baudRate, parity)
// Function to load modules that require data from config object,
// AFTER the config object is loaded
function loadModules() {
// Vehicle data bus interface libraries
intf = {
path : config.intf[app_intf],
};
// Vehicle data bus protocol config
proto = {
config : {
msgLengthMin : 5,
msgLengthMax : config.options.msgLengthMax[app_intf],
queueLengthMax : 1000,
errorMax : 25,
},
proto : null,
};
let intfBaudRate;
let intfParity;
let intfType;
// Load vehicle interface and protocol libs
switch (app_intf) {
case 'can0' :
case 'can1' : {
intfType = 'can';
break;
}
case 'dbus' : {
intfType = 'bmw';
intfBaudRate = 9600;
intfParity = 'even';
break;
}
case 'ibus' :
case 'kbus' : {
intfType = 'bmw';
intfBaudRate = 9600;
intfParity = 'even';
break;
}
case 'isp2' : {
intfType = 'isp2';
intfBaudRate = 19200;
intfParity = 'none';
}
}
// Populate interface, options, and protocol using above rendered variables
intf.intf = require(`intf-${intfType}`);
intf.opts = serialOpts(intfBaudRate, intfParity);
if (intfType === 'bmw') {
proto.proto = require(`proto-${intfType}`);
}
log.msg('Loaded modules');
} // loadModules()
async function signalTerm(signal = 'unknown') {
if (terminating === true) return;
console.log('');
config.console.output = true;
log.msg(`Caught signal: '${signal}'`);
await term();
} // async signalTerm(signal)
// Global term
async function term() {
if (terminating === true) return;
terminating = true;
log.msg('Terminating');
// Close defined interface
try {
await intf.intf.term();
}
catch (intfTermError) {
log.error(intfTermError);
}
await socket.term(); // Close socket server
await json.write(); // Write JSON config and status files
log.msg('Terminated');
process.exit();
} // async term()
// Global init
async function init() {
// Enable console output
config = { console : { output : true } };
log.msg(`Initializing network interface: '${app_intf}'`);
// Configure term event listeners
process.on('exit', async () => signalTerm('exit'));
process.on('SIGINT', async () => signalTerm('SIGINT'));
process.on('SIGTERM', async () => signalTerm('SIGTERM'));
await json.read(); // Read JSON config and status files
await json.reset(); // Reset status vars pertinent to launching app
loadModules(); // Configure interface and protocol
await intf.intf.init(); // Open defined interface
await socket.init(); // Open socket server
log.msg(`Initialized network interface '${app_intf}'`);
} // async init()
// FASTEN SEATBELTS
(async () => { await init(); })();