Commit 35ba3f5746483b0daae0099e37cee4e17f216fc6

Authored by Adhidarma Hadiwinoto
1 parent 71522f4bf8
Exists in master

Proteksi kalo modem gak responsive atau error

Showing 1 changed file with 17 additions and 1 deletions Side-by-side Diff

... ... @@ -6,6 +6,8 @@ const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000;
6 6 const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r\n/;
7 7 // const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r\n/;
8 8  
  9 +const MAX_LAST_DATA_AGE_MS = 3 * 60 * 100;
  10 +
9 11 const moment = require('moment');
10 12 const SerialPort = require('serialport');
11 13 const ParserReadline = require('@serialport/parser-readline');
... ... @@ -38,7 +40,13 @@ const modemInfo = {
38 40 config: config.modem,
39 41 };
40 42  
41   -const port = new SerialPort(config.modem.device, { baudRate: 115200 });
  43 +let lastTs = new Date();
  44 +
  45 +const port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => {
  46 + logger.warn(`${err.toString()}. Terminating.`);
  47 + process.exit(1);
  48 +});
  49 +
42 50  
43 51 const parserReadLine = new ParserReadline();
44 52  
... ... @@ -136,6 +144,7 @@ function onCOPS(data) {
136 144 parserReadLine.on('data', (data) => {
137 145 logger.verbose(`* IN: ${data}`);
138 146 if (data) {
  147 + lastTs = new Date();
139 148 if (data.indexOf('+CSQ: ') === 0) {
140 149 const signalStrength = common.extractValueFromReadLineData(data).trim();
141 150 if (signalStrength) {
... ... @@ -299,6 +308,13 @@ async function sendSMS(destination, msg) {
299 308 }
300 309  
301 310 function init() {
  311 + setTimeout(() => {
  312 + if ((new Date() - lastTs) > MAX_LAST_DATA_AGE_MS) {
  313 + logger.warn(`No data for more than ${MAX_LAST_DATA_AGE_MS} ms. Modem might be unresponsive. Terminating.`);
  314 + process.exit(0);
  315 + }
  316 + }, 30 * 1000);
  317 +
302 318 port.on('open', async () => {
303 319 await mutex.setLockWaitForCommand();
304 320