Commit 9fd37cd21f35c70f90478af6cd64985389ddcb1d

Authored by Adhidarma Hadiwinoto
1 parent 3728e3517d
Exists in master

MODEM-TESTER: setinterval csq

Showing 2 changed files with 5 additions and 0 deletions Inline Diff

1 const REGEX_WAIT_FOR_OK_OR_ERROR = /\r\n(?:OK|ERROR)\r/; 1 const REGEX_WAIT_FOR_OK_OR_ERROR = /\r\n(?:OK|ERROR)\r/;
2 // const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r/; 2 // const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r/;
3 3
4 const SerialPort = require('serialport'); 4 const SerialPort = require('serialport');
5 5
6 const config = require('komodo-sdk/config'); 6 const config = require('komodo-sdk/config');
7 const logger = require('komodo-sdk/logger'); 7 const logger = require('komodo-sdk/logger');
8 8
9 const ParserReadline = require('@serialport/parser-readline'); 9 const ParserReadline = require('@serialport/parser-readline');
10 10
11 const parserReadline = new ParserReadline({ delimiter: '\r\n' }); 11 const parserReadline = new ParserReadline({ delimiter: '\r\n' });
12 parserReadline.on('data', (data) => { 12 parserReadline.on('data', (data) => {
13 logger.verbose('INCOMING', { parser: 'parserReadLine', data: data.toString() }); 13 logger.verbose('INCOMING', { parser: 'parserReadLine', data: data.toString() });
14 }); 14 });
15 15
16 const ParserRegex = require('@serialport/parser-regex'); 16 const ParserRegex = require('@serialport/parser-regex');
17 17
18 const parserWaitForOkOrError = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); 18 const parserWaitForOkOrError = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
19 parserWaitForOkOrError.on('data', (data) => { 19 parserWaitForOkOrError.on('data', (data) => {
20 logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); 20 logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
21 }); 21 });
22 22
23 23
24 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); 24 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout');
25 25
26 const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); 26 const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 });
27 parserInterByteTimeout.on('data', (data) => { 27 parserInterByteTimeout.on('data', (data) => {
28 logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); 28 logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() });
29 }); 29 });
30 30
31 let port; 31 let port;
32 32
33 function sleep(ms) { 33 function sleep(ms) {
34 return new Promise((resolve) => { 34 return new Promise((resolve) => {
35 setTimeout(() => { 35 setTimeout(() => {
36 resolve(); 36 resolve();
37 }, ms || 0); 37 }, ms || 0);
38 }); 38 });
39 } 39 }
40 40
41 function writeToPort(data) { 41 function writeToPort(data) {
42 return new Promise((resolve) => { 42 return new Promise((resolve) => {
43 port.write(data, (err, bytesWritten) => { 43 port.write(data, (err, bytesWritten) => {
44 if (err) logger.warn(`ERROR: ${err.toString()}`); 44 if (err) logger.warn(`ERROR: ${err.toString()}`);
45 45
46 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); 46 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() });
47 resolve(bytesWritten); 47 resolve(bytesWritten);
48 }); 48 });
49 }); 49 });
50 } 50 }
51 51
52 async function writeToPortDelayed(data, ms) { 52 async function writeToPortDelayed(data, ms) {
53 await sleep(ms || 500); 53 await sleep(ms || 500);
54 const result = writeToPort(data); 54 const result = writeToPort(data);
55 return result; 55 return result;
56 } 56 }
57 57
58 /* 58 /*
59 function isNotBlacklistedCommand(command) { 59 function isNotBlacklistedCommand(command) {
60 let [, cmd] = (command || '').trim().split('+'); 60 let [, cmd] = (command || '').trim().split('+');
61 cmd = (cmd || '').replace(/=.*$/, ''); 61 cmd = (cmd || '').replace(/=.*$/, '');
62 return !config 62 return !config
63 || !config.modem_tester 63 || !config.modem_tester
64 || !config.modem_tester.skip_commands 64 || !config.modem_tester.skip_commands
65 || (config.modem_tester.skip_commands.indexOf(cmd) < 0); 65 || (config.modem_tester.skip_commands.indexOf(cmd) < 0);
66 } 66 }
67 */ 67 */
68 68
69 port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { 69 port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => {
70 if (err) { 70 if (err) {
71 logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); 71 logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`);
72 process.exit(1); 72 process.exit(1);
73 } 73 }
74 74
75 await writeToPortDelayed('AT\r'); 75 await writeToPortDelayed('AT\r');
76 76
77 /* 77 /*
78 if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT&F\r', 2000); 78 if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT&F\r', 2000);
79 if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT+CGSN\r', 2000); 79 if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT+CGSN\r', 2000);
80 if (isNotBlacklistedCommand('CIMI')) await writeToPortDelayed('AT+CIMI\r', 2000); 80 if (isNotBlacklistedCommand('CIMI')) await writeToPortDelayed('AT+CIMI\r', 2000);
81 if (isNotBlacklistedCommand('CSQ')) await writeToPortDelayed('AT+CSQ\r', 2000); 81 if (isNotBlacklistedCommand('CSQ')) await writeToPortDelayed('AT+CSQ\r', 2000);
82 if (isNotBlacklistedCommand('COPS?')) await writeToPortDelayed('AT+COPS?\r', 2000); 82 if (isNotBlacklistedCommand('COPS?')) await writeToPortDelayed('AT+COPS?\r', 2000);
83 // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000); 83 // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000);
84 */ 84 */
85 85
86 let commands; 86 let commands;
87 if (config && config.modem_tester && config.modem_tester.commands) { 87 if (config && config.modem_tester && config.modem_tester.commands) {
88 ({ commands } = config.modem_tester); 88 ({ commands } = config.modem_tester);
89 } else { 89 } else {
90 commands = [ 90 commands = [
91 'AT&F\r', 91 'AT&F\r',
92 'ATE0\r', 92 'ATE0\r',
93 'AT+CGSN\r', 93 'AT+CGSN\r',
94 'AT+CIMI\r', 94 'AT+CIMI\r',
95 'AT+COPS?\r', 95 'AT+COPS?\r',
96 'AT+CNMI=1,1,2,1,1\r', 96 'AT+CNMI=1,1,2,1,1\r',
97 'AT+CSQ\r', 97 'AT+CSQ\r',
98 ]; 98 ];
99 } 99 }
100 100
101 const commandsCount = commands.length; 101 const commandsCount = commands.length;
102 // eslint-disable-next-line no-plusplus 102 // eslint-disable-next-line no-plusplus
103 for (let i = 0; i < commandsCount; i++) { 103 for (let i = 0; i < commandsCount; i++) {
104 // eslint-disable-next-line no-await-in-loop 104 // eslint-disable-next-line no-await-in-loop
105 await writeToPortDelayed(commands[i], 2000); 105 await writeToPortDelayed(commands[i], 2000);
106 } 106 }
107
108 setInterval(() => {
109 writeToPort('AT+CSQ\r', 2000);
110 }, 30000);
107 }); 111 });
108 112
109 if (config && config.modem_tester && config.modem_tester.parser === 'regex') { 113 if (config && config.modem_tester && config.modem_tester.parser === 'regex') {
110 logger.info('Using parserWaitForOkOrError'); 114 logger.info('Using parserWaitForOkOrError');
111 port.pipe(parserWaitForOkOrError); 115 port.pipe(parserWaitForOkOrError);
112 } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { 116 } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') {
113 logger.info('Using parserInterByteTimeout'); 117 logger.info('Using parserInterByteTimeout');
114 port.pipe(parserInterByteTimeout); 118 port.pipe(parserInterByteTimeout);
115 } else { 119 } else {
116 logger.info('Using parserReadline'); 120 logger.info('Using parserReadline');
117 port.pipe(parserReadline); 121 port.pipe(parserReadline);
118 } 122 }
119 123
1 { 1 {
2 "name": "komodo-modem-sms", 2 "name": "komodo-modem-sms",
3 "version": "0.10.52", 3 "version": "0.10.52",
4 "description": "Generic SMS modem driver", 4 "description": "Generic SMS modem driver",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha", 7 "test": "mocha",
8 "postversion": "git push && git push --tags" 8 "postversion": "git push && git push --tags"
9 }, 9 },
10 "repository": { 10 "repository": {
11 "type": "git", 11 "type": "git",
12 "url": "http://gitlab.kodesumber.com/komodo/komodo-modem-sms.git" 12 "url": "http://gitlab.kodesumber.com/komodo/komodo-modem-sms.git"
13 }, 13 },
14 "keywords": [ 14 "keywords": [
15 "komodo", 15 "komodo",
16 "sms", 16 "sms",
17 "tektrans", 17 "tektrans",
18 "ppob" 18 "ppob"
19 ], 19 ],
20 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", 20 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>",
21 "license": "ISC", 21 "license": "ISC",
22 "devDependencies": { 22 "devDependencies": {
23 "eslint": "^5.16.0", 23 "eslint": "^5.16.0",
24 "eslint-config-airbnb-base": "^13.2.0", 24 "eslint-config-airbnb-base": "^13.2.0",
25 "eslint-plugin-import": "^2.18.2", 25 "eslint-plugin-import": "^2.18.2",
26 "should": "^13.2.3" 26 "should": "^13.2.3"
27 }, 27 },
28 "dependencies": { 28 "dependencies": {
29 "@serialport/parser-delimiter": "^2.0.2", 29 "@serialport/parser-delimiter": "^2.0.2",
30 "@serialport/parser-inter-byte-timeout": "^1.1.0", 30 "@serialport/parser-inter-byte-timeout": "^1.1.0",
31 "@serialport/parser-readline": "^2.0.2", 31 "@serialport/parser-readline": "^2.0.2",
32 "@serialport/parser-regex": "^2.0.2", 32 "@serialport/parser-regex": "^2.0.2",
33 "express": "^4.17.1", 33 "express": "^4.17.1",
34 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", 34 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
35 "locks": "^0.2.2", 35 "locks": "^0.2.2",
36 "moment": "^2.24.0", 36 "moment": "^2.24.0",
37 "node-pdu": "^1.0.15",
37 "request": "^2.88.0", 38 "request": "^2.88.0",
38 "serialport": "^7.1.5", 39 "serialport": "^7.1.5",
39 "serialport-gsm": "^3.2.0" 40 "serialport-gsm": "^3.2.0"
40 } 41 }
41 } 42 }
42 43