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