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