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