Commit 04b85c0934d829e52c499fbc6701138fdf0b0030
1 parent
c5ab35e0ac
Exists in
master
Register modem before init
Showing 1 changed file with 11 additions and 10 deletions Side-by-side Diff
lib/modem.js
... | ... | @@ -39,13 +39,7 @@ const modemInfo = { |
39 | 39 | |
40 | 40 | let lastTs = new Date(); |
41 | 41 | |
42 | -const port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => { | |
43 | - if (err) { | |
44 | - logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | |
45 | - process.exit(1); | |
46 | - } | |
47 | -}); | |
48 | - | |
42 | +let port; | |
49 | 43 | |
50 | 44 | const parserReadLine = new ParserReadline(); |
51 | 45 | |
... | ... | @@ -54,9 +48,6 @@ parserWaitForOK.on('data', () => { |
54 | 48 | mutex.releaseLockWaitForCommand(); |
55 | 49 | }); |
56 | 50 | |
57 | - | |
58 | -port.pipe(parserReadLine); | |
59 | - | |
60 | 51 | function writeToPort(data) { |
61 | 52 | return new Promise((resolve) => { |
62 | 53 | port.write(data, (err, bytesWritten) => { |
... | ... | @@ -319,6 +310,16 @@ async function sendSMS(destination, msg) { |
319 | 310 | } |
320 | 311 | |
321 | 312 | function init() { |
313 | + port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => { | |
314 | + if (err) { | |
315 | + logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | |
316 | + process.exit(1); | |
317 | + } | |
318 | + }); | |
319 | + port.pipe(parserReadLine); | |
320 | + | |
321 | + registerModem(modemInfo); | |
322 | + | |
322 | 323 | setInterval(() => { |
323 | 324 | if ((new Date() - lastTs) > MAX_LAST_DATA_AGE_MS) { |
324 | 325 | logger.warn(`No data for more than ${MAX_LAST_DATA_AGE_MS} ms. Modem might be unresponsive. Terminating modem ${config.modem.device}.`); |