From b66966e5c54c4b3c671f2234f0710ae43377ac64 Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <me@adhisimon.org>
Date: Wed, 16 Oct 2019 18:01:54 +0700
Subject: [PATCH] Ready to rock. I hope so

---
 lib/bootstrap.js                      |  2 +-
 lib/http-command-server/index.js      |  9 ++++++++-
 lib/http-command-server/router-sms.js | 16 +++++++++++-----
 lib/modem-commands/index.js           |  9 ++++++++-
 package-lock.json                     | 13 +++++++++++++
 package.json                          |  1 +
 6 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/lib/bootstrap.js b/lib/bootstrap.js
index c8fd9d0..26d89d6 100644
--- a/lib/bootstrap.js
+++ b/lib/bootstrap.js
@@ -60,7 +60,7 @@ const port = new SerialPort(config.modem.device, {
         logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
     }
 
-    if ((modemInfo.networkName && modemInfo.networkName === 'TELKOMSEL') || config.bootstrap_tsel_sms_quota_check) {
+    if ((config.bootstrap_sms_quota_check && modemInfo.networkName && modemInfo.networkName === 'TELKOMSEL') || config.bootstrap_tsel_sms_quota_check) {
         const ussdResponse = await modemCommands.executeUSSD(ussdCodes.TSEL_SMS_QUOTA_CHECK, 1);
         logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
     }
diff --git a/lib/http-command-server/index.js b/lib/http-command-server/index.js
index a629d34..2354be9 100644
--- a/lib/http-command-server/index.js
+++ b/lib/http-command-server/index.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const express = require('express');
+const shortid = require('shortid');
 
 const config = require('komodo-sdk/config');
 const logger = require('komodo-sdk/logger');
@@ -11,13 +12,18 @@ const routerUSSD = require('./router-ussd');
 
 const app = express();
 
+function middlewareInit(req, res, next) {
+    res.locals.xid = shortid.generate();
+    next();
+}
+
 function middlewareCustomLog(req, res, next) {
     if (req.url.indexOf('/favicon.ico') === 0) {
         res.sendStatus(404);
         return;
     }
 
-    logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url });
+    logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url, xid: res.locals.xid });
     next();
 }
 
@@ -33,6 +39,7 @@ function middlewareCheckApikey(req, res, next) {
     }
 }
 
+app.use(middlewareInit);
 app.use(middlewareCustomLog);
 app.use(middlewareCheckApikey);
 
diff --git a/lib/http-command-server/router-sms.js b/lib/http-command-server/router-sms.js
index 4585d0b..7589976 100644
--- a/lib/http-command-server/router-sms.js
+++ b/lib/http-command-server/router-sms.js
@@ -1,14 +1,15 @@
-'use strict';
-
 const express = require('express');
 
-// const modem = require('../modem');
+const logger = require('komodo-sdk/logger');
+
 const modem = require('../modem-commands');
 
 const router = express.Router();
 module.exports = router;
 
-function handlerIndex(req, res) {
+async function handlerIndex(req, res) {
+    logger.verbose('ROUTER-SMS: Got a request to send sms', { destination: req.query.number, msg: req.query.msg, xid: res.locals.xid });
+
     if (!req.query || !req.query.number || !req.query.msg) {
         res.json({
             status: 'NOT-OK',
@@ -23,7 +24,12 @@ function handlerIndex(req, res) {
         error: false,
         message: 'Message queued.',
     });
-    modem.sendSMS(req.query.number, req.query.msg);
+
+    const result = await modem.sendSMS(req.query.number, req.query.msg);
+
+    logger.verbose('ROUTER-SMS: Finished sending SMS', {
+        destination: req.query.number, msg: req.query.msg, xid: res.locals.xid, result,
+    });
 }
 
 router.get('/', handlerIndex);
diff --git a/lib/modem-commands/index.js b/lib/modem-commands/index.js
index e765d30..8e5932b 100644
--- a/lib/modem-commands/index.js
+++ b/lib/modem-commands/index.js
@@ -4,6 +4,7 @@
  * @module modem-commands
  */
 
+const SLEEP_AFTER_SENDING_SMS_MS = 2000;
 
 /**
  * Label mutex command
@@ -303,8 +304,14 @@ function sendSMS(destination, msg) {
             if (data.indexOf('ERROR') >= 0 || data.indexOf('+CMS ERROR') >= 0 || data.indexOf('+CMGS') >= 0) {
                 logger.info('SMS sent', { data });
                 parsers.setSmsSentCallback(null);
-                mutex.unlock(MUTEX_COMMAND, 'sendSMS');
+
                 resolve(data.indexOf('ERROR') >= 0 ? null : data.toString().trim());
+
+                if (SLEEP_AFTER_SENDING_SMS_MS) {
+                    await sleep(SLEEP_AFTER_SENDING_SMS_MS);
+                }
+
+                mutex.unlock(MUTEX_COMMAND, 'sendSMS');
             }
         }
 
diff --git a/package-lock.json b/package-lock.json
index 48b71f2..81e2dc4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -3126,6 +3126,11 @@
       "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
       "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg=="
     },
+    "nanoid": {
+      "version": "2.1.3",
+      "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.3.tgz",
+      "integrity": "sha512-SbgVmGjEUAR/rYdAM0p0TCdKtJILZeYk3JavV2cmNVmIeR0SaKDudLRk58au6gpJqyFM9qz8ufEsS91D7RZyYA=="
+    },
     "nanomatch": {
       "version": "1.2.13",
       "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz",
@@ -4047,6 +4052,14 @@
       "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
       "dev": true
     },
+    "shortid": {
+      "version": "2.2.15",
+      "resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.15.tgz",
+      "integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==",
+      "requires": {
+        "nanoid": "^2.1.0"
+      }
+    },
     "should": {
       "version": "13.2.3",
       "resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz",
diff --git a/package.json b/package.json
index 47e4bf6..51f9763 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,7 @@
     "request": "^2.88.0",
     "serialport": "^7.1.5",
     "serialport-gsm": "^3.2.0",
+    "shortid": "^2.2.15",
     "sms-pdu-node": "^0.1.2",
     "uuid": "^3.3.3"
   }
-- 
1.9.0