Commit c5ce14d55825fca17560d387f65e4361a52914ae
1 parent
027645527b
Exists in
master
Uji coba ussd
Showing 3 changed files with 59 additions and 1 deletions Side-by-side Diff
lib/http-command-server/index.js
... | ... | @@ -7,6 +7,7 @@ const logger = require('komodo-sdk/logger'); |
7 | 7 | |
8 | 8 | const routerInfo = require('./router-info'); |
9 | 9 | const routerSMS = require('./router-sms'); |
10 | +const routerUSSD = require('./router-ussd'); | |
10 | 11 | |
11 | 12 | const app = express(); |
12 | 13 | |
... | ... | @@ -37,7 +38,7 @@ app.use(middlewareCheckApikey); |
37 | 38 | |
38 | 39 | app.use('/info', routerInfo); |
39 | 40 | app.use('/sms', routerSMS); |
40 | - | |
41 | +app.use('/ussd', routerUSSD); | |
41 | 42 | |
42 | 43 | app.listen(config.http_command_server.listen_port, () => { |
43 | 44 | logger.info(`HTTP command server listeing on port ${config.http_command_server.listen_port}`); |
lib/http-command-server/router-ussd.js
... | ... | @@ -0,0 +1,29 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const express = require('express'); | |
4 | + | |
5 | +const modem = require('../modem'); | |
6 | + | |
7 | +const router = express.Router(); | |
8 | +module.exports = router; | |
9 | + | |
10 | + | |
11 | +async function handlerIndex(req, res) { | |
12 | + if (!req.query || !req.query.code || typeof req.query.code !== 'string' || !req.query.code.trim()) { | |
13 | + res.json({ | |
14 | + status: 'NOT-OK', | |
15 | + error: 'INVALID-PARAMETER', | |
16 | + message: 'Undefined parameter: code', | |
17 | + }); | |
18 | + } | |
19 | + | |
20 | + const reply = await modem.executeUSSD(req.query.code.trim()); | |
21 | + res.json({ | |
22 | + status: 'OK', | |
23 | + error: false, | |
24 | + message: 'USSD executed', | |
25 | + result: reply, | |
26 | + }); | |
27 | +} | |
28 | + | |
29 | +router.get('/', handlerIndex); |
lib/modem.js
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000; |
4 | 4 | const MAX_LAST_DATA_AGE_MS = 3 * 60 * 1000; |
5 | 5 | const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r/; |
6 | +// const REGEX_WAIT_FOR_OK_OR_ERROR_USSD = /\n(?:OK|ERROR)\r/; | |
6 | 7 | |
7 | 8 | const moment = require('moment'); |
8 | 9 | const SerialPort = require('serialport'); |
... | ... | @@ -305,6 +306,32 @@ async function sendSMS(destination, msg) { |
305 | 306 | mutex.releaseLockWaitForCommand(); |
306 | 307 | }, config.wait_for_release_lock_wait_for_command_ms || 2000); |
307 | 308 | } |
309 | +/** | |
310 | + * Ekseksusi kode USSD. | |
311 | + * | |
312 | + * @param {string} cmd - Kode USSD | |
313 | + */ | |
314 | +function executeUSSD(cmd) { | |
315 | + return new Promise(async (resolve) => { | |
316 | + const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | |
317 | + parser.on('data', (data) => { | |
318 | + port.unpipe(parser); | |
319 | + mutex.releaseLockWaitForSubCommand(); | |
320 | + resolve(data); | |
321 | + }); | |
322 | + | |
323 | + logger.verbose('Waiting for command lock to execute USSD'); | |
324 | + await mutex.setLockWaitForCommand(); | |
325 | + | |
326 | + await mutex.setLockWaitForSubCommand(); | |
327 | + await writeToPort(`AT+CUSD=1,"${cmd.trim()}",15\r`); | |
328 | + | |
329 | + await mutex.setLockWaitForSubCommand(); | |
330 | + mutex.releaseLockWaitForSubCommand(); | |
331 | + | |
332 | + mutex.releaseLockWaitForCommand(); | |
333 | + }); | |
334 | +} | |
308 | 335 | |
309 | 336 | function init() { |
310 | 337 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => { |
... | ... | @@ -364,3 +391,4 @@ init(); |
364 | 391 | |
365 | 392 | exports.modemInfo = modemInfo; |
366 | 393 | exports.sendSMS = sendSMS; |
394 | +exports.executeUSSD = executeUSSD; |