Commit d5e7b729a02e20c39d78ce34009ec644989d45a7
1 parent
398f34e884
Exists in
master
commiting modem serialport single port
Showing 7 changed files with 308 additions and 0 deletions Side-by-side Diff
lib/common.js
lib/config.json
lib/modem.js
... | ... | @@ -0,0 +1,181 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const SerialPort = require("serialport"); | |
4 | +const Readline = require('@serialport/parser-readline'); | |
5 | +const Delimiter = require('@serialport/parser-delimiter'); | |
6 | +const config = require('./config.json'); | |
7 | +const request = require('request'); | |
8 | +const common = require('./common'); | |
9 | +const qs = require('querystring'); | |
10 | + | |
11 | +//let portUse = process.argv[2]; | |
12 | +//let modemName = process.argv[4]; | |
13 | + | |
14 | +//common.log("port use man !! "+portUse+" "+modemName); | |
15 | + | |
16 | +const serialPort = new SerialPort(config.core.usb_port, { | |
17 | + baudRate: 115200 | |
18 | +}); | |
19 | +const parser = serialPort.pipe(new Readline()); | |
20 | + | |
21 | +exports.startModem = async function(){ | |
22 | + let count = 1; | |
23 | + let statusModem = "0K"; | |
24 | + let errorMessageModem= ""; | |
25 | + | |
26 | + | |
27 | + serialPort.on("open", function () { | |
28 | + common.log('Serial communication open'); | |
29 | + // | |
30 | + serialPort.write("AT+CNMI?"); | |
31 | + serialPort.write("\r"); | |
32 | + //serialPort.write("AT+CNMI=1,1,2,1,1"); | |
33 | + //serialPort.write("\r"); | |
34 | + common.log('receive sms ON'); | |
35 | + }); | |
36 | + | |
37 | + serialPort.on('error', function(err) { | |
38 | + statusModem = "NOT OK"; | |
39 | + errorMessageModem = err.message; | |
40 | + common.log('Error!: ', err.message); | |
41 | + process.exit(1); | |
42 | + }); | |
43 | + | |
44 | + parser.on('data', function(data) { | |
45 | + common.log("incoming data: " + data); | |
46 | + let lastData = data.toString(); | |
47 | + if (lastData.match(/\+CMTI:/)){ | |
48 | + let numberPattern = /(\d+)/g; | |
49 | + let slot = lastData.match(numberPattern); | |
50 | + common.log("slot "+slot); | |
51 | + common.log("ada message baru"); | |
52 | + readSmsAuto(serialPort, slot); | |
53 | + } | |
54 | + }); | |
55 | +} | |
56 | + | |
57 | +async function readSmsAuto(serialPort, slot){ | |
58 | + | |
59 | + serialPort.write("AT+CMGR="+slot);// read only slot 1 | |
60 | + serialPort.write('\r'); | |
61 | + | |
62 | + const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' })); | |
63 | + parserD.on('data',function(dataD){ | |
64 | + //common.log("incoming datad "+dataD); | |
65 | + let lastData = dataD.toString(); | |
66 | + common.log("incoming datad "+lastData); | |
67 | + if (lastData.match(/\+CMGR:/)){ | |
68 | + common.log("incoming SMS "+lastData); | |
69 | + // try to parse dataD | |
70 | + const lines = lastData.split(/\n/); | |
71 | + common.log("lines1 "+lines[1]); | |
72 | + common.log("lines2 "+lines[2]); | |
73 | + const content = lines[1].split(','); | |
74 | + common.log("content0 "+content[0]); | |
75 | + common.log("content1 "+content[1]); | |
76 | + common.log("content2 "+content[2]); | |
77 | + common.log("content3 "+content[3]); | |
78 | + common.log("content4 "+content[4]); | |
79 | + let nomor = content[1].toString(); | |
80 | + nomor = nomor.replace(/"/g,''); | |
81 | + nomor = nomor.replace(/\+/g,''); | |
82 | + let pesan = lines[2].toString(); | |
83 | + common.log("nomor nya "+nomor); | |
84 | + serialPort.unpipe(parserD); | |
85 | + postIncomingMessage(nomor, pesan); | |
86 | + } | |
87 | + }); | |
88 | + | |
89 | + common.log("pesan slot "+slot+" sudah di baca"); | |
90 | + | |
91 | + setTimeout(function(){ | |
92 | + serialPort.write("AT+CMGD="+slot); | |
93 | + serialPort.write("\r"); | |
94 | + common.log("delete pesan slot "+slot+" done!"); | |
95 | + }, 2000); | |
96 | +} | |
97 | + | |
98 | +function postIncomingMessage(nomor, pesan){ | |
99 | + common.log("----------------------------post incoming message-------------------------"); | |
100 | + pesan = pesan.replace(/\s+$/, ''); | |
101 | + //let encodePesan = encodeURIComponent(pesan); | |
102 | + let inboxUrl = config.core.url_post+"/inbox?";//msg="+encodePesan+"&number="+nomor; | |
103 | + common.log("url 2211; "+inboxUrl); | |
104 | + let tryOptions = { | |
105 | + url: inboxUrl, | |
106 | + qs: { | |
107 | + msg: pesan, | |
108 | + number: nomor, | |
109 | + modem: modemName | |
110 | + } | |
111 | + } | |
112 | + | |
113 | + //post to URL end Point | |
114 | + request(tryOptions, function (error, response, body) { | |
115 | + if (error) { | |
116 | + common.log("error post! "+nomor+" msg; \""+pesan+"\" errornya; "+error.message); | |
117 | + } else if (!error && response.statusCode == 200) { | |
118 | + common.log("success post "+nomor+" msg; \""+pesan+"\" resp; "+body); | |
119 | + } | |
120 | + }); | |
121 | +} | |
122 | + | |
123 | +exports.sendingSMS = function (message, phone_no) { | |
124 | + | |
125 | + serialPort.write("AT+CMGF=1"); | |
126 | + serialPort.write('\r'); | |
127 | + | |
128 | + common.log('number '+phone_no); | |
129 | + serialPort.write("AT+CMGS=\"" + phone_no + "\""); | |
130 | + serialPort.write('\r'); | |
131 | + serialPort.write(message); | |
132 | + common.log(message); | |
133 | + serialPort.write(Buffer([0x1A])); | |
134 | + serialPort.write('^z'); | |
135 | +} | |
136 | + | |
137 | +exports.sendingUSSD = function (msg, count){ | |
138 | + | |
139 | + return new Promise(resolve => { | |
140 | + let respUssd = null; | |
141 | + let new_respUssd; | |
142 | + serialPort.write("AT+CUSD=1,\""+msg+"\""); | |
143 | + serialPort.write('\r'); | |
144 | + | |
145 | + const parserUSSD = serialPort.pipe(new Delimiter({ delimiter: '\",0'})); | |
146 | + parserUSSD.on('data',function(dataUSSD){ | |
147 | + let lastData = dataUSSD.toString(); | |
148 | + common.log("incoming dataUSSD "+lastData); | |
149 | + respUssd = lastData; | |
150 | + serialPort.unpipe(parserUSSD); | |
151 | + }); | |
152 | + | |
153 | + let countDown = 10; | |
154 | + let waiting = setInterval(() => { | |
155 | + countDown--; | |
156 | + console.log("wait respUssd "+countDown); | |
157 | + //if (respUssd || countDown < 2 ){ | |
158 | + if (respUssd){ | |
159 | + new_respUssd = respUssd.replace(/^[^ ]+\+CUSD:/, '\+CUSD:'); | |
160 | + //to close command if resp CUSD: 1 | |
161 | + if (new_respUssd.match(/^\+CUSD: 1,/)){ | |
162 | + serialPort.write("AT+CUSD=2"); | |
163 | + serialPort.write('\r'); | |
164 | + } | |
165 | + resolve(new_respUssd); | |
166 | + clearInterval(waiting); | |
167 | + } else if (!respUssd && countDown < 2){ | |
168 | + new_respUssd = "+CUSD: 4"; | |
169 | + resolve(new_respUssd); | |
170 | + clearInterval(waiting); | |
171 | + } | |
172 | + },1000); | |
173 | + }); | |
174 | +} | |
175 | + | |
176 | +function deleteMessage(serialPort, slot){ | |
177 | + serialPort.write("AT+CMGD="+slot); | |
178 | + serialPort.write("\r"); | |
179 | + common.log("delete pesan slot "+slot+" done!"); | |
180 | +} | |
181 | + |
lib/sms.js
... | ... | @@ -0,0 +1,31 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const config = require('./config.json'); | |
4 | +const modem = require('./modem'); | |
5 | +const common = require('./common'); | |
6 | + | |
7 | +exports.sendSms = async function (req, res){ | |
8 | + let msg = req.query.msg; | |
9 | + let number = req.query.number; | |
10 | + let reqid = req.query.reqid; | |
11 | + let apiKey = req.query.apikey; | |
12 | + //apiKey="746573206D6F64656D2074656C6D6F73656C20666C617368"; | |
13 | + if(!reqid||!apiKey){ | |
14 | + common.log("invalid reqid or apikey;msg "+msg+" number "+number); | |
15 | + res.json({"status": "invalid parameter", "msg": msg, "number": number, "apikey" : apiKey, "reqid": reqid}); | |
16 | + return; | |
17 | + } else if (apiKey !== config.core.api_key){ | |
18 | + common.log("invalid apiKey;msg \""+msg+"\" number \""+number+"\" reqid \""+reqid); | |
19 | + res.json({"status": "invalid parameter", "msg": msg, "number": number, "apikey" : apiKey, "reqid": reqid}); | |
20 | + return; | |
21 | + } | |
22 | + common.log("msg "+ msg + " number " +number); | |
23 | + //---------------- | |
24 | + let val = await modem.sendingSMS(msg, "+"+number, reqid); | |
25 | + //common.log('resp; status '+statusModem+" reqid "+reqid); | |
26 | + //--------------- | |
27 | + common.log("val "+val); | |
28 | + res.json({"msg": msg, "number": number, code: "200"}); | |
29 | + | |
30 | +} | |
31 | + |
lib/ussd.js
... | ... | @@ -0,0 +1,41 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const config = require('./config.json'); | |
4 | +const modem = require('./modem'); | |
5 | +const common = require('./common'); | |
6 | + | |
7 | +let count = 1; | |
8 | + | |
9 | +exports.sendUssd = async function sendUssd (req, res){ | |
10 | + let msg = req.query.msg; | |
11 | + let reqid = req.query.reqid; | |
12 | + let apiKey = req.query.apikey; | |
13 | + //apiKey="746573206D6F64656D2074656C6D6F73656C20666C617368"; | |
14 | + if(!reqid||!apiKey){ | |
15 | + common.log("invalid reqid or apikey;msg "+msg); | |
16 | + res.json({"status": "invalid parameter", "msg": msg, "apikey" : apikey, "reqid": reqid}); | |
17 | + return; | |
18 | + } else if (apiKey !== config.core.api_key){ | |
19 | + common.log("invalid apiKey;msg \""+msg+"\"reqid \""+reqid); | |
20 | + res.json({"status": "invalid parameter", "msg": msg, "apikey" : apikey, "reqid": reqid}); | |
21 | + return; | |
22 | + } | |
23 | + common.log("msg "+ msg+" - "+reqid); | |
24 | + | |
25 | + count++; | |
26 | + common.log("count "+ count); | |
27 | + if (count > 2){ | |
28 | + common.log("modem busy!"); | |
29 | + count--; | |
30 | + res.json({status: "reject", info: "modem busy,try a few second", msg: msg}); | |
31 | + return; | |
32 | + } | |
33 | + | |
34 | + //---------------- | |
35 | + let newData = await modem.sendingUSSD(msg, count); | |
36 | + common.log("data diterima "+newData); | |
37 | + count--; | |
38 | + res.json({data : newData, code: "200"}); | |
39 | + | |
40 | +} | |
41 | + |
package.json
... | ... | @@ -0,0 +1,19 @@ |
1 | +{ | |
2 | + "name": "modemX", | |
3 | + "version": "1.0.0", | |
4 | + "description": "", | |
5 | + "main": "index.js", | |
6 | + "scripts": { | |
7 | + "test": "echo \"Error: no test specified\" && exit 1" | |
8 | + }, | |
9 | + "author": "", | |
10 | + "license": "ISC", | |
11 | + "dependencies": { | |
12 | + "@serialport/parser-delimiter": "^2.0.2", | |
13 | + "@serialport/parser-readline": "^2.0.2", | |
14 | + "date-and-time": "^0.7.0", | |
15 | + "express": "^4.16.4", | |
16 | + "request": "^2.88.0", | |
17 | + "serialport": "^6.2.2" | |
18 | + } | |
19 | +} |
start.js
... | ... | @@ -0,0 +1,19 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const express = require('express'); | |
4 | +const app = express(); | |
5 | +const config = require('./lib/config.json'); | |
6 | +const modem = require('./lib/modem'); | |
7 | +const sms = require('./lib/sms'); | |
8 | +const ussd = require('./lib/ussd'); | |
9 | +const argv = process.argv; | |
10 | + | |
11 | +//let portUse = argv[2]; | |
12 | + | |
13 | +app.get('/sms', sms.sendSms); | |
14 | +app.get('/ussd', ussd.sendUssd); | |
15 | + | |
16 | +//start port modem | |
17 | +modem.startModem(); | |
18 | + | |
19 | +app.listen(config.core.node_port, () => console.log(`app listen on port ${config.core.node_port}!`)); |