Commit 49ca8506b009c10233eb375255109aa9c50ceee8
1 parent
b21c4fec10
Exists in
master
CUSD2
Showing 1 changed file with 31 additions and 4 deletions Side-by-side Diff
lib/modem.js
... | ... | @@ -5,6 +5,9 @@ const MAX_LAST_DATA_AGE_MS = 3 * 60 * 1000; |
5 | 5 | const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r/; |
6 | 6 | // const REGEX_WAIT_FOR_OK_OR_ERROR_USSD = /\n(?:OK|ERROR)\r/; |
7 | 7 | |
8 | +const CUSD2_BEFORE = -1; | |
9 | +const CUSD2_AFTER = 1; | |
10 | + | |
8 | 11 | const moment = require('moment'); |
9 | 12 | const SerialPort = require('serialport'); |
10 | 13 | const ParserReadline = require('@serialport/parser-readline'); |
... | ... | @@ -311,21 +314,42 @@ async function sendSMS(destination, msg) { |
311 | 314 | * |
312 | 315 | * @param {string} code - Kode USSD |
313 | 316 | */ |
314 | -function executeUSSD(code) { | |
317 | +function executeUSSD(code, includeCUSD2) { | |
315 | 318 | 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 | + const parserMain = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | |
320 | + parserMain.on('data', (data) => { | |
321 | + port.unpipe(parserMain); | |
319 | 322 | mutex.releaseLockWaitForSubCommand(); |
320 | 323 | resolve(data); |
321 | 324 | }); |
322 | 325 | |
326 | + const parserCUSD2 = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | |
327 | + parserCUSD2.on('data', () => { | |
328 | + port.unpipe(parserCUSD2); | |
329 | + mutex.releaseLockWaitForSubCommand(); | |
330 | + }); | |
331 | + | |
323 | 332 | logger.verbose('Waiting for command lock to execute USSD'); |
324 | 333 | await mutex.setLockWaitForCommand(); |
325 | 334 | |
335 | + if (includeCUSD2 === CUSD2_BEFORE) { | |
336 | + logger.info('Terminating existing USSD session'); | |
337 | + await mutex.setLockWaitForSubCommand(); | |
338 | + port.pipe(parserCUSD2); | |
339 | + await writeToPort('AT+CUSD=2\r'); | |
340 | + } | |
341 | + | |
326 | 342 | await mutex.setLockWaitForSubCommand(); |
343 | + port.pipe(parserMain); | |
327 | 344 | await writeToPort(`AT+CUSD=1,"${code}",15\r`); |
328 | 345 | |
346 | + if (includeCUSD2 === CUSD2_AFTER) { | |
347 | + logger.info('Terminating existing USSD session'); | |
348 | + await mutex.setLockWaitForSubCommand(); | |
349 | + port.pipe(parserCUSD2); | |
350 | + await writeToPort('AT+CUSD=2\r'); | |
351 | + } | |
352 | + | |
329 | 353 | await mutex.setLockWaitForSubCommand(); |
330 | 354 | mutex.releaseLockWaitForSubCommand(); |
331 | 355 | |
... | ... | @@ -389,6 +413,9 @@ function init() { |
389 | 413 | |
390 | 414 | init(); |
391 | 415 | |
416 | +exports.CUSD2_BEFORE = CUSD2_BEFORE; | |
417 | +exports.CUSD2_AFTER = CUSD2_AFTER; | |
418 | + | |
392 | 419 | exports.modemInfo = modemInfo; |
393 | 420 | exports.sendSMS = sendSMS; |
394 | 421 | exports.executeUSSD = executeUSSD; |