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