Commit cba7eef40e86fe792e58fd54ad7a7412a7823c4a
1 parent
67af4245ef
Exists in
master
Separate locks functions to mutex module
Showing 2 changed files with 57 additions and 48 deletions Side-by-side Diff
lib/modem.js
... | ... | @@ -9,51 +9,14 @@ const ParserDelimiter = require('@serialport/parser-delimiter'); |
9 | 9 | |
10 | 10 | const config = require('komodo-sdk/config'); |
11 | 11 | const logger = require('komodo-sdk/logger'); |
12 | -const locks = require('locks'); | |
13 | 12 | |
13 | +const mutex = require('./mutex'); | |
14 | 14 | const common = require('./common'); |
15 | 15 | const sms = require('./sms'); |
16 | 16 | |
17 | 17 | let imsi; |
18 | 18 | let signalStrength; |
19 | 19 | |
20 | -const mutexWaitForOK = locks.createMutex(); | |
21 | -const mutexCommand = locks.createMutex(); | |
22 | - | |
23 | -function setLockWaitForOK() { | |
24 | - return new Promise((resolve) => { | |
25 | - mutexWaitForOK.lock(() => { | |
26 | - resolve(true); | |
27 | - }); | |
28 | - }); | |
29 | -} | |
30 | - | |
31 | -function releaseLockWaitForOK() { | |
32 | - try { | |
33 | - mutexWaitForOK.unlock(); | |
34 | - } catch (e) { | |
35 | - // | |
36 | - } | |
37 | -} | |
38 | - | |
39 | -function setLockWaitForCommand() { | |
40 | - return new Promise((resolve) => { | |
41 | - mutexCommand.lock(() => { | |
42 | - resolve(true); | |
43 | - }); | |
44 | - }); | |
45 | -} | |
46 | - | |
47 | -function releaseLockWaitForCommand() { | |
48 | - setTimeout(() => { | |
49 | - try { | |
50 | - mutexCommand.unlock(); | |
51 | - } catch (e) { | |
52 | - // | |
53 | - } | |
54 | - }, 1500); | |
55 | -} | |
56 | - | |
57 | 20 | const port = new SerialPort(config.modem.device, { baudRate: 115200 }); |
58 | 21 | |
59 | 22 | const parserReadLine = new ParserReadline(); |
... | ... | @@ -71,10 +34,10 @@ function writeToPort(data) { |
71 | 34 | } |
72 | 35 | |
73 | 36 | async function writeToPortAndWaitForOK(data) { |
74 | - await setLockWaitForOK(); | |
37 | + await mutex.setLockWaitForOK(); | |
75 | 38 | const result = await writeToPort(data); |
76 | - await setLockWaitForOK(); | |
77 | - releaseLockWaitForOK(); | |
39 | + await mutex.setLockWaitForOK(); | |
40 | + mutex.releaseLockWaitForOK(); | |
78 | 41 | return result; |
79 | 42 | } |
80 | 43 | |
... | ... | @@ -85,7 +48,7 @@ async function readSMS(slot) { |
85 | 48 | const smsObject = sms.extract(data.toString().trim()); |
86 | 49 | console.log('SMS', smsObject); // eslint-disable-line no-console |
87 | 50 | } |
88 | - releaseLockWaitForOK(); | |
51 | + mutex.releaseLockWaitForOK(); | |
89 | 52 | }); |
90 | 53 | |
91 | 54 | logger.info(`Reading SMS on slot ${slot}`); |
... | ... | @@ -128,7 +91,7 @@ parserReadLine.on('data', (data) => { |
128 | 91 | }); |
129 | 92 | |
130 | 93 | parserWaitForOK.on('data', () => { |
131 | - releaseLockWaitForOK(); | |
94 | + mutex.releaseLockWaitForOK(); | |
132 | 95 | }); |
133 | 96 | |
134 | 97 | async function readIMSI() { |
... | ... | @@ -139,13 +102,13 @@ async function readIMSI() { |
139 | 102 | imsi = data.toString().trim(); |
140 | 103 | logger.info(`IMSI: ${imsi}`); |
141 | 104 | } |
142 | - releaseLockWaitForOK(); | |
105 | + mutex.releaseLockWaitForOK(); | |
143 | 106 | }); |
144 | 107 | |
145 | 108 | port.pipe(parserReadIMSI); |
146 | 109 | await writeToPortAndWaitForOK('AT+CIMI\r'); |
147 | - await setLockWaitForOK(); | |
148 | - releaseLockWaitForOK(); | |
110 | + await mutex.setLockWaitForOK(); | |
111 | + mutex.releaseLockWaitForOK(); | |
149 | 112 | port.unpipe(parserReadIMSI); |
150 | 113 | } |
151 | 114 | |
... | ... | @@ -164,7 +127,7 @@ async function registerSignalStrengthBackgroundQuery() { |
164 | 127 | } |
165 | 128 | |
166 | 129 | async function sendSMS(destination, msg) { |
167 | - await setLockWaitForCommand(); | |
130 | + await mutex.setLockWaitForCommand(); | |
168 | 131 | logger.info('Sending message', { destination, msg }); |
169 | 132 | |
170 | 133 | const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+'); |
... | ... | @@ -176,7 +139,7 @@ async function sendSMS(destination, msg) { |
176 | 139 | |
177 | 140 | logger.info('Message has been sent'); |
178 | 141 | |
179 | - releaseLockWaitForCommand(); | |
142 | + mutex.releaseLockWaitForCommand(); | |
180 | 143 | } |
181 | 144 | |
182 | 145 | function init() { |
lib/mutex.js
... | ... | @@ -0,0 +1,46 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const locks = require('locks'); | |
4 | + | |
5 | +const mutexWaitForOK = locks.createMutex(); | |
6 | +const mutexCommand = locks.createMutex(); | |
7 | + | |
8 | +function setLockWaitForOK() { | |
9 | + return new Promise((resolve) => { | |
10 | + mutexWaitForOK.lock(() => { | |
11 | + resolve(true); | |
12 | + }); | |
13 | + }); | |
14 | +} | |
15 | + | |
16 | +function releaseLockWaitForOK() { | |
17 | + try { | |
18 | + mutexWaitForOK.unlock(); | |
19 | + } catch (e) { | |
20 | + // | |
21 | + } | |
22 | +} | |
23 | + | |
24 | +function setLockWaitForCommand() { | |
25 | + return new Promise((resolve) => { | |
26 | + mutexCommand.lock(() => { | |
27 | + resolve(true); | |
28 | + }); | |
29 | + }); | |
30 | +} | |
31 | + | |
32 | +function releaseLockWaitForCommand() { | |
33 | + setTimeout(() => { | |
34 | + try { | |
35 | + mutexCommand.unlock(); | |
36 | + } catch (e) { | |
37 | + // | |
38 | + } | |
39 | + }, 1500); | |
40 | +} | |
41 | + | |
42 | +exports.setLockWaitForOK = setLockWaitForOK; | |
43 | +exports.releaseLockWaitForOK = releaseLockWaitForOK; | |
44 | + | |
45 | +exports.setLockWaitForCommand = setLockWaitForCommand; | |
46 | +exports.releaseLockWaitForCommand = releaseLockWaitForCommand; |