Commit 5ae5434534092aea798b420f405b9130d2263818
1 parent
0440fda97c
Exists in
master
Report via HTTP
Showing 6 changed files with 126 additions and 5 deletions Side-by-side Diff
lib/modem.js
... | ... | @@ -13,6 +13,7 @@ const logger = require('komodo-sdk/logger'); |
13 | 13 | const mutex = require('./mutex'); |
14 | 14 | const common = require('./common'); |
15 | 15 | const sms = require('./sms'); |
16 | +const reportSender = require('./report-sender'); | |
16 | 17 | |
17 | 18 | const modemInfo = { |
18 | 19 | manufacturer: null, |
... | ... | @@ -51,7 +52,8 @@ async function readSMS(slot) { |
51 | 52 | parser.on('data', async (data) => { |
52 | 53 | if (data) { |
53 | 54 | const smsObject = sms.extract(data.toString().trim()); |
54 | - console.log('SMS', smsObject); // eslint-disable-line no-console | |
55 | + // console.log('SMS', smsObject); // eslint-disable-line no-console | |
56 | + reportSender.incomingSMS(smsObject); | |
55 | 57 | } |
56 | 58 | mutex.releaseLockWaitForOK(); |
57 | 59 | }); |
lib/report-sender.js
... | ... | @@ -0,0 +1,33 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const request = require('request'); | |
4 | + | |
5 | +const config = require('komodo-sdk/config'); | |
6 | +const logger = require('komodo-sdk/logger'); | |
7 | + | |
8 | +function incomingSMS(message) { | |
9 | + if (!config || !config.report_url || !config.report_url.incoming_sms) return; | |
10 | + | |
11 | + const requestOptions = { | |
12 | + url: config.report_url.incoming_sms, | |
13 | + qs: { | |
14 | + modem: config.name, | |
15 | + number: message.from, | |
16 | + msg: message.message, | |
17 | + }, | |
18 | + }; | |
19 | + | |
20 | + logger.info('Sending report via HTTP', requestOptions); | |
21 | + request(requestOptions, (err, res, body) => { | |
22 | + if (err) { | |
23 | + logger.warn(`Error sending report via HTTP. ${err.toString()}`); | |
24 | + return; | |
25 | + } | |
26 | + | |
27 | + if (res.statusCode !== 200) { | |
28 | + logger.warn(`Error sending report via HTTP. Server respond with HTTP status code ${res.statusCode}`, { http_status_code: res.statusCode, body }); | |
29 | + } | |
30 | + }); | |
31 | +} | |
32 | + | |
33 | +exports.incomingSMS = incomingSMS; |
lib/sms.js
... | ... | @@ -7,13 +7,15 @@ 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(',') || []; | |
10 | + const metadata = lines[0].split(',').map(el => el.replace(/"/g, '')) || []; | |
11 | 11 | |
12 | - const ts = moment(`20${metadata[3]} ${metadata[4]}`, 'YYYY/MM/DD HH:mmm:ss').format('YYYY-MM-DD HH:mm:ss'); | |
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'); | |
13 | 14 | |
14 | 15 | const result = { |
15 | 16 | metadata: { |
16 | - from: metadata[1], | |
17 | + status: metadata[0].replace(/^.+: /, ''), | |
18 | + from: metadata[1].replace(/^\+/, ''), | |
17 | 19 | ts, |
18 | 20 | raw: lines[0], |
19 | 21 | }, |
package-lock.json
... | ... | @@ -3934,6 +3934,60 @@ |
3934 | 3934 | "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", |
3935 | 3935 | "dev": true |
3936 | 3936 | }, |
3937 | + "should": { | |
3938 | + "version": "13.2.3", | |
3939 | + "resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz", | |
3940 | + "integrity": "sha512-ggLesLtu2xp+ZxI+ysJTmNjh2U0TsC+rQ/pfED9bUZZ4DKefP27D+7YJVVTvKsmjLpIi9jAa7itwDGkDDmt1GQ==", | |
3941 | + "dev": true, | |
3942 | + "requires": { | |
3943 | + "should-equal": "^2.0.0", | |
3944 | + "should-format": "^3.0.3", | |
3945 | + "should-type": "^1.4.0", | |
3946 | + "should-type-adaptors": "^1.0.1", | |
3947 | + "should-util": "^1.0.0" | |
3948 | + } | |
3949 | + }, | |
3950 | + "should-equal": { | |
3951 | + "version": "2.0.0", | |
3952 | + "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-2.0.0.tgz", | |
3953 | + "integrity": "sha512-ZP36TMrK9euEuWQYBig9W55WPC7uo37qzAEmbjHz4gfyuXrEUgF8cUvQVO+w+d3OMfPvSRQJ22lSm8MQJ43LTA==", | |
3954 | + "dev": true, | |
3955 | + "requires": { | |
3956 | + "should-type": "^1.4.0" | |
3957 | + } | |
3958 | + }, | |
3959 | + "should-format": { | |
3960 | + "version": "3.0.3", | |
3961 | + "resolved": "https://registry.npmjs.org/should-format/-/should-format-3.0.3.tgz", | |
3962 | + "integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=", | |
3963 | + "dev": true, | |
3964 | + "requires": { | |
3965 | + "should-type": "^1.3.0", | |
3966 | + "should-type-adaptors": "^1.0.1" | |
3967 | + } | |
3968 | + }, | |
3969 | + "should-type": { | |
3970 | + "version": "1.4.0", | |
3971 | + "resolved": "https://registry.npmjs.org/should-type/-/should-type-1.4.0.tgz", | |
3972 | + "integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM=", | |
3973 | + "dev": true | |
3974 | + }, | |
3975 | + "should-type-adaptors": { | |
3976 | + "version": "1.1.0", | |
3977 | + "resolved": "https://registry.npmjs.org/should-type-adaptors/-/should-type-adaptors-1.1.0.tgz", | |
3978 | + "integrity": "sha512-JA4hdoLnN+kebEp2Vs8eBe9g7uy0zbRo+RMcU0EsNy+R+k049Ki+N5tT5Jagst2g7EAja+euFuoXFCa8vIklfA==", | |
3979 | + "dev": true, | |
3980 | + "requires": { | |
3981 | + "should-type": "^1.3.0", | |
3982 | + "should-util": "^1.0.0" | |
3983 | + } | |
3984 | + }, | |
3985 | + "should-util": { | |
3986 | + "version": "1.0.1", | |
3987 | + "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.1.tgz", | |
3988 | + "integrity": "sha512-oXF8tfxx5cDk8r2kYqlkUJzZpDBqVY/II2WhvU0n9Y3XYvAYRmeaf1PvvIvTgPnv4KJ+ES5M0PyDq5Jp+Ygy2g==", | |
3989 | + "dev": true | |
3990 | + }, | |
3937 | 3991 | "signal-exit": { |
3938 | 3992 | "version": "3.0.2", |
3939 | 3993 | "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", |
package.json
... | ... | @@ -21,7 +21,8 @@ |
21 | 21 | "devDependencies": { |
22 | 22 | "eslint": "^5.16.0", |
23 | 23 | "eslint-config-airbnb-base": "^13.2.0", |
24 | - "eslint-plugin-import": "^2.18.2" | |
24 | + "eslint-plugin-import": "^2.18.2", | |
25 | + "should": "^13.2.3" | |
25 | 26 | }, |
26 | 27 | "dependencies": { |
27 | 28 | "@serialport/parser-delimiter": "^2.0.2", |
... | ... | @@ -29,6 +30,7 @@ |
29 | 30 | "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", |
30 | 31 | "locks": "^0.2.2", |
31 | 32 | "moment": "^2.24.0", |
33 | + "request": "^2.88.0", | |
32 | 34 | "serialport": "^7.1.5", |
33 | 35 | "serialport-gsm": "^3.2.0" |
34 | 36 | } |
test/sms.js
... | ... | @@ -0,0 +1,28 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +/* global describe it */ | |
4 | + | |
5 | +require('should'); | |
6 | + | |
7 | +const sms = require('../lib/sms'); | |
8 | + | |
9 | +describe('#sms', () => { | |
10 | + describe('#extract', () => { | |
11 | + const raw = '+CMGR: "REC UNREAD","+6282210008543",,"19/07/26,13:21:16+28"\r\nsudah bisa?'; | |
12 | + const messageParsed = sms.extract(raw); | |
13 | + | |
14 | + it('should return raw correctly', () => { | |
15 | + messageParsed.raw.should.equal(raw); | |
16 | + }); | |
17 | + | |
18 | + it('should return correct metadata', () => { | |
19 | + messageParsed.metadata.status.should.equal('REC UNREAD'); | |
20 | + messageParsed.metadata.from.should.equal('6282210008543'); | |
21 | + messageParsed.metadata.ts.should.equal('2019-07-26 13:21:16'); | |
22 | + }); | |
23 | + | |
24 | + it('should return correct message', () => { | |
25 | + messageParsed.message.should.equal('sudah bisa?'); | |
26 | + }); | |
27 | + }); | |
28 | +}); |