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