Commit 2db546a87026b1d0e04ec6edef14f3bec89d267e

Authored by Adhidarma Hadiwinoto
1 parent 0bdac2f9c6
Exists in master

Regex parser

Showing 4 changed files with 78 additions and 33 deletions Side-by-side Diff

1 1 'use strict';
2 2  
3 3 const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000;
4   -const DELIMITER_WAIT_FOR_OK = '\nOK\r\n';
  4 +// const DELIMITER_WAIT_FOR_OK = '\nOK\r\n';
  5 +
  6 +const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r\n/;
  7 +// const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r\n/;
5 8  
6 9 const moment = require('moment');
7 10 const SerialPort = require('serialport');
8 11 const ParserReadline = require('@serialport/parser-readline');
9   -const ParserDelimiter = require('@serialport/parser-delimiter');
  12 +// const ParserDelimiter = require('@serialport/parser-delimiter');
  13 +
  14 +const ParserRegex = require('@serialport/parser-regex');
10 15  
11 16 const config = require('komodo-sdk/config');
12 17 const logger = require('komodo-sdk/logger');
... ... @@ -37,7 +42,7 @@ const port = new SerialPort(config.modem.device, { baudRate: 115200 });
37 42  
38 43 const parserReadLine = new ParserReadline();
39 44  
40   -const parserWaitForOK = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  45 +const parserWaitForOK = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
41 46 parserWaitForOK.on('data', () => {
42 47 mutex.releaseLockWaitForCommand();
43 48 });
... ... @@ -55,6 +60,7 @@ function writeToPort(data) {
55 60 });
56 61 }
57 62  
  63 +// eslint-disable-next-line no-unused-vars
58 64 async function writeToPortAndWaitForOK(data) {
59 65 await mutex.setLockWaitForCommand();
60 66 const result = await writeToPort(data);
... ... @@ -66,16 +72,23 @@ async function writeToPortAndWaitForOK(data) {
66 72 }
67 73  
68 74 async function readSMS(slot) {
69   - const parserCMGR = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  75 + const parserCMGR = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
70 76 parserCMGR.on('data', (data) => {
71 77 if (data) {
72   - reportSender.incomingSMS(sms.extract(data.toString().trim()));
  78 + try {
  79 + reportSender.incomingSMS(sms.extract(data.toString().trim()));
  80 + } catch (e) {
  81 + logger.warn(`Exception on reporting new message. ${e.toString()}`, { smsObj: e.smsObj, dataFromModem: data });
  82 +
  83 + process.exit(0);
  84 + }
73 85 }
74 86 port.unpipe(parserCMGR);
75 87 mutex.releaseLockWaitForCommand();
76 88 });
77 89  
78   - const parserCMGD = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  90 + // const parserCMGD = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  91 + const parserCMGD = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
79 92 parserCMGD.on('data', () => {
80 93 port.unpipe(parserCMGD);
81 94 mutex.releaseLockWaitForCommand();
... ... @@ -139,24 +152,32 @@ parserReadLine.on('data', (data) => {
139 152 }
140 153 });
141 154  
142   -function simpleCommand(cmd, callback) {
143   - const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  155 +async function simpleSubCommand(cmd, callback) {
  156 + const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
144 157 parser.on('data', (data) => {
145 158 port.unpipe(parser);
146   - mutex.releaseLockWaitForCommand();
  159 + mutex.releaseLockWaitForSubCommand();
147 160  
148 161 if (data) {
149   - callback(null, data.toString().trim());
  162 + if (callback) callback(null, data.toString().trim());
150 163 }
151 164 });
152 165  
153   - port.pipe(parser);
154   - writeToPortAndWaitForOK(cmd);
  166 + return new Promise(async (resolve) => {
  167 + await mutex.setLockWaitForSubCommand();
  168 + port.pipe(parser);
  169 + writeToPort(cmd);
  170 +
  171 + await mutex.setLockWaitForSubCommand();
  172 + mutex.releaseLockWaitForSubCommand();
  173 +
  174 + resolve();
  175 + });
155 176 }
156 177  
157 178 function readManufacturer() {
158 179 return new Promise((resolve) => {
159   - simpleCommand('AT+CGMI\r', (err, result) => {
  180 + simpleSubCommand('AT+CGMI\r', (err, result) => {
160 181 modemInfo.manufacturer = result;
161 182 logger.info(`Manufacturer: ${result}`);
162 183 resolve(result);
... ... @@ -166,7 +187,7 @@ function readManufacturer() {
166 187  
167 188 function readModel() {
168 189 return new Promise((resolve) => {
169   - simpleCommand('AT+CGMM\r', (err, result) => {
  190 + simpleSubCommand('AT+CGMM\r', (err, result) => {
170 191 modemInfo.model = result;
171 192 logger.info(`Model: ${result}`);
172 193 resolve(result);
... ... @@ -176,7 +197,7 @@ function readModel() {
176 197  
177 198 function readIMEI() {
178 199 return new Promise((resolve) => {
179   - simpleCommand('AT+CGSN\r', (err, result) => {
  200 + simpleSubCommand('AT+CGSN\r', (err, result) => {
180 201 modemInfo.imei = result;
181 202 logger.info(`IMEI: ${result}`);
182 203 resolve(result);
... ... @@ -186,7 +207,7 @@ function readIMEI() {
186 207  
187 208 function readIMSI() {
188 209 return new Promise((resolve) => {
189   - simpleCommand('AT+CIMI\r', (err, result) => {
  210 + simpleSubCommand('AT+CIMI\r', (err, result) => {
190 211 modemInfo.imsi = result;
191 212 logger.info(`IMSI: ${result}`);
192 213  
... ... @@ -201,8 +222,24 @@ function readIMSI() {
201 222 });
202 223 }
203 224  
  225 +function readCOPS() {
  226 + return new Promise((resolve) => {
  227 + simpleSubCommand('AT+COPS?\r', (err, result) => {
  228 + resolve(result);
  229 + });
  230 + });
  231 +}
  232 +
  233 +function deleteInbox() {
  234 + return new Promise((resolve) => {
  235 + simpleSubCommand('AT+CMGD=0,4\r', (err, result) => {
  236 + resolve(result);
  237 + });
  238 + });
  239 +}
  240 +
204 241 async function querySignalStrength() {
205   - const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  242 + const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
206 243 parser.on('data', () => {
207 244 port.unpipe(parser);
208 245 mutex.releaseLockWaitForCommand();
... ... @@ -227,7 +264,7 @@ async function registerSignalStrengthBackgroundQuery() {
227 264 async function sendSMS(destination, msg) {
228 265 if (typeof destination !== 'string' || typeof msg !== 'string' || !destination.trim() || !msg.trim()) return;
229 266  
230   - const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  267 + const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
231 268 parser.on('data', () => {
232 269 port.unpipe(parser);
233 270 mutex.releaseLockWaitForSubCommand();
... ... @@ -263,19 +300,19 @@ async function sendSMS(destination, msg) {
263 300  
264 301 function init() {
265 302 port.on('open', async () => {
266   - // await mutex.setLockWaitForCommand();
267   - port.pipe(parserWaitForOK);
  303 + await mutex.setLockWaitForCommand();
268 304  
269 305 logger.info('Modem opened');
270   - await writeToPortAndWaitForOK('AT\r');
  306 + await writeToPort('\r');
  307 + await simpleSubCommand('AT\r');
271 308  
272 309 logger.info('Initializing modem to factory set');
273   - await writeToPortAndWaitForOK('AT&F\r');
  310 + await simpleSubCommand('AT&F\r');
274 311  
275 312 logger.info('Disabling echo');
276   - await writeToPortAndWaitForOK('ATE0\r');
  313 + await simpleSubCommand('ATE0\r');
277 314  
278   - await writeToPortAndWaitForOK('AT+COPS?\r');
  315 + await readCOPS();
279 316  
280 317 await readManufacturer();
281 318 await readModel();
... ... @@ -284,16 +321,13 @@ function init() {
284 321  
285 322 if (!config.disable_delete_inbox_on_startup) {
286 323 logger.info('Deleting existing messages');
287   - await writeToPortAndWaitForOK('AT+CMGD=0,4\r');
  324 + await deleteInbox();
288 325 }
289 326  
290   - port.unpipe(parserWaitForOK);
291   -
292   - await mutex.setLockWaitForCommand();
293 327 mutex.releaseLockWaitForCommand();
  328 + logger.verbose('Init completed');
294 329  
295 330 registerSignalStrengthBackgroundQuery();
296   - logger.verbose('Init completed');
297 331 });
298 332 }
299 333  
... ... @@ -53,15 +53,13 @@ function setLockWaitForSubCommand() {
53 53 }
54 54  
55 55 function releaseLockWaitForSubCommand() {
56   - mutexSubCommand.unlock();
  56 + // mutexSubCommand.unlock();
57 57  
58   - /*
59 58 try {
60 59 mutexSubCommand.unlock();
61 60 } catch (e) {
62 61 //
63 62 }
64   - */
65 63 }
66 64  
67 65 exports.setLockWaitForOK = setLockWaitForOK;
lib/report-sender.js
... ... @@ -6,7 +6,19 @@ const config = require('komodo-sdk/config');
6 6 const logger = require('komodo-sdk/logger');
7 7  
8 8 function incomingSMS(message) {
9   - if (!config || !config.report_url || !config.report_url.incoming_sms) return;
  9 + if (!message || !config || !config.report_url || !config.report_url.incoming_sms) return;
  10 +
  11 + if (!message.metadata) {
  12 + const e = new Error('Missing metadata on incoming sms');
  13 + e.smsObj = message;
  14 + throw e;
  15 + }
  16 +
  17 + if (!message.message) {
  18 + const e = new Error('Missing message on incoming sms');
  19 + e.smsObj = message;
  20 + throw e;
  21 + }
10 22  
11 23 const requestOptions = {
12 24 url: config.report_url.incoming_sms,
... ... @@ -28,6 +28,7 @@
28 28 "dependencies": {
29 29 "@serialport/parser-delimiter": "^2.0.2",
30 30 "@serialport/parser-readline": "^2.0.2",
  31 + "@serialport/parser-regex": "^2.0.2",
31 32 "express": "^4.17.1",
32 33 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
33 34 "locks": "^0.2.2",