Commit edb8b6e81d4fd8e260a8011356dde99bb66a677c
1 parent
7355ad895c
Exists in
master
Penanganan error
Showing 3 changed files with 19 additions and 16 deletions Side-by-side Diff
lib/modem.js
... | ... | @@ -56,9 +56,7 @@ async function readSMS(slot) { |
56 | 56 | const parser = new ParserDelimiter({ delimiter: DELIMITER_WAIT_FOR_OK }); |
57 | 57 | parser.on('data', async (data) => { |
58 | 58 | if (data) { |
59 | - const smsObject = sms.extract(data.toString().trim()); | |
60 | - // console.log('SMS', smsObject); // eslint-disable-line no-console | |
61 | - reportSender.incomingSMS(smsObject); | |
59 | + reportSender.incomingSMS(sms.extract(data.toString().trim())); | |
62 | 60 | } |
63 | 61 | mutex.releaseLockWaitForOK(); |
64 | 62 | }); |
lib/report-sender.js
... | ... | @@ -12,9 +12,9 @@ function incomingSMS(message) { |
12 | 12 | url: config.report_url.incoming_sms, |
13 | 13 | qs: { |
14 | 14 | modem: config.name, |
15 | - ts: message.ts, | |
16 | - status: message.status, | |
17 | - number: message.from, | |
15 | + ts: message.metadata.ts, | |
16 | + status: message.metadata.status, | |
17 | + number: message.metadata.from, | |
18 | 18 | msg: message.message, |
19 | 19 | }, |
20 | 20 | }; |
lib/sms.js
... | ... | @@ -7,22 +7,27 @@ function extract(raw) { |
7 | 7 | const lines = raw.trim().split(/[\n\r]+/m); |
8 | 8 | if (!lines) return null; |
9 | 9 | |
10 | - const metadata = lines[0].split(',').map(el => el.replace(/"/g, '')) || []; | |
11 | - | |
12 | - const tsFromRaw = `${metadata[3]} ${metadata[4].replace(/\+\d+/, '')}`; | |
13 | - const ts = moment(tsFromRaw, 'YY/MM/DD HH:mm:ss').format('YYYY-MM-DD HH:mm:ss'); | |
14 | - | |
15 | 10 | const result = { |
16 | 11 | metadata: { |
17 | - status: metadata[0].replace(/^.+: /, ''), | |
18 | - from: metadata[1].replace(/^\+/, ''), | |
19 | - ts, | |
20 | - raw: lines[0], | |
12 | + status: null, | |
13 | + from: null, | |
14 | + ts: null, | |
15 | + raw: null, | |
21 | 16 | }, |
22 | - message: lines.slice(1).join('\n').trim(), | |
17 | + message: null, | |
23 | 18 | raw, |
24 | 19 | }; |
25 | 20 | |
21 | + const metadata = lines[0].split(',').map(el => el.replace(/"/g, '')) || []; | |
22 | + | |
23 | + [result.metadata.raw] = lines; | |
24 | + result.metadata.status = typeof metadata[0] === 'string' ? metadata[0].replace(/^.+: /, '') : ''; | |
25 | + result.metadata.from = typeof metadata[1] === 'string' ? metadata[1].replace(/^\+/, '') : ''; | |
26 | + | |
27 | + const tsFromRaw = `${metadata[3]} ${(metadata[4] || '').replace(/\+\d+/, '')}`; | |
28 | + result.metadata.ts = moment(tsFromRaw, 'YY/MM/DD HH:mm:ss').format('YYYY-MM-DD HH:mm:ss'); | |
29 | + | |
30 | + result.message = lines.slice(1).join('\n').trim(); | |
26 | 31 | return result; |
27 | 32 | } |
28 | 33 |