Commit 796f16005d713cc9576b908b6ae7ed9431b153a0

Authored by Nur Fajar Wibowo
1 parent 60d719e58a
Exists in master

sudah terima USSD command satu per satu

Showing 2 changed files with 106 additions and 28 deletions Side-by-side Diff

... ... @@ -5,7 +5,10 @@ const app = express();
5 5 const SerialPort = require("serialport");
6 6 const config = require('./config.json');
7 7 const request = require('request');
  8 +const Readline = require('@serialport/parser-readline');
  9 +const Delimiter = require('@serialport/parser-delimiter');
8 10  
  11 +var count = 1;
9 12 var statusModem = "0K";
10 13 var errorMessageModem= "";
11 14 var node_port = config.development.node_port;
... ... @@ -15,10 +18,11 @@ var api_key = config.development.api_key;
15 18  
16 19 //set serialport
17 20 const serialPort = new SerialPort(usb_port, {
18   - baudRate: 115200, dataBits: 8, autoOpen: true, lock: false,
19   - //parser: SerialPort.parsers.readline("\n")
  21 + baudRate: 115200
20 22 });
21 23  
  24 +const parser = serialPort.pipe(new Readline());
  25 +
22 26 serialPort.on("open", function () {
23 27 console.log('Serial communication open');
24 28 //
... ... @@ -37,7 +41,8 @@ serialPort.on('error', function(err) {
37 41 process.exit(1);
38 42 });
39 43  
40   -serialPort.on('data', function(data) {
  44 +
  45 +parser.on('data', function(data) {
41 46 console.log("incoming data: " + data);
42 47 var lastData = data.toString();
43 48 if (lastData.match(/\+CMTI:/)){
... ... @@ -46,25 +51,7 @@ serialPort.on('data', function(data) {
46 51 console.log("slot "+slot);
47 52 console.log("ada message baru");
48 53 readSmsAuto(serialPort, slot);
49   - } else if (lastData.match(/\+CMGR:/)){
50   - console.log("read sms!" + data);
51   - console.log("end read msg!");
52   - const lines = lastData.split(/\n/);
53   - console.log("lines1 "+lines[1]);
54   - console.log("lines2 "+lines[2]);
55   - const content = lines[1].split(',');
56   - console.log("content0 "+content[0]);
57   - console.log("content1 "+content[1]);
58   - console.log("content2 "+content[2]);
59   - console.log("content3 "+content[3]);
60   - console.log("content4 "+content[4]);
61   - var nomor = content[1].toString();
62   - nomor = nomor.replace(/"/g,'');
63   - nomor = nomor.replace(/\+/g,'');
64   - var pesan = lines[2].toString();
65   - //console.log("nomor nya "+nomor);
66   - postIncomingMessage(nomor, pesan);
67   - }
  54 + }
68 55 });
69 56  
70 57 //------------------function 2 000000000000 -----------------
... ... @@ -86,7 +73,7 @@ function postIncomingMessage(nomor, pesan){
86 73 console.log("----------------------------post incoming message-------------------------");
87 74 pesan = pesan.replace(/\s+$/, '');
88 75 //post to URL end Point
89   - request(url_post+"/gtwjs?msg="+pesan+"&number="+nomor, function (error, response, body) {
  76 + request(url_post+"?msg="+pesan+"&number="+nomor, function (error, response, body) {
90 77 if (error) { console.log("error post! "+nomor+" msg; \""+pesan+"\" errornya; "+error.message); }
91 78 else if (!error && response.statusCode == 200) {console.log("success post "+nomor+" msg; \""+pesan+"\" resp; "+body);}
92 79 });
... ... @@ -119,20 +106,109 @@ function sendSms (req, res, next){
119 106 //---------------
120 107 res.json({"status": statusModem+" "+errorMessageModem, "msg": msg, "number": number});
121 108 }
  109 +//send ussd
  110 +function sendUSSD (req, res, next){
  111 + var msg = req.query.msg;
  112 + var reqid = req.query.reqid;
  113 + var apikey = req.query.apikey;
  114 + //api_key="746573206D6F64656D2074656C6D6F73656C20666C617368";
  115 + if(!reqid||!apikey){
  116 + console.log("invalid reqid or apikey;msg "+msg);
  117 + res.json({"status": "invalid parameter", "msg": msg, "apikey" : apikey, "reqid": reqid});
  118 + return;
  119 + } else if (apikey !== api_key){
  120 + console.log("invalid api_key;msg \""+msg+"\"reqid \""+reqid);
  121 + res.json({"status": "invalid parameter", "msg": msg, "apikey" : apikey, "reqid": reqid});
  122 + return;
122 123  
123   -function readSmsAuto(serialPort, slot){
  124 + }
  125 + console.log("msg "+ msg+" - "+reqid);
  126 +
  127 + count++;
  128 + console.log("count "+ count);
  129 + if (count > 2){
  130 + console.log("modem busy!");
  131 + count--;
  132 + res.json({status: "reject", msg: "modem busy,try a few second"});
  133 + return;
  134 + }
124 135  
125   - serialPort.write("AT+CMGF=1");
  136 + //----------------
  137 + serialPort.write("AT+CUSD=1,\""+msg+"\"");
126 138 serialPort.write('\r');
  139 +
  140 + //parse response USSD
  141 + const parserUSSD = serialPort.pipe(new Delimiter({ delimiter: '\",0'}));
  142 + var respUssd = null;
  143 + parserUSSD.on('data',function(dataUSSD){
  144 + //console.log("incoming datad "+dataD);
  145 + var lastData = dataUSSD.toString();
  146 + console.log("incoming dataUSSD "+lastData);
  147 + respUssd = lastData;
  148 + serialPort.unpipe(parserUSSD);
  149 + });
  150 +
  151 + console.log('resp; status '+statusModem+" reqid "+reqid);
  152 + //---------------
  153 + let countDown = 8;// max witing response USSD 8 detik
  154 + var waitingRespUssd = setInterval (() => {
  155 + console.log(countDown+" second for reqid "+reqid);
  156 + countDown--;
  157 + if (respUssd || countDown < 2){
  158 + console.log("response USSD; "+respUssd);
  159 + count--;
  160 + res.json({
  161 + "response_ussd": respUssd,
  162 + "status": statusModem+" "+errorMessageModem,
  163 + "msg": msg,
  164 + "reqid": reqid
  165 + });
  166 + clearInterval(waitingRespUssd);
  167 + }
  168 + },1000);
  169 +}
  170 +
  171 +
  172 +
  173 +function readSmsAuto(serialPort, slot){
  174 +
127 175 serialPort.write("AT+CMGR="+slot);// read only slot 1
128 176 serialPort.write('\r');
  177 +
  178 + const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' }));
  179 + parserD.on('data',function(dataD){
  180 + //console.log("incoming datad "+dataD);
  181 + var lastData = dataD.toString();
  182 + console.log("incoming datad "+lastData);
  183 + if (lastData.match(/\+CMGR:/)){
  184 + console.log("incoming SMS "+lastData);
  185 + // try to parse dataD
  186 + const lines = lastData.split(/\n/);
  187 + console.log("lines1 "+lines[1]);
  188 + console.log("lines2 "+lines[2]);
  189 + const content = lines[1].split(',');
  190 + console.log("content0 "+content[0]);
  191 + console.log("content1 "+content[1]);
  192 + console.log("content2 "+content[2]);
  193 + console.log("content3 "+content[3]);
  194 + console.log("content4 "+content[4]);
  195 + var nomor = content[1].toString();
  196 + nomor = nomor.replace(/"/g,'');
  197 + nomor = nomor.replace(/\+/g,'');
  198 + var pesan = lines[2].toString();
  199 + console.log("nomor nya "+nomor);
  200 + serialPort.unpipe(parserD);
  201 + postIncomingMessage(nomor, pesan);
  202 + }
  203 + });
  204 +
129 205 console.log("pesan slot "+slot+" sudah di baca");
  206 +
130 207 setTimeout(function(){
131 208 serialPort.write("AT+CMGD="+slot);
132 209 serialPort.write("\r");
133 210 console.log("delete pesan slot "+slot+" done!");
134 211 }, 2000);
135   -
136 212 }
137 213  
138 214  
... ... @@ -158,11 +234,11 @@ function sendingSMS(serial, message, phone_no) {
158 234 console.log(message);
159 235 serial.write(Buffer([0x1A]));
160 236 serial.write('^z');
161   - //serial.close();
162 237 }
163 238  
164 239 app.get('/', helloWorld);
165 240 app.get('/sms', sendSms);
  241 +app.get('/ussd', sendUSSD);
166 242 app.get('/read', readSms);
167 243  
168 244 app.listen(node_port, () => console.log(`Example app listening on port ${node_port}!`));
... ... @@ -9,8 +9,10 @@
9 9 "author": "",
10 10 "license": "ISC",
11 11 "dependencies": {
  12 + "@serialport/parser-delimiter": "^2.0.2",
  13 + "@serialport/parser-readline": "^2.0.2",
12 14 "express": "^4.16.4",
13 15 "request": "^2.88.0",
14   - "serialport": "^4.0.7"
  16 + "serialport": "^6.2.2"
15 17 }
16 18 }