Blame view

lib/modem.js 6.11 KB
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
1
2
3
4
5
6
7
8
9
10
11
12
13
  'use strict';
  
  const INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS = 60000;
  const DELIMITER_WAIT_FOR_OK = '
  OK\r
  ';
  
  const SerialPort = require('serialport');
  const ParserReadline = require('@serialport/parser-readline');
  const ParserDelimiter = require('@serialport/parser-delimiter');
  
  const config = require('komodo-sdk/config');
  const logger = require('komodo-sdk/logger');
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
14

cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
15
  const mutex = require('./mutex');
67af4245e   Adhidarma Hadiwinoto   Refactor util men...
16
  const common = require('./common');
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
17
  const sms = require('./sms');
5ae543453   Adhidarma Hadiwinoto   Report via HTTP
18
  const reportSender = require('./report-sender');
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
19

bc541e414   Adhidarma Hadiwinoto   modemInfo
20
  const modemInfo = {
c428dcf05   Adhidarma Hadiwinoto   More information ...
21
22
      manufacturer: null,
      model: null,
3ec3e9eb3   Adhidarma Hadiwinoto   HTTP command serv...
23
      imei: null,
bc541e414   Adhidarma Hadiwinoto   modemInfo
24
25
      imsi: null,
      signalStrength: null,
0440fda97   Adhidarma Hadiwinoto   Include config on...
26
      config: config.modem,
bc541e414   Adhidarma Hadiwinoto   modemInfo
27
  };
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
28

49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  const port = new SerialPort(config.modem.device, { baudRate: 115200 });
  
  const parserReadLine = new ParserReadline();
  const parserWaitForOK = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
  port.pipe(parserReadLine);
  
  function writeToPort(data) {
      return new Promise((resolve) => {
          port.write(data, (err, bytesWritten) => {
              if (err) logger.warn(`ERROR: ${err.toString()}`);
              logger.verbose(`* OUT: ${data}`);
              resolve(bytesWritten);
          });
      });
  }
  
  async function writeToPortAndWaitForOK(data) {
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
46
      await mutex.setLockWaitForOK();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
47
      const result = await writeToPort(data);
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
48
49
      await mutex.setLockWaitForOK();
      mutex.releaseLockWaitForOK();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
50
51
52
53
54
55
56
57
      return result;
  }
  
  async function readSMS(slot) {
      const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
      parser.on('data', async (data) => {
          if (data) {
              const smsObject = sms.extract(data.toString().trim());
5ae543453   Adhidarma Hadiwinoto   Report via HTTP
58
59
              // console.log('SMS', smsObject); // eslint-disable-line no-console
              reportSender.incomingSMS(smsObject);
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
60
          }
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
61
          mutex.releaseLockWaitForOK();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      });
  
      logger.info(`Reading SMS on slot ${slot}`);
      port.pipe(parser);
      await writeToPortAndWaitForOK(`AT+CMGR=${slot}\r`);
      port.unpipe(parser);
      logger.verbose(`Finished reading SMS on slot ${slot}`);
  
      logger.info(`Deleting message on slot ${slot}`);
      port.pipe(parserWaitForOK);
      await writeToPortAndWaitForOK(`AT+CMGD=${slot}\r`);
      port.unpipe(parserWaitForOK);
  
      logger.info('Message processing has completed');
  }
  
  function onIncomingSMS(data) {
      const value = common.extractValueFromReadLineData(data);
      if (!value) return;
  
      const chunks = value.split(',');
      if (!chunks && !chunks[1]) return;
  
      const slot = chunks[1];
  
      logger.info(`Incoming SMS on slot ${slot}`);
      readSMS(slot);
  }
  
  parserReadLine.on('data', (data) => {
      logger.verbose(`* IN: ${data}`);
      if (data) {
          if (data.indexOf('+CSQ: ') === 0) {
3ec3e9eb3   Adhidarma Hadiwinoto   HTTP command serv...
95
              modemInfo.signalStrength = common.extractValueFromReadLineData(data).trim();
bc541e414   Adhidarma Hadiwinoto   modemInfo
96
              logger.info(`Signal strength: ${modemInfo.signalStrength}`);
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
97
98
99
100
101
102
103
          } else if (data.indexOf('+CMTI: ') === 0) {
              onIncomingSMS(data);
          }
      }
  });
  
  parserWaitForOK.on('data', () => {
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
104
      mutex.releaseLockWaitForOK();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
105
  });
c428dcf05   Adhidarma Hadiwinoto   More information ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  function simpleCommand(cmd, callback) {
      const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK });
      parser.on('data', (data) => {
          port.unpipe(parser);
          mutex.releaseLockWaitForOK();
  
          if (data) {
              callback(null, data.toString().trim());
          }
      });
  
      port.pipe(parser);
      writeToPortAndWaitForOK(cmd);
  }
c428dcf05   Adhidarma Hadiwinoto   More information ...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  function readManufacturer() {
      return new Promise((resolve) => {
          simpleCommand('AT+CGMI\r', (err, result) => {
              modemInfo.manufacturer = result;
              logger.info(`Manufacturer: ${result}`);
              resolve(result);
          });
      });
  }
  
  function readModel() {
      return new Promise((resolve) => {
          simpleCommand('AT+CGMM\r', (err, result) => {
              modemInfo.model = result;
              logger.info(`Model: ${result}`);
              resolve(result);
          });
      });
  }
  
  function readIMEI() {
      return new Promise((resolve) => {
          simpleCommand('AT+CGSN\r', (err, result) => {
              modemInfo.imei = result;
              logger.info(`IMEI: ${result}`);
              resolve(result);
          });
      });
  }
  
  function readIMSI() {
      return new Promise((resolve) => {
          simpleCommand('AT+CIMI\r', (err, result) => {
              modemInfo.imsi = result;
              logger.info(`IMSI: ${result}`);
              resolve(result);
          });
      });
  }
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  
  async function querySignalStrength() {
      port.pipe(parserWaitForOK);
      await writeToPortAndWaitForOK('AT+CSQ\r');
      port.unpipe(parserWaitForOK);
  }
  
  async function registerSignalStrengthBackgroundQuery() {
      logger.info('Registering background signal strength query');
  
      setInterval(() => {
          querySignalStrength();
      }, INTERVAL_BEETWEN_SIGNAL_STRENGTH_MS);
  }
  
  async function sendSMS(destination, msg) {
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
175
      await mutex.setLockWaitForCommand();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
176
177
178
179
180
181
182
183
184
185
186
      logger.info('Sending message', { destination, msg });
  
      const correctedDestination = `+${destination}`.replace(/^0/, '62').replace(/^\++/, '+');
  
      port.pipe(parserWaitForOK);
      await writeToPortAndWaitForOK('AT+CMGF=1\r');
      await writeToPortAndWaitForOK(`AT+CMGS="${correctedDestination}"
  ${msg}${Buffer.from([0x1A])}`);
      port.unpipe(parserWaitForOK);
  
      logger.info('Message has been sent');
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
187
      mutex.releaseLockWaitForCommand();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  }
  
  function init() {
      port.on('open', async () => {
          port.pipe(parserWaitForOK);
  
          logger.info('Modem opened');
          await writeToPortAndWaitForOK('AT\r');
  
          logger.info('Initializing modem to factory set');
          await writeToPortAndWaitForOK('AT&F\r');
  
          logger.info('Disabling echo');
          await writeToPortAndWaitForOK('ATE0\r');
  
          logger.info('Querying signal strength');
          await writeToPortAndWaitForOK('AT+CSQ\r');
  
          logger.info('Deleting existing messages');
          // await writeToPortAndWaitForOK('AT+CMGD=0,4\r');
c428dcf05   Adhidarma Hadiwinoto   More information ...
208
209
210
          await readManufacturer();
          await readModel();
          await readIMEI();
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
211
212
213
214
215
216
217
218
219
220
          await readIMSI();
  
          port.unpipe(parserWaitForOK);
  
          registerSignalStrengthBackgroundQuery();
          logger.verbose('Init completed');
      });
  }
  
  init();
bc541e414   Adhidarma Hadiwinoto   modemInfo
221
  exports.modemInfo = modemInfo;
49eaf19a3   Adhidarma Hadiwinoto   SMS is ok
222
  exports.sendSMS = sendSMS;