Commit 2a4678f67a1c6d279a2d4b343eb0cd9edd0a8242
1 parent
e816f09945
Exists in
master
global MODEM_PORT
Showing 1 changed file with 116 additions and 30 deletions Side-by-side Diff
lib/modem-commands/index.js
1 | 1 | /** |
2 | 2 | * Modul modem-commands |
3 | 3 | * |
4 | - * @module | |
4 | + * @module modem-commands | |
5 | 5 | */ |
6 | 6 | |
7 | 7 | |
8 | +/** | |
9 | + * Label mutex command | |
10 | + * @static | |
11 | + */ | |
8 | 12 | const MUTEX_COMMAND = 'COMMAND'; |
9 | -exports.MUTEX_COMMAND = MUTEX_COMMAND; | |
10 | 13 | |
14 | +/** | |
15 | + * Label mutex subcommand | |
16 | + * @static | |
17 | + */ | |
11 | 18 | const MUTEX_SUBCOMMAND = 'SUBCOMMAND'; |
12 | -exports.MUTEX_SUBCOMMAND = MUTEX_SUBCOMMAND; | |
13 | 19 | |
14 | 20 | /** |
15 | 21 | * CTRL-Z string |
16 | - * @constant | |
22 | + * @static | |
17 | 23 | */ |
18 | 24 | const CTRLZ = '\u001a'; |
19 | -exports.CTRLZ = CTRLZ; | |
25 | + | |
20 | 26 | |
21 | 27 | const pdu = require('node-pdu'); |
22 | 28 | const uuidv1 = require('uuid/v1'); |
... | ... | @@ -30,10 +36,12 @@ const mutex = require('../mutex-common'); |
30 | 36 | const parsers = require('../serialport-parsers'); |
31 | 37 | const modemInfo = require('../modem-info'); |
32 | 38 | |
33 | -let port; | |
39 | +// let port; | |
34 | 40 | |
35 | 41 | function writeToPort(data) { |
36 | 42 | return new Promise((resolve) => { |
43 | + const port = global.MODEM_PORT; | |
44 | + | |
37 | 45 | modemInfo.lastWriteTs = new Date(); |
38 | 46 | port.write(data, (err, bytesWritten) => { |
39 | 47 | if (err) logger.warn(`ERROR: ${err.toString()}`); |
... | ... | @@ -43,9 +51,9 @@ function writeToPort(data) { |
43 | 51 | }); |
44 | 52 | }); |
45 | 53 | } |
46 | -exports.writeToPort = writeToPort; | |
47 | 54 | |
48 | 55 | function writeToPortAndWaitForReadline(cmd, lockName) { |
56 | + const port = global.MODEM_PORT; | |
49 | 57 | let resolved = false; |
50 | 58 | |
51 | 59 | return new Promise(async (resolve) => { |
... | ... | @@ -64,11 +72,12 @@ function writeToPortAndWaitForReadline(cmd, lockName) { |
64 | 72 | await writeToPort(cmd); |
65 | 73 | }); |
66 | 74 | } |
67 | -exports.writeToPortAndWaitForReadline = writeToPortAndWaitForReadline; | |
68 | 75 | |
69 | 76 | function writeToPortAndWaitForOkOrError(cmd, lockName) { |
70 | 77 | return new Promise(async (resolve) => { |
78 | + const port = global.MODEM_PORT; | |
71 | 79 | const parser = new ParserRegex({ regex: /(?:OK|ERROR)\r\n/ }); |
80 | + | |
72 | 81 | parser.on('data', (data) => { |
73 | 82 | port.unpipe(parser); |
74 | 83 | mutex.unlock(lockName || MUTEX_COMMAND, cmd.trim()); |
... | ... | @@ -80,8 +89,13 @@ function writeToPortAndWaitForOkOrError(cmd, lockName) { |
80 | 89 | await writeToPort(cmd); |
81 | 90 | }); |
82 | 91 | } |
83 | -exports.writeToPortAndWaitForOkOrError = writeToPortAndWaitForOkOrError; | |
84 | 92 | |
93 | +/** | |
94 | + * Sleep async | |
95 | + * @static | |
96 | + * @param {number} ms - Milliseconds to sleep | |
97 | + * @return {Promise} | |
98 | + */ | |
85 | 99 | function sleep(ms) { |
86 | 100 | return new Promise((resolve) => { |
87 | 101 | setTimeout(() => { |
... | ... | @@ -89,11 +103,18 @@ function sleep(ms) { |
89 | 103 | }, ms || 0); |
90 | 104 | }); |
91 | 105 | } |
92 | -exports.sleep = sleep; | |
93 | 106 | |
94 | -exports.setPort = function setPort(val) { | |
95 | - port = val; | |
96 | -}; | |
107 | +/** | |
108 | + * Set port | |
109 | + * @static | |
110 | + * @param {SerialPort} val | |
111 | + */ | |
112 | + | |
113 | +/* | |
114 | +function setPort(val) { | |
115 | + // port = val || global.MODEM_PORT; | |
116 | +} | |
117 | +*/ | |
97 | 118 | |
98 | 119 | function querySignalQuality() { |
99 | 120 | return new Promise(async (resolve) => { |
... | ... | @@ -107,7 +128,6 @@ function querySignalQuality() { |
107 | 128 | resolve(true); |
108 | 129 | }); |
109 | 130 | } |
110 | -exports.querySignalQuality = querySignalQuality; | |
111 | 131 | |
112 | 132 | function queryCOPS(lockName) { |
113 | 133 | return new Promise(async (resolve) => { |
... | ... | @@ -117,7 +137,6 @@ function queryCOPS(lockName) { |
117 | 137 | resolve(true); |
118 | 138 | }); |
119 | 139 | } |
120 | -exports.queryCOPS = queryCOPS; | |
121 | 140 | |
122 | 141 | function queryCOPSAndSignalQuality(skipOnLocked) { |
123 | 142 | return new Promise(async (resolve) => { |
... | ... | @@ -135,11 +154,12 @@ function queryCOPSAndSignalQuality(skipOnLocked) { |
135 | 154 | resolve(true); |
136 | 155 | }); |
137 | 156 | } |
138 | -exports.queryCOPSAndSignalQuality = queryCOPSAndSignalQuality; | |
139 | 157 | |
140 | 158 | function queryIMEI(lockName) { |
141 | 159 | return new Promise(async (resolve) => { |
160 | + const port = global.MODEM_PORT; | |
142 | 161 | const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
162 | + | |
143 | 163 | parser.on('data', (data) => { |
144 | 164 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserIMEI' }); |
145 | 165 | port.unpipe(parser); |
... | ... | @@ -155,11 +175,12 @@ function queryIMEI(lockName) { |
155 | 175 | await writeToPort('AT+CGSN\r'); |
156 | 176 | }); |
157 | 177 | } |
158 | -exports.queryIMEI = queryIMEI; | |
159 | 178 | |
160 | 179 | function queryIMSI(lockName) { |
161 | 180 | return new Promise(async (resolve) => { |
181 | + const port = global.MODEM_PORT; | |
162 | 182 | const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
183 | + | |
163 | 184 | parser.on('data', (data) => { |
164 | 185 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserIMSI' }); |
165 | 186 | port.unpipe(parser); |
... | ... | @@ -175,9 +196,8 @@ function queryIMSI(lockName) { |
175 | 196 | await writeToPort('AT+CIMI\r'); |
176 | 197 | }); |
177 | 198 | } |
178 | -exports.queryIMSI = queryIMSI; | |
179 | 199 | |
180 | -exports.queryIMEIAndIMSI = async function queryIMEIAndIMSI() { | |
200 | +async function queryIMEIAndIMSI() { | |
181 | 201 | await mutex.lock(MUTEX_COMMAND, 'queryIMEIAndIMSI'); |
182 | 202 | |
183 | 203 | const imei = await queryIMEI(MUTEX_SUBCOMMAND); |
... | ... | @@ -185,11 +205,13 @@ exports.queryIMEIAndIMSI = async function queryIMEIAndIMSI() { |
185 | 205 | |
186 | 206 | await mutex.unlock(MUTEX_COMMAND, 'queryIMEIAndIMSI'); |
187 | 207 | return { imei, imsi }; |
188 | -}; | |
208 | +} | |
189 | 209 | |
190 | -exports.queryManufacturer = function queryManufacturer(lockName) { | |
210 | +function queryManufacturer(lockName) { | |
191 | 211 | return new Promise(async (resolve) => { |
212 | + const port = global.MODEM_PORT; | |
192 | 213 | const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
214 | + | |
193 | 215 | parser.on('data', (data) => { |
194 | 216 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserManufacturer' }); |
195 | 217 | port.unpipe(parser); |
... | ... | @@ -204,11 +226,13 @@ exports.queryManufacturer = function queryManufacturer(lockName) { |
204 | 226 | port.pipe(parser); |
205 | 227 | await writeToPort('AT+CGMI\r'); |
206 | 228 | }); |
207 | -}; | |
229 | +} | |
208 | 230 | |
209 | -exports.queryModel = function queryModel(lockName) { | |
231 | +function queryModel(lockName) { | |
210 | 232 | return new Promise(async (resolve) => { |
233 | + const port = global.MODEM_PORT; | |
211 | 234 | const parser = new ParserRegex({ regex: parsers.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
235 | + | |
212 | 236 | parser.on('data', (data) => { |
213 | 237 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserModel' }); |
214 | 238 | port.unpipe(parser); |
... | ... | @@ -223,12 +247,15 @@ exports.queryModel = function queryModel(lockName) { |
223 | 247 | port.pipe(parser); |
224 | 248 | await writeToPort('AT+CGMM\r'); |
225 | 249 | }); |
226 | -}; | |
250 | +} | |
227 | 251 | |
252 | +/** | |
253 | + * Menulis CTRL-Z ke port. | |
254 | + * @static | |
255 | + */ | |
228 | 256 | async function sendCtrlZ() { |
229 | 257 | await writeToPort(CTRLZ); |
230 | 258 | } |
231 | -exports.sendCtrlZ = sendCtrlZ; | |
232 | 259 | |
233 | 260 | async function initATCommands() { |
234 | 261 | await mutex.lock(MUTEX_COMMAND, 'INIT MODEM'); |
... | ... | @@ -237,11 +264,17 @@ async function initATCommands() { |
237 | 264 | await this.writeToPortAndWaitForOkOrError('AT+CNMI=1,2,0,1,0\r', MUTEX_SUBCOMMAND); |
238 | 265 | mutex.unlock(MUTEX_COMMAND, 'INIT MODEM'); |
239 | 266 | } |
240 | -exports.initATCommands = initATCommands; | |
241 | 267 | |
268 | +/** | |
269 | + * Menulis awal pesan PDU. | |
270 | + * | |
271 | + * @param {number} pduLength | |
272 | + */ | |
242 | 273 | function sendCMGSPdu(pduLength) { |
243 | 274 | return new Promise((resolve) => { |
275 | + const port = global.MODEM_PORT; | |
244 | 276 | const parser = new ParserReady({ delimiter: '>' }); |
277 | + | |
245 | 278 | parser.on('data', () => { |
246 | 279 | logger.verbose('Got ">" message prompt, gonna to write PDU message'); |
247 | 280 | port.unpipe(parser); |
... | ... | @@ -255,7 +288,14 @@ function sendCMGSPdu(pduLength) { |
255 | 288 | }); |
256 | 289 | } |
257 | 290 | |
258 | -exports.sendSMS = function sendSMS(destination, msg) { | |
291 | +/** | |
292 | + * Mengirim sms | |
293 | + * @param {string} destination - nomor tujuan | |
294 | + * @param {string} msg - isi pesan | |
295 | + * @return {Promise} | |
296 | + * @static | |
297 | + */ | |
298 | +function sendSMS(destination, msg) { | |
259 | 299 | return new Promise(async (resolve) => { |
260 | 300 | async function responseHandler(data) { |
261 | 301 | logger.verbose('SMS sent callback called', { data }); |
... | ... | @@ -296,9 +336,23 @@ exports.sendSMS = function sendSMS(destination, msg) { |
296 | 336 | parsers.setSmsSentCallback(responseHandler); |
297 | 337 | await writeToPort(`${submit.toString()}${CTRLZ}`, MUTEX_SUBCOMMAND); |
298 | 338 | }); |
299 | -}; | |
339 | +} | |
300 | 340 | |
301 | -exports.executeUSSD = function executeUSSD(code, _includeCUSD2, _sessionId) { | |
341 | +/** | |
342 | + * Ekseksusi kode USSD. | |
343 | + * <br> | |
344 | + * <br>Pilihan includeCUSD2: | |
345 | + * <br>-1: sebelum | |
346 | + * <br>0: tidak (default) | |
347 | + * <br>1: sesudah | |
348 | + * <br>2: sebelum dan sesudah | |
349 | + * | |
350 | + * @static | |
351 | + * @param {string} code - Kode USSD | |
352 | + * @param {number} [includeCUSD2=0] - Apakah ingin otomatis memasukkan CUSD=2 | |
353 | + * @return {Promise} | |
354 | + */ | |
355 | +function executeUSSD(code, _includeCUSD2, _sessionId) { | |
302 | 356 | return new Promise(async (resolve) => { |
303 | 357 | const includeCUSD2 = _includeCUSD2 || 0; |
304 | 358 | const sessionId = _sessionId || uuidv1(); |
... | ... | @@ -326,4 +380,36 @@ exports.executeUSSD = function executeUSSD(code, _includeCUSD2, _sessionId) { |
326 | 380 | |
327 | 381 | await writeToPort(`AT+CUSD=1,"${code}",15\r`, MUTEX_SUBCOMMAND); |
328 | 382 | }); |
329 | -}; | |
383 | +} | |
384 | + | |
385 | +exports.MUTEX_COMMAND = MUTEX_COMMAND; | |
386 | +exports.MUTEX_SUBCOMMAND = MUTEX_SUBCOMMAND; | |
387 | +exports.CTRLZ = CTRLZ; | |
388 | + | |
389 | +/** | |
390 | + * Modem info. | |
391 | + * @type {object} | |
392 | + */ | |
393 | +exports.modemInfo = modemInfo; | |
394 | +// exports.setPort = setPort; | |
395 | + | |
396 | +exports.writeToPort = writeToPort; | |
397 | +exports.writeToPortAndWaitForReadline = writeToPortAndWaitForReadline; | |
398 | +exports.writeToPortAndWaitForOkOrError = writeToPortAndWaitForOkOrError; | |
399 | +exports.sleep = sleep; | |
400 | + | |
401 | +exports.querySignalQuality = querySignalQuality; | |
402 | +exports.queryCOPS = queryCOPS; | |
403 | +exports.queryCOPSAndSignalQuality = queryCOPSAndSignalQuality; | |
404 | + | |
405 | +exports.queryIMEI = queryIMEI; | |
406 | +exports.queryIMSI = queryIMSI; | |
407 | +exports.queryIMEIAndIMSI = queryIMEIAndIMSI; | |
408 | + | |
409 | +exports.queryManufacturer = queryManufacturer; | |
410 | +exports.queryModel = queryModel; | |
411 | + | |
412 | +exports.sendCtrlZ = sendCtrlZ; | |
413 | +exports.initATCommands = initATCommands; | |
414 | +exports.sendSMS = sendSMS; | |
415 | +exports.executeUSSD = executeUSSD; |