Commit 761e213ec49c3114dc3f3c634e3bcb94882f86a6
1 parent
0a21f5dfb2
Exists in
master
USSD pakai parser delimiter
Showing 1 changed file with 4 additions and 2 deletions Inline Diff
lib/modem.js
1 | 'use strict'; | 1 | 'use strict'; |
2 | 2 | ||
3 | const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000; | 3 | const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000; |
4 | const MAX_LAST_DATA_AGE_MS = 3 * 60 * 1000; | 4 | const MAX_LAST_DATA_AGE_MS = 3 * 60 * 1000; |
5 | const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r/; | 5 | const REGEX_WAIT_FOR_OK_OR_ERROR = /\n(?:OK|ERROR)\r/; |
6 | // const REGEX_WAIT_FOR_OK_OR_ERROR_USSD = /\n(?:OK|ERROR)\r/; | 6 | // const REGEX_WAIT_FOR_OK_OR_ERROR_USSD = /\n(?:OK|ERROR)\r/; |
7 | 7 | ||
8 | const CUSD2_BEFORE = -1; | 8 | const CUSD2_BEFORE = -1; |
9 | const CUSD2_AFTER = 1; | 9 | const CUSD2_AFTER = 1; |
10 | 10 | ||
11 | const moment = require('moment'); | 11 | const moment = require('moment'); |
12 | const SerialPort = require('serialport'); | 12 | const SerialPort = require('serialport'); |
13 | const ParserReadline = require('@serialport/parser-readline'); | 13 | const ParserReadline = require('@serialport/parser-readline'); |
14 | // const ParserDelimiter = require('@serialport/parser-delimiter'); | 14 | // const ParserDelimiter = require('@serialport/parser-delimiter'); |
15 | 15 | ||
16 | const ParserRegex = require('@serialport/parser-regex'); | 16 | const ParserRegex = require('@serialport/parser-regex'); |
17 | 17 | ||
18 | const config = require('komodo-sdk/config'); | 18 | const config = require('komodo-sdk/config'); |
19 | const logger = require('komodo-sdk/logger'); | 19 | const logger = require('komodo-sdk/logger'); |
20 | 20 | ||
21 | const mutex = require('./mutex'); | 21 | const mutex = require('./mutex'); |
22 | const common = require('./common'); | 22 | const common = require('./common'); |
23 | const sms = require('./sms'); | 23 | const sms = require('./sms'); |
24 | const dbCops = require('./db-cops'); | 24 | const dbCops = require('./db-cops'); |
25 | const reportSender = require('./report-sender'); | 25 | const reportSender = require('./report-sender'); |
26 | // const msisdn = require('./msisdn'); | 26 | // const msisdn = require('./msisdn'); |
27 | const registerModem = require('./register-modem'); | 27 | const registerModem = require('./register-modem'); |
28 | // const counters = require('./counters'); | 28 | // const counters = require('./counters'); |
29 | 29 | ||
30 | const modemInfo = { | 30 | const modemInfo = { |
31 | device: config.modem.device, | 31 | device: config.modem.device, |
32 | manufacturer: null, | 32 | manufacturer: null, |
33 | model: null, | 33 | model: null, |
34 | imei: null, | 34 | imei: null, |
35 | imsi: null, | 35 | imsi: null, |
36 | msisdn: null, | 36 | msisdn: null, |
37 | cops: null, | 37 | cops: null, |
38 | networkId: null, | 38 | networkId: null, |
39 | networkName: null, | 39 | networkName: null, |
40 | signalStrength: null, | 40 | signalStrength: null, |
41 | signalStrengthTs: null, | 41 | signalStrengthTs: null, |
42 | signalStrengthTsReadable: null, | 42 | signalStrengthTsReadable: null, |
43 | // messageSentCounter: null, | 43 | // messageSentCounter: null, |
44 | // messageReceivedCounter: null, | 44 | // messageReceivedCounter: null, |
45 | }; | 45 | }; |
46 | 46 | ||
47 | let lastTs = new Date(); | 47 | let lastTs = new Date(); |
48 | 48 | ||
49 | let port; | 49 | let port; |
50 | 50 | ||
51 | const parserReadLine = new ParserReadline(); | 51 | const parserReadLine = new ParserReadline(); |
52 | 52 | ||
53 | const parserWaitForOK = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 53 | const parserWaitForOK = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
54 | parserWaitForOK.on('data', () => { | 54 | parserWaitForOK.on('data', () => { |
55 | mutex.releaseLockWaitForCommand(); | 55 | mutex.releaseLockWaitForCommand(); |
56 | }); | 56 | }); |
57 | 57 | ||
58 | function writeToPort(data) { | 58 | function writeToPort(data) { |
59 | return new Promise((resolve) => { | 59 | return new Promise((resolve) => { |
60 | port.write(data, (err, bytesWritten) => { | 60 | port.write(data, (err, bytesWritten) => { |
61 | if (err) logger.warn(`ERROR: ${err.toString()}`); | 61 | if (err) logger.warn(`ERROR: ${err.toString()}`); |
62 | logger.verbose(`* OUT: ${data}`); | 62 | logger.verbose(`* OUT: ${data}`); |
63 | resolve(bytesWritten); | 63 | resolve(bytesWritten); |
64 | }); | 64 | }); |
65 | }); | 65 | }); |
66 | } | 66 | } |
67 | 67 | ||
68 | async function readSMS(slot) { | 68 | async function readSMS(slot) { |
69 | const parserCMGR = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 69 | const parserCMGR = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
70 | parserCMGR.on('data', (data) => { | 70 | parserCMGR.on('data', (data) => { |
71 | if (data) { | 71 | if (data) { |
72 | try { | 72 | try { |
73 | reportSender.incomingSMS(sms.extract(data.toString().trim()), modemInfo); | 73 | reportSender.incomingSMS(sms.extract(data.toString().trim()), modemInfo); |
74 | } catch (e) { | 74 | } catch (e) { |
75 | logger.warn(`Exception on reporting new message. ${e.toString()}`, { smsObj: e.smsObj, dataFromModem: data }); | 75 | logger.warn(`Exception on reporting new message. ${e.toString()}`, { smsObj: e.smsObj, dataFromModem: data }); |
76 | 76 | ||
77 | process.exit(0); | 77 | process.exit(0); |
78 | } | 78 | } |
79 | } | 79 | } |
80 | port.unpipe(parserCMGR); | 80 | port.unpipe(parserCMGR); |
81 | mutex.releaseLockWaitForCommand(); | 81 | mutex.releaseLockWaitForCommand(); |
82 | }); | 82 | }); |
83 | 83 | ||
84 | // const parserCMGD = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); | 84 | // const parserCMGD = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); |
85 | const parserCMGD = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 85 | const parserCMGD = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
86 | parserCMGD.on('data', () => { | 86 | parserCMGD.on('data', () => { |
87 | port.unpipe(parserCMGD); | 87 | port.unpipe(parserCMGD); |
88 | mutex.releaseLockWaitForCommand(); | 88 | mutex.releaseLockWaitForCommand(); |
89 | }); | 89 | }); |
90 | 90 | ||
91 | logger.info(`Reading SMS on slot ${slot}`); | 91 | logger.info(`Reading SMS on slot ${slot}`); |
92 | await mutex.setLockWaitForCommand(); | 92 | await mutex.setLockWaitForCommand(); |
93 | port.pipe(parserCMGR); | 93 | port.pipe(parserCMGR); |
94 | await writeToPort(`AT+CMGR=${slot}\r`); | 94 | await writeToPort(`AT+CMGR=${slot}\r`); |
95 | logger.info(`Finished reading SMS on slot ${slot}`); | 95 | logger.info(`Finished reading SMS on slot ${slot}`); |
96 | 96 | ||
97 | logger.info(`Deleting message on slot ${slot}`); | 97 | logger.info(`Deleting message on slot ${slot}`); |
98 | await mutex.setLockWaitForCommand(); | 98 | await mutex.setLockWaitForCommand(); |
99 | port.pipe(parserCMGD); | 99 | port.pipe(parserCMGD); |
100 | await writeToPort(`AT+CMGD=${slot}\r`); | 100 | await writeToPort(`AT+CMGD=${slot}\r`); |
101 | logger.info('Message processing has completed'); | 101 | logger.info('Message processing has completed'); |
102 | } | 102 | } |
103 | 103 | ||
104 | function onIncomingSMS(data) { | 104 | function onIncomingSMS(data) { |
105 | const value = common.extractValueFromReadLineData(data); | 105 | const value = common.extractValueFromReadLineData(data); |
106 | if (!value) return; | 106 | if (!value) return; |
107 | 107 | ||
108 | const chunks = value.split(','); | 108 | const chunks = value.split(','); |
109 | if (!chunks && !chunks[1]) return; | 109 | if (!chunks && !chunks[1]) return; |
110 | 110 | ||
111 | const slot = chunks[1]; | 111 | const slot = chunks[1]; |
112 | 112 | ||
113 | logger.info(`Incoming SMS on slot ${slot}`); | 113 | logger.info(`Incoming SMS on slot ${slot}`); |
114 | readSMS(slot); | 114 | readSMS(slot); |
115 | } | 115 | } |
116 | 116 | ||
117 | function onCOPS(data) { | 117 | function onCOPS(data) { |
118 | modemInfo.cops = common.extractValueFromReadLineData(data).trim(); | 118 | modemInfo.cops = common.extractValueFromReadLineData(data).trim(); |
119 | logger.info(`Connected Network: ${modemInfo.cops}`); | 119 | logger.info(`Connected Network: ${modemInfo.cops}`); |
120 | 120 | ||
121 | if (!modemInfo.cops) return; | 121 | if (!modemInfo.cops) return; |
122 | 122 | ||
123 | [, , modemInfo.networkId] = modemInfo.cops.split(','); | 123 | [, , modemInfo.networkId] = modemInfo.cops.split(','); |
124 | 124 | ||
125 | if (modemInfo.networkId) { | 125 | if (modemInfo.networkId) { |
126 | modemInfo.networkName = dbCops[modemInfo.networkId] || modemInfo.networkId; | 126 | modemInfo.networkName = dbCops[modemInfo.networkId] || modemInfo.networkId; |
127 | } | 127 | } |
128 | } | 128 | } |
129 | 129 | ||
130 | parserReadLine.on('data', (data) => { | 130 | parserReadLine.on('data', (data) => { |
131 | logger.verbose(`* IN: ${data}`); | 131 | logger.verbose(`* IN: ${data}`); |
132 | if (data) { | 132 | if (data) { |
133 | lastTs = new Date(); | 133 | lastTs = new Date(); |
134 | if (data.indexOf('+CSQ: ') === 0) { | 134 | if (data.indexOf('+CSQ: ') === 0) { |
135 | const signalStrength = common.extractValueFromReadLineData(data).trim(); | 135 | const signalStrength = common.extractValueFromReadLineData(data).trim(); |
136 | if (signalStrength) { | 136 | if (signalStrength) { |
137 | modemInfo.signalStrength = signalStrength; | 137 | modemInfo.signalStrength = signalStrength; |
138 | modemInfo.signalStrengthTs = new Date(); | 138 | modemInfo.signalStrengthTs = new Date(); |
139 | modemInfo.signalStrengthTsReadable = moment(modemInfo.signalStrengthTs).format('YYYY-MM-DD HH:mm:ss'); | 139 | modemInfo.signalStrengthTsReadable = moment(modemInfo.signalStrengthTs).format('YYYY-MM-DD HH:mm:ss'); |
140 | logger.info(`Signal strength: ${modemInfo.signalStrength}`); | 140 | logger.info(`Signal strength: ${modemInfo.signalStrength}`); |
141 | registerModem(modemInfo); | 141 | registerModem(modemInfo); |
142 | } | 142 | } |
143 | } else if (data.indexOf('+CMTI: ') === 0) { | 143 | } else if (data.indexOf('+CMTI: ') === 0) { |
144 | // counters.increment('MESSAGE_RECEIVED', modemInfo); | 144 | // counters.increment('MESSAGE_RECEIVED', modemInfo); |
145 | onIncomingSMS(data); | 145 | onIncomingSMS(data); |
146 | } else if (data.indexOf('+COPS: ') === 0) { | 146 | } else if (data.indexOf('+COPS: ') === 0) { |
147 | onCOPS(data); | 147 | onCOPS(data); |
148 | } | 148 | } |
149 | } | 149 | } |
150 | }); | 150 | }); |
151 | 151 | ||
152 | async function simpleSubCommand(cmd, callback) { | 152 | async function simpleSubCommand(cmd, callback) { |
153 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 153 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
154 | parser.on('data', (data) => { | 154 | parser.on('data', (data) => { |
155 | port.unpipe(parser); | 155 | port.unpipe(parser); |
156 | mutex.releaseLockWaitForSubCommand(); | 156 | mutex.releaseLockWaitForSubCommand(); |
157 | 157 | ||
158 | if (data) { | 158 | if (data) { |
159 | if (callback) callback(null, data.toString().trim()); | 159 | if (callback) callback(null, data.toString().trim()); |
160 | } | 160 | } |
161 | }); | 161 | }); |
162 | 162 | ||
163 | return new Promise(async (resolve) => { | 163 | return new Promise(async (resolve) => { |
164 | await mutex.setLockWaitForSubCommand(); | 164 | await mutex.setLockWaitForSubCommand(); |
165 | port.pipe(parser); | 165 | port.pipe(parser); |
166 | writeToPort(cmd); | 166 | writeToPort(cmd); |
167 | 167 | ||
168 | await mutex.setLockWaitForSubCommand(); | 168 | await mutex.setLockWaitForSubCommand(); |
169 | mutex.releaseLockWaitForSubCommand(); | 169 | mutex.releaseLockWaitForSubCommand(); |
170 | 170 | ||
171 | resolve(); | 171 | resolve(); |
172 | }); | 172 | }); |
173 | } | 173 | } |
174 | 174 | ||
175 | function readManufacturer() { | 175 | function readManufacturer() { |
176 | return new Promise((resolve) => { | 176 | return new Promise((resolve) => { |
177 | simpleSubCommand('AT+CGMI\r', (err, result) => { | 177 | simpleSubCommand('AT+CGMI\r', (err, result) => { |
178 | modemInfo.manufacturer = result; | 178 | modemInfo.manufacturer = result; |
179 | logger.info(`Manufacturer: ${result}`); | 179 | logger.info(`Manufacturer: ${result}`); |
180 | resolve(result); | 180 | resolve(result); |
181 | }); | 181 | }); |
182 | }); | 182 | }); |
183 | } | 183 | } |
184 | 184 | ||
185 | function readModel() { | 185 | function readModel() { |
186 | return new Promise((resolve) => { | 186 | return new Promise((resolve) => { |
187 | simpleSubCommand('AT+CGMM\r', (err, result) => { | 187 | simpleSubCommand('AT+CGMM\r', (err, result) => { |
188 | modemInfo.model = result; | 188 | modemInfo.model = result; |
189 | logger.info(`Model: ${result}`); | 189 | logger.info(`Model: ${result}`); |
190 | resolve(result); | 190 | resolve(result); |
191 | }); | 191 | }); |
192 | }); | 192 | }); |
193 | } | 193 | } |
194 | 194 | ||
195 | function readIMEI() { | 195 | function readIMEI() { |
196 | return new Promise((resolve) => { | 196 | return new Promise((resolve) => { |
197 | simpleSubCommand('AT+CGSN\r', (err, result) => { | 197 | simpleSubCommand('AT+CGSN\r', (err, result) => { |
198 | modemInfo.imei = result; | 198 | modemInfo.imei = result; |
199 | logger.info(`IMEI: ${result}`); | 199 | logger.info(`IMEI: ${result}`); |
200 | resolve(result); | 200 | resolve(result); |
201 | }); | 201 | }); |
202 | }); | 202 | }); |
203 | } | 203 | } |
204 | 204 | ||
205 | function readIMSI() { | 205 | function readIMSI() { |
206 | return new Promise((resolve) => { | 206 | return new Promise((resolve) => { |
207 | simpleSubCommand('AT+CIMI\r', (err, result) => { | 207 | simpleSubCommand('AT+CIMI\r', (err, result) => { |
208 | modemInfo.imsi = result; | 208 | modemInfo.imsi = result; |
209 | logger.info(`IMSI: ${result}`); | 209 | logger.info(`IMSI: ${result}`); |
210 | 210 | ||
211 | if (result) { | 211 | if (result) { |
212 | /* | 212 | /* |
213 | modemInfo.msisdn = msisdn[result]; | 213 | modemInfo.msisdn = msisdn[result]; |
214 | if (modemInfo.msisdn) { | 214 | if (modemInfo.msisdn) { |
215 | logger.info(`MSISDN: ${modemInfo.msisdn}`); | 215 | logger.info(`MSISDN: ${modemInfo.msisdn}`); |
216 | registerModem(modemInfo); | 216 | registerModem(modemInfo); |
217 | } | 217 | } |
218 | */ | 218 | */ |
219 | } else { | 219 | } else { |
220 | logger.warn(`IMSI not detected. Please insert a sim card to your modem. Terminating ${config.modem.device}.`); | 220 | logger.warn(`IMSI not detected. Please insert a sim card to your modem. Terminating ${config.modem.device}.`); |
221 | process.exit(2); | 221 | process.exit(2); |
222 | } | 222 | } |
223 | resolve(result); | 223 | resolve(result); |
224 | }); | 224 | }); |
225 | }); | 225 | }); |
226 | } | 226 | } |
227 | 227 | ||
228 | function readCOPS() { | 228 | function readCOPS() { |
229 | return new Promise((resolve) => { | 229 | return new Promise((resolve) => { |
230 | simpleSubCommand('AT+COPS?\r', (err, result) => { | 230 | simpleSubCommand('AT+COPS?\r', (err, result) => { |
231 | resolve(result); | 231 | resolve(result); |
232 | }); | 232 | }); |
233 | }); | 233 | }); |
234 | } | 234 | } |
235 | 235 | ||
236 | function deleteInbox() { | 236 | function deleteInbox() { |
237 | return new Promise((resolve) => { | 237 | return new Promise((resolve) => { |
238 | simpleSubCommand('AT+CMGD=0,4\r', (err, result) => { | 238 | simpleSubCommand('AT+CMGD=0,4\r', (err, result) => { |
239 | resolve(result); | 239 | resolve(result); |
240 | }); | 240 | }); |
241 | }); | 241 | }); |
242 | } | 242 | } |
243 | 243 | ||
244 | async function querySignalStrength() { | 244 | async function querySignalStrength() { |
245 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 245 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
246 | parser.on('data', () => { | 246 | parser.on('data', () => { |
247 | port.unpipe(parser); | 247 | port.unpipe(parser); |
248 | mutex.releaseLockWaitForCommand(); | 248 | mutex.releaseLockWaitForCommand(); |
249 | }); | 249 | }); |
250 | 250 | ||
251 | if (mutex.tryLockWaitForCommand()) { | 251 | if (mutex.tryLockWaitForCommand()) { |
252 | port.pipe(parser); | 252 | port.pipe(parser); |
253 | await writeToPort('AT+CSQ\r'); | 253 | await writeToPort('AT+CSQ\r'); |
254 | } | 254 | } |
255 | } | 255 | } |
256 | 256 | ||
257 | function registerModemToCenterPeriodically() { | 257 | function registerModemToCenterPeriodically() { |
258 | registerModem(modemInfo); | 258 | registerModem(modemInfo); |
259 | 259 | ||
260 | setInterval(() => { | 260 | setInterval(() => { |
261 | registerModem(modemInfo); | 261 | registerModem(modemInfo); |
262 | }, 60 * 1000); | 262 | }, 60 * 1000); |
263 | } | 263 | } |
264 | 264 | ||
265 | async function registerSignalStrengthBackgroundQuery() { | 265 | async function registerSignalStrengthBackgroundQuery() { |
266 | logger.info('Registering background signal strength query'); | 266 | logger.info('Registering background signal strength query'); |
267 | 267 | ||
268 | querySignalStrength(); | 268 | querySignalStrength(); |
269 | 269 | ||
270 | setInterval(() => { | 270 | setInterval(() => { |
271 | querySignalStrength(); | 271 | querySignalStrength(); |
272 | }, config.interval_beetwen_signal_strength_ms || INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS); | 272 | }, config.interval_beetwen_signal_strength_ms || INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS); |
273 | } | 273 | } |
274 | 274 | ||
275 | async function sendSMS(destination, msg) { | 275 | async function sendSMS(destination, msg) { |
276 | if (typeof destination !== 'string' || typeof msg !== 'string' || !destination.trim() || !msg.trim()) return; | 276 | if (typeof destination !== 'string' || typeof msg !== 'string' || !destination.trim() || !msg.trim()) return; |
277 | 277 | ||
278 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 278 | const parser = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); |
279 | parser.on('data', () => { | 279 | parser.on('data', () => { |
280 | port.unpipe(parser); | 280 | port.unpipe(parser); |
281 | mutex.releaseLockWaitForSubCommand(); | 281 | mutex.releaseLockWaitForSubCommand(); |
282 | }); | 282 | }); |
283 | 283 | ||
284 | logger.verbose('Waiting for command lock to send message'); | 284 | logger.verbose('Waiting for command lock to send message'); |
285 | await mutex.setLockWaitForCommand(); | 285 | await mutex.setLockWaitForCommand(); |
286 | 286 | ||
287 | logger.info('Sending message', { destination, msg }); | 287 | logger.info('Sending message', { destination, msg }); |
288 | // counters.increment('MESSAGE_SENT', modemInfo); | 288 | // counters.increment('MESSAGE_SENT', modemInfo); |
289 | 289 | ||
290 | const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+'); | 290 | const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+'); |
291 | 291 | ||
292 | logger.verbose('Waiting for lock before set to text mode'); | 292 | logger.verbose('Waiting for lock before set to text mode'); |
293 | await mutex.setLockWaitForSubCommand(); | 293 | await mutex.setLockWaitForSubCommand(); |
294 | port.pipe(parser); | 294 | port.pipe(parser); |
295 | await writeToPort('AT+CMGF=1\r'); | 295 | await writeToPort('AT+CMGF=1\r'); |
296 | 296 | ||
297 | logger.verbose('Waiting for lock before writing message'); | 297 | logger.verbose('Waiting for lock before writing message'); |
298 | await mutex.setLockWaitForSubCommand(); | 298 | await mutex.setLockWaitForSubCommand(); |
299 | port.pipe(parser); | 299 | port.pipe(parser); |
300 | await writeToPort(`AT+CMGS="${correctedDestination}"\r${msg}${Buffer.from([0x1A])}`); | 300 | await writeToPort(`AT+CMGS="${correctedDestination}"\r${msg}${Buffer.from([0x1A])}`); |
301 | 301 | ||
302 | await mutex.setLockWaitForSubCommand(); | 302 | await mutex.setLockWaitForSubCommand(); |
303 | mutex.releaseLockWaitForSubCommand(); | 303 | mutex.releaseLockWaitForSubCommand(); |
304 | 304 | ||
305 | logger.info('Message has been sent'); | 305 | logger.info('Message has been sent'); |
306 | 306 | ||
307 | setTimeout(() => { | 307 | setTimeout(() => { |
308 | logger.verbose('Releasing command lock'); | 308 | logger.verbose('Releasing command lock'); |
309 | mutex.releaseLockWaitForCommand(); | 309 | mutex.releaseLockWaitForCommand(); |
310 | }, config.wait_for_release_lock_wait_for_command_ms || 2000); | 310 | }, config.wait_for_release_lock_wait_for_command_ms || 2000); |
311 | } | 311 | } |
312 | /** | 312 | /** |
313 | * Ekseksusi kode USSD. | 313 | * Ekseksusi kode USSD. |
314 | * | 314 | * |
315 | * @param {string} code - Kode USSD | 315 | * @param {string} code - Kode USSD |
316 | */ | 316 | */ |
317 | function executeUSSD(code, includeCUSD2) { | 317 | function executeUSSD(code, includeCUSD2) { |
318 | return new Promise(async (resolve) => { | 318 | return new Promise(async (resolve) => { |
319 | const parserMain = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 319 | const parserMain = new ParserReadline({ delimiter: '\r\n' }); |
320 | // const parserMain = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | ||
320 | parserMain.on('data', (data) => { | 321 | parserMain.on('data', (data) => { |
321 | port.unpipe(parserMain); | 322 | port.unpipe(parserMain); |
322 | mutex.releaseLockWaitForSubCommand(); | 323 | mutex.releaseLockWaitForSubCommand(); |
323 | resolve(data); | 324 | resolve(data); |
324 | }); | 325 | }); |
325 | 326 | ||
326 | const parserCUSD2 = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | 327 | const parserCUSD2 = new ParserReadline({ delimiter: '\r\n' }); |
328 | // const parserCUSD2 = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); | ||
327 | parserCUSD2.on('data', () => { | 329 | parserCUSD2.on('data', () => { |
328 | port.unpipe(parserCUSD2); | 330 | port.unpipe(parserCUSD2); |
329 | mutex.releaseLockWaitForSubCommand(); | 331 | mutex.releaseLockWaitForSubCommand(); |
330 | }); | 332 | }); |
331 | 333 | ||
332 | logger.verbose('Waiting for command lock to execute USSD'); | 334 | logger.verbose('Waiting for command lock to execute USSD'); |
333 | await mutex.setLockWaitForCommand(); | 335 | await mutex.setLockWaitForCommand(); |
334 | 336 | ||
335 | if (includeCUSD2 === CUSD2_BEFORE) { | 337 | if (includeCUSD2 === CUSD2_BEFORE) { |
336 | logger.info('Terminating existing USSD session'); | 338 | logger.info('Terminating existing USSD session'); |
337 | await mutex.setLockWaitForSubCommand(); | 339 | await mutex.setLockWaitForSubCommand(); |
338 | port.pipe(parserCUSD2); | 340 | port.pipe(parserCUSD2); |
339 | await writeToPort('AT+CUSD=2\r'); | 341 | await writeToPort('AT+CUSD=2\r'); |
340 | } | 342 | } |
341 | 343 | ||
342 | await mutex.setLockWaitForSubCommand(); | 344 | await mutex.setLockWaitForSubCommand(); |
343 | port.pipe(parserMain); | 345 | port.pipe(parserMain); |
344 | await writeToPort(`AT+CUSD=1,"${code}",15\r`); | 346 | await writeToPort(`AT+CUSD=1,"${code}",15\r`); |
345 | 347 | ||
346 | if (includeCUSD2 === CUSD2_AFTER) { | 348 | if (includeCUSD2 === CUSD2_AFTER) { |
347 | logger.info('Terminating existing USSD session'); | 349 | logger.info('Terminating existing USSD session'); |
348 | await mutex.setLockWaitForSubCommand(); | 350 | await mutex.setLockWaitForSubCommand(); |
349 | port.pipe(parserCUSD2); | 351 | port.pipe(parserCUSD2); |
350 | await writeToPort('AT+CUSD=2\r'); | 352 | await writeToPort('AT+CUSD=2\r'); |
351 | } | 353 | } |
352 | 354 | ||
353 | await mutex.setLockWaitForSubCommand(); | 355 | await mutex.setLockWaitForSubCommand(); |
354 | mutex.releaseLockWaitForSubCommand(); | 356 | mutex.releaseLockWaitForSubCommand(); |
355 | 357 | ||
356 | mutex.releaseLockWaitForCommand(); | 358 | mutex.releaseLockWaitForCommand(); |
357 | }); | 359 | }); |
358 | } | 360 | } |
359 | 361 | ||
360 | function init() { | 362 | function init() { |
361 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => { | 363 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, (err) => { |
362 | if (err) { | 364 | if (err) { |
363 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | 365 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); |
364 | process.exit(1); | 366 | process.exit(1); |
365 | } | 367 | } |
366 | 368 | ||
367 | registerModem(modemInfo); | 369 | registerModem(modemInfo); |
368 | }); | 370 | }); |
369 | port.pipe(parserReadLine); | 371 | port.pipe(parserReadLine); |
370 | 372 | ||
371 | setInterval(() => { | 373 | setInterval(() => { |
372 | if ((new Date() - lastTs) > MAX_LAST_DATA_AGE_MS) { | 374 | if ((new Date() - lastTs) > MAX_LAST_DATA_AGE_MS) { |
373 | logger.warn(`No data for more than ${MAX_LAST_DATA_AGE_MS} ms. Modem might be unresponsive. Terminating modem ${config.modem.device}.`); | 375 | logger.warn(`No data for more than ${MAX_LAST_DATA_AGE_MS} ms. Modem might be unresponsive. Terminating modem ${config.modem.device}.`); |
374 | process.exit(0); | 376 | process.exit(0); |
375 | } | 377 | } |
376 | }, 30 * 1000); | 378 | }, 30 * 1000); |
377 | 379 | ||
378 | port.on('open', async () => { | 380 | port.on('open', async () => { |
379 | await mutex.setLockWaitForCommand(); | 381 | await mutex.setLockWaitForCommand(); |
380 | 382 | ||
381 | logger.info('Modem opened'); | 383 | logger.info('Modem opened'); |
382 | await writeToPort('\r'); | 384 | await writeToPort('\r'); |
383 | await simpleSubCommand('AT\r'); | 385 | await simpleSubCommand('AT\r'); |
384 | 386 | ||
385 | logger.info('Initializing modem to factory set'); | 387 | logger.info('Initializing modem to factory set'); |
386 | await simpleSubCommand('AT&F\r'); | 388 | await simpleSubCommand('AT&F\r'); |
387 | 389 | ||
388 | logger.info('Disabling echo'); | 390 | logger.info('Disabling echo'); |
389 | await simpleSubCommand('ATE0\r'); | 391 | await simpleSubCommand('ATE0\r'); |
390 | 392 | ||
391 | logger.info('Set to text mode'); | 393 | logger.info('Set to text mode'); |
392 | await simpleSubCommand('AT+CMGF=1\r'); | 394 | await simpleSubCommand('AT+CMGF=1\r'); |
393 | 395 | ||
394 | await readCOPS(); | 396 | await readCOPS(); |
395 | 397 | ||
396 | await readManufacturer(); | 398 | await readManufacturer(); |
397 | await readModel(); | 399 | await readModel(); |
398 | await readIMEI(); | 400 | await readIMEI(); |
399 | await readIMSI(); | 401 | await readIMSI(); |
400 | 402 | ||
401 | if (!config.disable_delete_inbox_on_startup) { | 403 | if (!config.disable_delete_inbox_on_startup) { |
402 | logger.info('Deleting existing messages'); | 404 | logger.info('Deleting existing messages'); |
403 | await deleteInbox(); | 405 | await deleteInbox(); |
404 | } | 406 | } |
405 | 407 | ||
406 | mutex.releaseLockWaitForCommand(); | 408 | mutex.releaseLockWaitForCommand(); |
407 | logger.verbose('Init completed'); | 409 | logger.verbose('Init completed'); |
408 | 410 | ||
409 | registerModemToCenterPeriodically(); | 411 | registerModemToCenterPeriodically(); |
410 | registerSignalStrengthBackgroundQuery(); | 412 | registerSignalStrengthBackgroundQuery(); |
411 | }); | 413 | }); |
412 | } | 414 | } |
413 | 415 | ||
414 | init(); | 416 | init(); |
415 | 417 | ||
416 | exports.CUSD2_BEFORE = CUSD2_BEFORE; | 418 | exports.CUSD2_BEFORE = CUSD2_BEFORE; |
417 | exports.CUSD2_AFTER = CUSD2_AFTER; | 419 | exports.CUSD2_AFTER = CUSD2_AFTER; |
418 | 420 | ||
419 | exports.modemInfo = modemInfo; | 421 | exports.modemInfo = modemInfo; |
420 | exports.sendSMS = sendSMS; | 422 | exports.sendSMS = sendSMS; |
421 | exports.executeUSSD = executeUSSD; | 423 | exports.executeUSSD = executeUSSD; |
422 | 424 |