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