Commit 7f00b5aeb72447c89115c5ad555d7cd4363d2260

Authored by Adhidarma Hadiwinoto
1 parent 2e577f57b0
Exists in master

modemInfo.imei

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

lib/modem-commands.js
1 const MUTEX_COMMAND = 'COMMAND'; 1 const MUTEX_COMMAND = 'COMMAND';
2 // const MUTEX_SUBCOMMAND = 'SUBCOMMAND'; 2 // const MUTEX_SUBCOMMAND = 'SUBCOMMAND';
3 3
4 const ParserRegex = require('@serialport/parser-regex'); 4 const ParserRegex = require('@serialport/parser-regex');
5 5
6 const logger = require('komodo-sdk/logger'); 6 const logger = require('komodo-sdk/logger');
7 const mutex = require('./mutex-common'); 7 const mutex = require('./mutex-common');
8 const parsers = require('./serialport-parsers'); 8 const parsers = require('./serialport-parsers');
9 const modemInfo = require('./modem-info');
9 10
10 let port; 11 let port;
11 12
12 function writeToPort(data) { 13 function writeToPort(data) {
13 return new Promise((resolve) => { 14 return new Promise((resolve) => {
14 port.write(data, (err, bytesWritten) => { 15 port.write(data, (err, bytesWritten) => {
15 if (err) logger.warn(`ERROR: ${err.toString()}`); 16 if (err) logger.warn(`ERROR: ${err.toString()}`);
16 17
17 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); 18 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() });
18 resolve(bytesWritten); 19 resolve(bytesWritten);
19 }); 20 });
20 }); 21 });
21 } 22 }
22 23
23 exports.sleep = function sleep(ms) { 24 exports.sleep = function sleep(ms) {
24 return new Promise((resolve) => { 25 return new Promise((resolve) => {
25 setTimeout(() => { 26 setTimeout(() => {
26 resolve(); 27 resolve();
27 }, ms || 0); 28 }, ms || 0);
28 }); 29 });
29 }; 30 };
30 31
31 32
32 exports.setPort = function setPort(val) { 33 exports.setPort = function setPort(val) {
33 port = val; 34 port = val;
34 }; 35 };
35 36
36 exports.querySignalQuality = function querySignalQuality() { 37 exports.querySignalQuality = function querySignalQuality() {
37 return new Promise(async (resolve) => { 38 return new Promise(async (resolve) => {
38 if (!mutex.tryLock(MUTEX_COMMAND, 'querySignalQuality')) { 39 if (!mutex.tryLock(MUTEX_COMMAND, 'querySignalQuality')) {
39 resolve(false); 40 resolve(false);
40 return; 41 return;
41 } 42 }
42 43
43 await writeToPort('AT+CSQ\r'); 44 await writeToPort('AT+CSQ\r');
44 mutex.unlock(MUTEX_COMMAND, 'querySignalQuality'); 45 mutex.unlock(MUTEX_COMMAND, 'querySignalQuality');
45 resolve(true); 46 resolve(true);
46 }); 47 });
47 }; 48 };
48 49
49 exports.queryCOPS = function querySignalQuality() { 50 exports.queryCOPS = function querySignalQuality() {
50 return new Promise(async (resolve) => { 51 return new Promise(async (resolve) => {
51 await mutex.lock(MUTEX_COMMAND, 'queryCOPS'); 52 await mutex.lock(MUTEX_COMMAND, 'queryCOPS');
52 await writeToPort('AT+COPS?\r'); 53 await writeToPort('AT+COPS?\r');
53 mutex.unlock(MUTEX_COMMAND, 'queryCOPS'); 54 mutex.unlock(MUTEX_COMMAND, 'queryCOPS');
54 resolve(true); 55 resolve(true);
55 }); 56 });
56 }; 57 };
57 58
58 exports.queryIMEI = function queryIMEI() { 59 exports.queryIMEI = function queryIMEI() {
59 return new Promise(async (resolve) => { 60 return new Promise(async (resolve) => {
60 const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); 61 const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX });
61 parser.on('data', (data) => { 62 parser.on('data', (data) => {
62 logger.verbose('INCOMING', { parser: 'parserIMEI', data: data.toString() }); 63 logger.verbose('INCOMING', { parser: 'parserIMEI', data: data.toString() });
63 port.unpipe(parser); 64 port.unpipe(parser);
64 mutex.unlock(MUTEX_COMMAND, 'queryIMEI'); 65 mutex.unlock(MUTEX_COMMAND, 'queryIMEI');
65 resolve(); 66 modemInfo.imei = data.toString().trim();
67 resolve(modemInfo.imei);
66 }); 68 });
67 69
68 await mutex.lock(MUTEX_COMMAND, 'queryIMEI'); 70 await mutex.lock(MUTEX_COMMAND, 'queryIMEI');
69 71
70 port.pipe(parser); 72 port.pipe(parser);
71 await writeToPort('AT+CGSN\r'); 73 await writeToPort('AT+CGSN\r');
72 }); 74 });
73 }; 75 };
74 76
1 const SerialPort = require('serialport'); 1 const SerialPort = require('serialport');
2 2
3 const config = require('komodo-sdk/config'); 3 const config = require('komodo-sdk/config');
4 const logger = require('komodo-sdk/logger'); 4 const logger = require('komodo-sdk/logger');
5 5
6 6
7 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); 7 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout');
8 8
9 const parsers = require('./lib/serialport-parsers'); 9 const parsers = require('./lib/serialport-parsers');
10 const modemCommands = require('./lib/modem-commands'); 10 const modemCommands = require('./lib/modem-commands');
11 11
12 const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); 12 const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 });
13 parserInterByteTimeout.on('data', (data) => { 13 parserInterByteTimeout.on('data', (data) => {
14 logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); 14 logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() });
15 }); 15 });
16 16
17 let port; 17 let port;
18 18
19 function sleep(ms) { 19 function sleep(ms) {
20 return new Promise((resolve) => { 20 return new Promise((resolve) => {
21 setTimeout(() => { 21 setTimeout(() => {
22 resolve(); 22 resolve();
23 }, ms || 0); 23 }, ms || 0);
24 }); 24 });
25 } 25 }
26 26
27 function writeToPort(data) { 27 function writeToPort(data) {
28 return new Promise((resolve) => { 28 return new Promise((resolve) => {
29 port.write(data, (err, bytesWritten) => { 29 port.write(data, (err, bytesWritten) => {
30 if (err) logger.warn(`ERROR: ${err.toString()}`); 30 if (err) logger.warn(`ERROR: ${err.toString()}`);
31 31
32 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); 32 logger.verbose('OUTGOING', { bytesWritten, data: data.toString() });
33 resolve(bytesWritten); 33 resolve(bytesWritten);
34 }); 34 });
35 }); 35 });
36 } 36 }
37 37
38 async function writeToPortDelayed(data, ms) { 38 async function writeToPortDelayed(data, ms) {
39 await sleep(ms || 500); 39 await sleep(ms || 500);
40 const result = writeToPort(data); 40 const result = writeToPort(data);
41 return result; 41 return result;
42 } 42 }
43 43
44 port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { 44 port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => {
45 if (err) { 45 if (err) {
46 logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); 46 logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`);
47 process.exit(1); 47 process.exit(1);
48 } 48 }
49 49
50 await writeToPortDelayed('AT\r'); 50 await writeToPortDelayed('AT\r');
51 51
52 const commands = [ 52 const commands = [
53 'AT&FE0\r', 53 'AT&FE0\r',
54 'AT+CIMI\r', 54 'AT+CIMI\r',
55 'AT+CMGF=0\r', 55 'AT+CMGF=0\r',
56 'AT+CNMI=1,2,0,1,0\r', 56 'AT+CNMI=1,2,0,1,0\r',
57 ]; 57 ];
58 58
59 const commandsCount = commands.length; 59 const commandsCount = commands.length;
60 // eslint-disable-next-line no-plusplus 60 // eslint-disable-next-line no-plusplus
61 for (let i = 0; i < commandsCount; i++) { 61 for (let i = 0; i < commandsCount; i++) {
62 // eslint-disable-next-line no-await-in-loop 62 // eslint-disable-next-line no-await-in-loop
63 await writeToPortDelayed(commands[i], 2000); 63 await writeToPortDelayed(commands[i], 2000);
64 } 64 }
65 65
66 if (config && config.modem_tester && config.modem_tester.commands 66 if (config && config.modem_tester && config.modem_tester.commands
67 && config.modem_tester.commands.length) { 67 && config.modem_tester.commands.length) {
68 const additionalCommandsLength = config.modem_tester.commands.length; 68 const additionalCommandsLength = config.modem_tester.commands.length;
69 // eslint-disable-next-line no-plusplus 69 // eslint-disable-next-line no-plusplus
70 for (let i = 0; i < additionalCommandsLength; i++) { 70 for (let i = 0; i < additionalCommandsLength; i++) {
71 // eslint-disable-next-line no-await-in-loop 71 // eslint-disable-next-line no-await-in-loop
72 await writeToPortDelayed(config.modem_tester.commands[i], 2000); 72 await writeToPortDelayed(config.modem_tester.commands[i], 2000);
73 } 73 }
74 } 74 }
75 75
76 await modemCommands.queryIMEI(); 76 const imei = await modemCommands.queryIMEI();
77 logger.verbose(`**** IMEI: ${imei}`);
77 await modemCommands.queryCOPS(); 78 await modemCommands.queryCOPS();
78 await modemCommands.querySignalQuality(); 79 await modemCommands.querySignalQuality();
79 80
80 setInterval(() => { 81 setInterval(() => {
81 modemCommands.querySignalQuality(); 82 modemCommands.querySignalQuality();
82 }, 30000); 83 }, 30000);
83 }); 84 });
84 85
85 parsers.setPort(port); 86 parsers.setPort(port);
86 modemCommands.setPort(port); 87 modemCommands.setPort(port);
87 88
88 if (config && config.modem_tester && config.modem_tester.parser === 'regex') { 89 if (config && config.modem_tester && config.modem_tester.parser === 'regex') {
89 logger.info('Using parserWaitForOkOrError'); 90 logger.info('Using parserWaitForOkOrError');
90 port.pipe(parsers.parserWaitForOkOrError); 91 port.pipe(parsers.parserWaitForOkOrError);
91 } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { 92 } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') {
92 logger.info('Using parserInterByteTimeout'); 93 logger.info('Using parserInterByteTimeout');
93 port.pipe(parserInterByteTimeout); 94 port.pipe(parserInterByteTimeout);
94 } else { 95 } else {
95 logger.info('Using parserReadline'); 96 logger.info('Using parserReadline');
96 port.pipe(parsers.parserReadline); 97 port.pipe(parsers.parserReadline);
97 } 98 }
98 99