Commit 0bdac2f9c68646872fc3ed0676950c56ff8468af
1 parent
26921e0c2d
Exists in
master
Bersih kirim sms banyak sekaligus tdk tabrakan dengan CSQ
Showing 3 changed files with 107 additions and 38 deletions Side-by-side Diff
config.sample.json
lib/modem.js
... | ... | @@ -36,7 +36,13 @@ const modemInfo = { |
36 | 36 | const port = new SerialPort(config.modem.device, { baudRate: 115200 }); |
37 | 37 | |
38 | 38 | const parserReadLine = new ParserReadline(); |
39 | + | |
39 | 40 | const parserWaitForOK = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); |
41 | +parserWaitForOK.on('data', () => { | |
42 | + mutex.releaseLockWaitForCommand(); | |
43 | +}); | |
44 | + | |
45 | + | |
40 | 46 | port.pipe(parserReadLine); |
41 | 47 | |
42 | 48 | function writeToPort(data) { |
... | ... | @@ -50,33 +56,41 @@ function writeToPort(data) { |
50 | 56 | } |
51 | 57 | |
52 | 58 | async function writeToPortAndWaitForOK(data) { |
53 | - await mutex.setLockWaitForOK(); | |
59 | + await mutex.setLockWaitForCommand(); | |
54 | 60 | const result = await writeToPort(data); |
55 | - await mutex.setLockWaitForOK(); | |
56 | - mutex.releaseLockWaitForOK(); | |
61 | + | |
62 | + await mutex.setLockWaitForCommand(); | |
63 | + mutex.releaseLockWaitForCommand(); | |
64 | + | |
57 | 65 | return result; |
58 | 66 | } |
59 | 67 | |
60 | 68 | async function readSMS(slot) { |
61 | - const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | |
62 | - parser.on('data', async (data) => { | |
69 | + const parserCMGR = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | |
70 | + parserCMGR.on('data', (data) => { | |
63 | 71 | if (data) { |
64 | 72 | reportSender.incomingSMS(sms.extract(data.toString().trim())); |
65 | 73 | } |
66 | - mutex.releaseLockWaitForOK(); | |
74 | + port.unpipe(parserCMGR); | |
75 | + mutex.releaseLockWaitForCommand(); | |
76 | + }); | |
77 | + | |
78 | + const parserCMGD = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | |
79 | + parserCMGD.on('data', () => { | |
80 | + port.unpipe(parserCMGD); | |
81 | + mutex.releaseLockWaitForCommand(); | |
67 | 82 | }); |
68 | 83 | |
69 | 84 | logger.info(`Reading SMS on slot ${slot}`); |
70 | - port.pipe(parser); | |
71 | - await writeToPortAndWaitForOK(`AT+CMGR=${slot}\r`); | |
72 | - port.unpipe(parser); | |
73 | - logger.verbose(`Finished reading SMS on slot ${slot}`); | |
85 | + await mutex.setLockWaitForCommand(); | |
86 | + port.pipe(parserCMGR); | |
87 | + await writeToPort(`AT+CMGR=${slot}\r`); | |
88 | + logger.info(`Finished reading SMS on slot ${slot}`); | |
74 | 89 | |
75 | 90 | logger.info(`Deleting message on slot ${slot}`); |
76 | - port.pipe(parserWaitForOK); | |
77 | - await writeToPortAndWaitForOK(`AT+CMGD=${slot}\r`); | |
78 | - port.unpipe(parserWaitForOK); | |
79 | - | |
91 | + await mutex.setLockWaitForCommand(); | |
92 | + port.pipe(parserCMGD); | |
93 | + await writeToPort(`AT+CMGD=${slot}\r`); | |
80 | 94 | logger.info('Message processing has completed'); |
81 | 95 | } |
82 | 96 | |
... | ... | @@ -125,15 +139,11 @@ parserReadLine.on('data', (data) => { |
125 | 139 | } |
126 | 140 | }); |
127 | 141 | |
128 | -parserWaitForOK.on('data', () => { | |
129 | - mutex.releaseLockWaitForOK(); | |
130 | -}); | |
131 | - | |
132 | 142 | function simpleCommand(cmd, callback) { |
133 | 143 | const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); |
134 | 144 | parser.on('data', (data) => { |
135 | 145 | port.unpipe(parser); |
136 | - mutex.releaseLockWaitForOK(); | |
146 | + mutex.releaseLockWaitForCommand(); | |
137 | 147 | |
138 | 148 | if (data) { |
139 | 149 | callback(null, data.toString().trim()); |
... | ... | @@ -192,39 +202,68 @@ function readIMSI() { |
192 | 202 | } |
193 | 203 | |
194 | 204 | async function querySignalStrength() { |
195 | - port.pipe(parserWaitForOK); | |
196 | - await writeToPortAndWaitForOK('AT+CSQ\r'); | |
197 | - port.unpipe(parserWaitForOK); | |
205 | + const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | |
206 | + parser.on('data', () => { | |
207 | + port.unpipe(parser); | |
208 | + mutex.releaseLockWaitForCommand(); | |
209 | + }); | |
210 | + | |
211 | + if (mutex.tryLockWaitForCommand()) { | |
212 | + port.pipe(parser); | |
213 | + await writeToPort('AT+CSQ\r'); | |
214 | + } | |
198 | 215 | } |
199 | 216 | |
200 | 217 | async function registerSignalStrengthBackgroundQuery() { |
201 | 218 | logger.info('Registering background signal strength query'); |
202 | 219 | |
220 | + querySignalStrength(); | |
221 | + | |
203 | 222 | setInterval(() => { |
204 | 223 | querySignalStrength(); |
205 | - }, INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS); | |
224 | + }, config.interval_beetwen_signal_strength_ms || INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS); | |
206 | 225 | } |
207 | 226 | |
208 | 227 | async function sendSMS(destination, msg) { |
209 | 228 | if (typeof destination !== 'string' || typeof msg !== 'string' || !destination.trim() || !msg.trim()) return; |
210 | 229 | |
230 | + const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | |
231 | + parser.on('data', () => { | |
232 | + port.unpipe(parser); | |
233 | + mutex.releaseLockWaitForSubCommand(); | |
234 | + }); | |
235 | + | |
236 | + logger.verbose('Waiting for command lock to send message'); | |
211 | 237 | await mutex.setLockWaitForCommand(); |
238 | + | |
212 | 239 | logger.info('Sending message', { destination, msg }); |
213 | 240 | |
214 | 241 | const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+'); |
215 | 242 | |
216 | - port.pipe(parserWaitForOK); | |
217 | - await writeToPortAndWaitForOK('AT+CMGF=1\r'); | |
218 | - await writeToPortAndWaitForOK(`AT+CMGS="${correctedDestination}"\n${msg}${Buffer.from([0x1A])}`); | |
219 | - port.unpipe(parserWaitForOK); | |
243 | + logger.verbose('Waiting for lock before set to text mode'); | |
244 | + await mutex.setLockWaitForSubCommand(); | |
245 | + port.pipe(parser); | |
246 | + await writeToPort('AT+CMGF=1\r'); | |
247 | + | |
248 | + logger.verbose('Waiting for lock before writing message'); | |
249 | + await mutex.setLockWaitForSubCommand(); | |
250 | + port.pipe(parser); | |
251 | + await writeToPort(`AT+CMGS="${correctedDestination}"\n${msg}${Buffer.from([0x1A])}`); | |
252 | + | |
253 | + await mutex.setLockWaitForSubCommand(); | |
254 | + mutex.releaseLockWaitForSubCommand(); | |
220 | 255 | |
221 | 256 | logger.info('Message has been sent'); |
222 | 257 | |
223 | - mutex.releaseLockWaitForCommand(); | |
258 | + setTimeout(() => { | |
259 | + logger.verbose('Releasing command lock'); | |
260 | + mutex.releaseLockWaitForCommand(); | |
261 | + }, 2000); | |
224 | 262 | } |
225 | 263 | |
226 | 264 | function init() { |
227 | 265 | port.on('open', async () => { |
266 | + // await mutex.setLockWaitForCommand(); | |
228 | 267 | port.pipe(parserWaitForOK); |
229 | 268 | |
230 | 269 | logger.info('Modem opened'); |
... | ... | @@ -238,9 +277,6 @@ function init() { |
238 | 277 | |
239 | 278 | await writeToPortAndWaitForOK('AT+COPS?\r'); |
240 | 279 | |
241 | - logger.info('Querying signal strength'); | |
242 | - await writeToPortAndWaitForOK('AT+CSQ\r'); | |
243 | - | |
244 | 280 | await readManufacturer(); |
245 | 281 | await readModel(); |
246 | 282 | await readIMEI(); |
... | ... | @@ -253,6 +289,9 @@ function init() { |
253 | 289 | |
254 | 290 | port.unpipe(parserWaitForOK); |
255 | 291 | |
292 | + await mutex.setLockWaitForCommand(); | |
293 | + mutex.releaseLockWaitForCommand(); | |
294 | + | |
256 | 295 | registerSignalStrengthBackgroundQuery(); |
257 | 296 | logger.verbose('Init completed'); |
258 | 297 | }); |
lib/mutex.js
... | ... | @@ -5,6 +5,8 @@ const locks = require('locks'); |
5 | 5 | const mutexWaitForOK = locks.createMutex(); |
6 | 6 | const mutexCommand = locks.createMutex(); |
7 | 7 | |
8 | +const mutexSubCommand = locks.createMutex(); | |
9 | + | |
8 | 10 | function setLockWaitForOK() { |
9 | 11 | return new Promise((resolve) => { |
10 | 12 | mutexWaitForOK.lock(() => { |
... | ... | @@ -29,14 +31,37 @@ function setLockWaitForCommand() { |
29 | 31 | }); |
30 | 32 | } |
31 | 33 | |
34 | +function tryLockWaitForCommand() { | |
35 | + return mutexCommand.tryLock(); | |
36 | +} | |
37 | + | |
38 | + | |
32 | 39 | function releaseLockWaitForCommand() { |
33 | - setTimeout(() => { | |
34 | - try { | |
35 | - mutexCommand.unlock(); | |
36 | - } catch (e) { | |
37 | - // | |
38 | - } | |
39 | - }, 2000); | |
40 | + try { | |
41 | + mutexCommand.unlock(); | |
42 | + } catch (e) { | |
43 | + // | |
44 | + } | |
45 | +} | |
46 | + | |
47 | +function setLockWaitForSubCommand() { | |
48 | + return new Promise((resolve) => { | |
49 | + mutexSubCommand.lock(() => { | |
50 | + resolve(true); | |
51 | + }); | |
52 | + }); | |
53 | +} | |
54 | + | |
55 | +function releaseLockWaitForSubCommand() { | |
56 | + mutexSubCommand.unlock(); | |
57 | + | |
58 | + /* | |
59 | + try { | |
60 | + mutexSubCommand.unlock(); | |
61 | + } catch (e) { | |
62 | + // | |
63 | + } | |
64 | + */ | |
40 | 65 | } |
41 | 66 | |
42 | 67 | exports.setLockWaitForOK = setLockWaitForOK; |
... | ... | @@ -44,3 +69,7 @@ exports.releaseLockWaitForOK = releaseLockWaitForOK; |
44 | 69 | |
45 | 70 | exports.setLockWaitForCommand = setLockWaitForCommand; |
46 | 71 | exports.releaseLockWaitForCommand = releaseLockWaitForCommand; |
72 | +exports.tryLockWaitForCommand = tryLockWaitForCommand; | |
73 | + | |
74 | +exports.setLockWaitForSubCommand = setLockWaitForSubCommand; | |
75 | +exports.releaseLockWaitForSubCommand = releaseLockWaitForSubCommand; |