diff --git a/lib/modem.js b/lib/modem.js index 278c53c..16a1d84 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -9,51 +9,14 @@ const ParserDelimiter = require('@serialport/parser-delimiter'); const config = require('komodo-sdk/config'); const logger = require('komodo-sdk/logger'); -const locks = require('locks'); +const mutex = require('./mutex'); const common = require('./common'); const sms = require('./sms'); let imsi; let signalStrength; -const mutexWaitForOK = locks.createMutex(); -const mutexCommand = locks.createMutex(); - -function setLockWaitForOK() { - return new Promise((resolve) => { - mutexWaitForOK.lock(() => { - resolve(true); - }); - }); -} - -function releaseLockWaitForOK() { - try { - mutexWaitForOK.unlock(); - } catch (e) { - // - } -} - -function setLockWaitForCommand() { - return new Promise((resolve) => { - mutexCommand.lock(() => { - resolve(true); - }); - }); -} - -function releaseLockWaitForCommand() { - setTimeout(() => { - try { - mutexCommand.unlock(); - } catch (e) { - // - } - }, 1500); -} - const port = new SerialPort(config.modem.device, { baudRate: 115200 }); const parserReadLine = new ParserReadline(); @@ -71,10 +34,10 @@ function writeToPort(data) { } async function writeToPortAndWaitForOK(data) { - await setLockWaitForOK(); + await mutex.setLockWaitForOK(); const result = await writeToPort(data); - await setLockWaitForOK(); - releaseLockWaitForOK(); + await mutex.setLockWaitForOK(); + mutex.releaseLockWaitForOK(); return result; } @@ -85,7 +48,7 @@ async function readSMS(slot) { const smsObject = sms.extract(data.toString().trim()); console.log('SMS', smsObject); // eslint-disable-line no-console } - releaseLockWaitForOK(); + mutex.releaseLockWaitForOK(); }); logger.info(`Reading SMS on slot ${slot}`); @@ -128,7 +91,7 @@ parserReadLine.on('data', (data) => { }); parserWaitForOK.on('data', () => { - releaseLockWaitForOK(); + mutex.releaseLockWaitForOK(); }); async function readIMSI() { @@ -139,13 +102,13 @@ async function readIMSI() { imsi = data.toString().trim(); logger.info(`IMSI: ${imsi}`); } - releaseLockWaitForOK(); + mutex.releaseLockWaitForOK(); }); port.pipe(parserReadIMSI); await writeToPortAndWaitForOK('AT+CIMI\r'); - await setLockWaitForOK(); - releaseLockWaitForOK(); + await mutex.setLockWaitForOK(); + mutex.releaseLockWaitForOK(); port.unpipe(parserReadIMSI); } @@ -164,7 +127,7 @@ async function registerSignalStrengthBackgroundQuery() { } async function sendSMS(destination, msg) { - await setLockWaitForCommand(); + await mutex.setLockWaitForCommand(); logger.info('Sending message', { destination, msg }); const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+'); @@ -176,7 +139,7 @@ async function sendSMS(destination, msg) { logger.info('Message has been sent'); - releaseLockWaitForCommand(); + mutex.releaseLockWaitForCommand(); } function init() { diff --git a/lib/mutex.js b/lib/mutex.js new file mode 100644 index 0000000..cf255bd --- /dev/null +++ b/lib/mutex.js @@ -0,0 +1,46 @@ +'use strict'; + +const locks = require('locks'); + +const mutexWaitForOK = locks.createMutex(); +const mutexCommand = locks.createMutex(); + +function setLockWaitForOK() { + return new Promise((resolve) => { + mutexWaitForOK.lock(() => { + resolve(true); + }); + }); +} + +function releaseLockWaitForOK() { + try { + mutexWaitForOK.unlock(); + } catch (e) { + // + } +} + +function setLockWaitForCommand() { + return new Promise((resolve) => { + mutexCommand.lock(() => { + resolve(true); + }); + }); +} + +function releaseLockWaitForCommand() { + setTimeout(() => { + try { + mutexCommand.unlock(); + } catch (e) { + // + } + }, 1500); +} + +exports.setLockWaitForOK = setLockWaitForOK; +exports.releaseLockWaitForOK = releaseLockWaitForOK; + +exports.setLockWaitForCommand = setLockWaitForCommand; +exports.releaseLockWaitForCommand = releaseLockWaitForCommand;