Commit b66966e5c54c4b3c671f2234f0710ae43377ac64

Authored by Adhidarma Hadiwinoto
1 parent b53db5e8b6
Exists in master

Ready to rock. I hope so

Showing 6 changed files with 42 additions and 8 deletions Side-by-side Diff

... ... @@ -60,7 +60,7 @@ const port = new SerialPort(config.modem.device, {
60 60 logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
61 61 }
62 62  
63   - if ((modemInfo.networkName && modemInfo.networkName === 'TELKOMSEL') || config.bootstrap_tsel_sms_quota_check) {
  63 + if ((config.bootstrap_sms_quota_check && modemInfo.networkName && modemInfo.networkName === 'TELKOMSEL') || config.bootstrap_tsel_sms_quota_check) {
64 64 const ussdResponse = await modemCommands.executeUSSD(ussdCodes.TSEL_SMS_QUOTA_CHECK, 1);
65 65 logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
66 66 }
lib/http-command-server/index.js
1 1 'use strict';
2 2  
3 3 const express = require('express');
  4 +const shortid = require('shortid');
4 5  
5 6 const config = require('komodo-sdk/config');
6 7 const logger = require('komodo-sdk/logger');
... ... @@ -11,13 +12,18 @@ const routerUSSD = require('./router-ussd');
11 12  
12 13 const app = express();
13 14  
  15 +function middlewareInit(req, res, next) {
  16 + res.locals.xid = shortid.generate();
  17 + next();
  18 +}
  19 +
14 20 function middlewareCustomLog(req, res, next) {
15 21 if (req.url.indexOf('/favicon.ico') === 0) {
16 22 res.sendStatus(404);
17 23 return;
18 24 }
19 25  
20   - logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url });
  26 + logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url, xid: res.locals.xid });
21 27 next();
22 28 }
23 29  
... ... @@ -33,6 +39,7 @@ function middlewareCheckApikey(req, res, next) {
33 39 }
34 40 }
35 41  
  42 +app.use(middlewareInit);
36 43 app.use(middlewareCustomLog);
37 44 app.use(middlewareCheckApikey);
38 45  
lib/http-command-server/router-sms.js
1   -'use strict';
2   -
3 1 const express = require('express');
4 2  
5   -// const modem = require('../modem');
  3 +const logger = require('komodo-sdk/logger');
  4 +
6 5 const modem = require('../modem-commands');
7 6  
8 7 const router = express.Router();
9 8 module.exports = router;
10 9  
11   -function handlerIndex(req, res) {
  10 +async function handlerIndex(req, res) {
  11 + logger.verbose('ROUTER-SMS: Got a request to send sms', { destination: req.query.number, msg: req.query.msg, xid: res.locals.xid });
  12 +
12 13 if (!req.query || !req.query.number || !req.query.msg) {
13 14 res.json({
14 15 status: 'NOT-OK',
... ... @@ -23,7 +24,12 @@ function handlerIndex(req, res) {
23 24 error: false,
24 25 message: 'Message queued.',
25 26 });
26   - modem.sendSMS(req.query.number, req.query.msg);
  27 +
  28 + const result = await modem.sendSMS(req.query.number, req.query.msg);
  29 +
  30 + logger.verbose('ROUTER-SMS: Finished sending SMS', {
  31 + destination: req.query.number, msg: req.query.msg, xid: res.locals.xid, result,
  32 + });
27 33 }
28 34  
29 35 router.get('/', handlerIndex);
lib/modem-commands/index.js
... ... @@ -4,6 +4,7 @@
4 4 * @module modem-commands
5 5 */
6 6  
  7 +const SLEEP_AFTER_SENDING_SMS_MS = 2000;
7 8  
8 9 /**
9 10 * Label mutex command
... ... @@ -303,8 +304,14 @@ function sendSMS(destination, msg) {
303 304 if (data.indexOf('ERROR') >= 0 || data.indexOf('+CMS ERROR') >= 0 || data.indexOf('+CMGS') >= 0) {
304 305 logger.info('SMS sent', { data });
305 306 parsers.setSmsSentCallback(null);
306   - mutex.unlock(MUTEX_COMMAND, 'sendSMS');
  307 +
307 308 resolve(data.indexOf('ERROR') >= 0 ? null : data.toString().trim());
  309 +
  310 + if (SLEEP_AFTER_SENDING_SMS_MS) {
  311 + await sleep(SLEEP_AFTER_SENDING_SMS_MS);
  312 + }
  313 +
  314 + mutex.unlock(MUTEX_COMMAND, 'sendSMS');
308 315 }
309 316 }
310 317  
... ... @@ -3126,6 +3126,11 @@
3126 3126 "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
3127 3127 "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg=="
3128 3128 },
  3129 + "nanoid": {
  3130 + "version": "2.1.3",
  3131 + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.3.tgz",
  3132 + "integrity": "sha512-SbgVmGjEUAR/rYdAM0p0TCdKtJILZeYk3JavV2cmNVmIeR0SaKDudLRk58au6gpJqyFM9qz8ufEsS91D7RZyYA=="
  3133 + },
3129 3134 "nanomatch": {
3130 3135 "version": "1.2.13",
3131 3136 "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz",
... ... @@ -4047,6 +4052,14 @@
4047 4052 "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
4048 4053 "dev": true
4049 4054 },
  4055 + "shortid": {
  4056 + "version": "2.2.15",
  4057 + "resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.15.tgz",
  4058 + "integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==",
  4059 + "requires": {
  4060 + "nanoid": "^2.1.0"
  4061 + }
  4062 + },
4050 4063 "should": {
4051 4064 "version": "13.2.3",
4052 4065 "resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz",
... ... @@ -41,6 +41,7 @@
41 41 "request": "^2.88.0",
42 42 "serialport": "^7.1.5",
43 43 "serialport-gsm": "^3.2.0",
  44 + "shortid": "^2.2.15",
44 45 "sms-pdu-node": "^0.1.2",
45 46 "uuid": "^3.3.3"
46 47 }