Commit 9ac900289851341fcc61b47240167a743382da1d

Authored by Adhidarma Hadiwinoto
Exists in master

Merge branch 'master' of http://gitlab.kodesumber.com/bowo/modem-sds

Showing 2 changed files Inline Diff

lib/config.sample.json
1 { 1 {
2 "core": { 2 "core": {
3 "usb_port": "/dev/ttyUSB0", 3 "usb_port": "/dev/ttyUSB0",
4 "modem_name": "CHANGE ME", 4 "modem_name": "CHANGE ME",
5 "node_port": "2110", 5 "node_port": "2110",
6 "url_post": "http://localhost:2211", 6 "url_post": "http://localhost:2211/inbox",
7 "api_key": "746573206D6F64656D2074656C6D6F73656C20666C617368" 7 "api_key": "746573206D6F64656D2074656C6D6F73656C20666C617368"
8 } 8 }
9 } 9 }
10 10
1 'use strict'; 1 'use strict';
2 2
3 const SerialPort = require("serialport"); 3 const SerialPort = require("serialport");
4 const Readline = require('@serialport/parser-readline'); 4 const Readline = require('@serialport/parser-readline');
5 const Delimiter = require('@serialport/parser-delimiter'); 5 const Delimiter = require('@serialport/parser-delimiter');
6 const config = require('./config.json'); 6 const config = require('./config.json');
7 const request = require('request'); 7 const request = require('request');
8 const common = require('./common'); 8 const common = require('./common');
9 const qs = require('querystring'); 9 const qs = require('querystring');
10 const locks = require('locks'); 10 const locks = require('locks');
11 11
12 var mutex = locks.createMutex(); 12 var mutex = locks.createMutex();
13 13
14 //let portUse = process.argv[2]; 14 //let portUse = process.argv[2];
15 //let modemName = process.argv[4]; 15 //let modemName = process.argv[4];
16 16
17 //common.log("port use man !! "+portUse+" "+modemName); 17 //common.log("port use man !! "+portUse+" "+modemName);
18 let modemName = config.core.modem_name; 18 let modemName = config.core.modem_name;
19 19
20 const serialPort = new SerialPort(config.core.usb_port, { 20 const serialPort = new SerialPort(config.core.usb_port, {
21 baudRate: 115200 21 baudRate: 115200
22 }); 22 });
23 const parser = serialPort.pipe(new Readline()); 23 const parser = serialPort.pipe(new Readline());
24 24
25 function serialPortWrite(data) { 25 function serialPortWrite(data) {
26 return new Promise(function(resolve, reject) { 26 return new Promise(function(resolve, reject) {
27 serialPort.write(data, function() { 27 serialPort.write(data, function() {
28 resolve(); 28 resolve();
29 }) 29 })
30 }) 30 })
31 } 31 }
32 32
33 function setLock() { 33 function setLock() {
34 return new Promise(function(resolve) { 34 return new Promise(function(resolve) {
35 mutex.lock(function() { 35 mutex.lock(function() {
36 resolve(true); 36 resolve(true);
37 }) 37 })
38 }) 38 })
39 } 39 }
40 40
41 function releaseLock() { 41 function releaseLock() {
42 mutex.unlock(); 42 mutex.unlock();
43 } 43 }
44 44
45 exports.startModem = async function(){ 45 exports.startModem = async function(){
46 let count = 1; 46 let count = 1;
47 let statusModem = "0K"; 47 let statusModem = "0K";
48 let errorMessageModem= ""; 48 let errorMessageModem= "";
49 49
50 50
51 serialPort.on("open", async function () { 51 serialPort.on("open", async function () {
52 //const serialPortWrite = util.promisify(serialPort.write); 52 //const serialPortWrite = util.promisify(serialPort.write);
53 53
54 common.log('Serial communication open'); 54 common.log('Serial communication open');
55 // 55 //
56 await serialPortWrite("ATE0\r"); 56 await serialPortWrite("ATE0\r");
57 await serialPortWrite("AT+CNMI?\r"); 57 await serialPortWrite("AT+CNMI?\r");
58 //await serialPortWrite("AT+CNMI=1,1,2,1,1"); 58 //await serialPortWrite("AT+CNMI=1,1,2,1,1");
59 //await serialPortWrite("\r"); 59 //await serialPortWrite("\r");
60 await serialPortWrite('AT+CUSD=2\r'); 60 await serialPortWrite('AT+CUSD=2\r');
61 await serialPortWrite('AT+CUSD=1,"*888*4#",15\r'); 61 await serialPortWrite('AT+CUSD=1,"*888*4#",15\r');
62 common.log('receive sms ON'); 62 common.log('receive sms ON');
63 }); 63 });
64 64
65 serialPort.on('error', function(err) { 65 serialPort.on('error', function(err) {
66 statusModem = "NOT OK"; 66 statusModem = "NOT OK";
67 errorMessageModem = err.message; 67 errorMessageModem = err.message;
68 common.log('Error!: ', err.message); 68 common.log('Error!: ', err.message);
69 process.exit(1); 69 process.exit(1);
70 }); 70 });
71 71
72 parser.on('data', function(data) { 72 parser.on('data', function(data) {
73 common.log("incoming data: " + data); 73 common.log("incoming data: " + data);
74 let lastData = data.toString(); 74 let lastData = data.toString();
75 if (lastData.match(/\+CMTI:/)){ 75 if (lastData.match(/\+CMTI:/)){
76 let numberPattern = /(\d+)/g; 76 let numberPattern = /(\d+)/g;
77 let slot = lastData.match(numberPattern); 77 let slot = lastData.match(numberPattern);
78 common.log("slot "+slot); 78 common.log("slot "+slot);
79 common.log("ada message baru"); 79 common.log("ada message baru");
80 readSmsAuto(serialPort, slot); 80 readSmsAuto(serialPort, slot);
81 } 81 }
82 }); 82 });
83 } 83 }
84 84
85 async function readSmsAuto(serialPort, slot){ 85 async function readSmsAuto(serialPort, slot){
86 86
87 await serialPortWrite("AT+CMGR="+slot);// read only slot 1 87 await serialPortWrite("AT+CMGR="+slot);// read only slot 1
88 await serialPortWrite('\r'); 88 await serialPortWrite('\r');
89 89
90 const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' })); 90 const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' }));
91 parserD.on('data',function(dataD){ 91 parserD.on('data',function(dataD){
92 //common.log("incoming datad "+dataD); 92 //common.log("incoming datad "+dataD);
93 let lastData = dataD.toString(); 93 let lastData = dataD.toString();
94 common.log("incoming datad "+lastData); 94 common.log("incoming datad "+lastData);
95 if (lastData.match(/\+CMGR:/)){ 95 if (lastData.match(/\+CMGR:/)){
96 common.log("incoming SMS "+lastData); 96 common.log("incoming SMS "+lastData);
97 // try to parse dataD 97 // try to parse dataD
98 const lines = lastData.split(/\n/); 98 const lines = lastData.split(/\n/);
99 common.log("lines1 "+lines[1]); 99 common.log("lines1 "+lines[1]);
100 common.log("lines2 "+lines[2]); 100 common.log("lines2 "+lines[2]);
101 const content = lines[1].split(','); 101 const content = lines[1].split(',');
102 common.log("content0 "+content[0]); 102 common.log("content0 "+content[0]);
103 common.log("content1 "+content[1]); 103 common.log("content1 "+content[1]);
104 common.log("content2 "+content[2]); 104 common.log("content2 "+content[2]);
105 common.log("content3 "+content[3]); 105 common.log("content3 "+content[3]);
106 common.log("content4 "+content[4]); 106 common.log("content4 "+content[4]);
107 let nomor = content[1].toString(); 107 let nomor = content[1].toString();
108 nomor = nomor.replace(/"/g,''); 108 nomor = nomor.replace(/"/g,'');
109 nomor = nomor.replace(/\+/g,''); 109 nomor = nomor.replace(/\+/g,'');
110 let pesan = lines[2].toString(); 110 let pesan = lines[2].toString();
111 common.log("nomor nya "+nomor); 111 common.log("nomor nya "+nomor);
112 serialPort.unpipe(parserD); 112 serialPort.unpipe(parserD);
113 postIncomingMessage(nomor, pesan); 113 postIncomingMessage(nomor, pesan);
114 } 114 }
115 }); 115 });
116 116
117 common.log("pesan slot "+slot+" sudah di baca"); 117 common.log("pesan slot "+slot+" sudah di baca");
118 118
119 setTimeout(async function(){ 119 setTimeout(async function(){
120 await serialPortWrite("AT+CMGD="+slot); 120 await serialPortWrite("AT+CMGD="+slot);
121 await serialPortWrite("\r"); 121 await serialPortWrite("\r");
122 common.log("delete pesan slot "+slot+" done!"); 122 common.log("delete pesan slot "+slot+" done!");
123 }, 2000); 123 }, 2000);
124 } 124 }
125 125
126 function postIncomingMessage(nomor, pesan){ 126 function postIncomingMessage(nomor, pesan){
127 common.log("----------------------------post incoming message-------------------------"); 127 common.log("----------------------------post incoming message-------------------------");
128 pesan = pesan.replace(/\s+$/, ''); 128 pesan = pesan.replace(/\s+$/, '');
129 //let encodePesan = encodeURIComponent(pesan); 129 //let encodePesan = encodeURIComponent(pesan);
130 let inboxUrl = config.core.url_post+"/inbox?";//msg="+encodePesan+"&number="+nomor; 130 let inboxUrl = config.core.url_post;//+"/inbox?";msg="+encodePesan+"&number="+nomor;
131 common.log("url 2211; "+inboxUrl); 131 common.log("url 2211; "+inboxUrl);
132 let tryOptions = { 132 let tryOptions = {
133 url: inboxUrl, 133 url: inboxUrl,
134 qs: { 134 qs: {
135 msg: pesan, 135 msg: pesan,
136 number: nomor, 136 number: nomor,
137 modem: modemName 137 modem: modemName
138 } 138 }
139 } 139 }
140 140
141 //post to URL end Point 141 //post to URL end Point
142 request(tryOptions, function (error, response, body) { 142 request(tryOptions, function (error, response, body) {
143 if (error) { 143 if (error) {
144 common.log("error post! "+nomor+" msg; \""+pesan+"\" errornya; "+error.message); 144 common.log("error post! "+nomor+" msg; \""+pesan+"\" errornya; "+error.message);
145 } else if (!error && response.statusCode == 200) { 145 } else if (!error && response.statusCode == 200) {
146 common.log("success post "+nomor+" msg; \""+pesan+"\" resp; "+body); 146 common.log("success post "+nomor+" msg; \""+pesan+"\" resp; "+body);
147 } 147 }
148 }); 148 });
149 } 149 }
150 150
151 exports.sendingSMS = async function (message, phone_no) { 151 exports.sendingSMS = async function (message, phone_no) {
152 await setLock(); 152 await setLock();
153 153
154 await serialPortWrite("AT+CMGF=1\r"); 154 await serialPortWrite("AT+CMGF=1\r");
155 155
156 const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' })); 156 const parserD = serialPort.pipe(new Delimiter({ delimiter: '\r\nOK\r\n' }));
157 parserD.on('data',function(dataD) { 157 parserD.on('data',function(dataD) {
158 common.log('MESSAGE SENT'); 158 common.log('MESSAGE SENT');
159 serialPort.unpipe(parserD); 159 serialPort.unpipe(parserD);
160 setTimeout(() => { 160 setTimeout(() => {
161 releaseLock(); 161 releaseLock();
162 }, 2000) 162 }, 2000)
163 }) 163 })
164 164
165 common.log('number '+phone_no); 165 common.log('number '+phone_no);
166 await serialPortWrite(`AT+CMGS="${ phone_no }"\r${ message }`); 166 await serialPortWrite(`AT+CMGS="${ phone_no }"\r${ message }`);
167 common.log(message); 167 common.log(message);
168 await serialPortWrite(Buffer([0x1A])); 168 await serialPortWrite(Buffer([0x1A]));
169 } 169 }
170 170
171 exports.sendingUSSD = function (msg, count){ 171 exports.sendingUSSD = function (msg, count){
172 172
173 return new Promise(async resolve => { 173 return new Promise(async resolve => {
174 let respUssd = null; 174 let respUssd = null;
175 let new_respUssd; 175 let new_respUssd;
176 await serialPortWrite("AT+CUSD=1,\""+msg+"\""); 176 await serialPortWrite("AT+CUSD=1,\""+msg+"\"");
177 await serialPortWrite('\r'); 177 await serialPortWrite('\r');
178 178
179 const parserUSSD = serialPort.pipe(new Delimiter({ delimiter: '\",0'})); 179 const parserUSSD = serialPort.pipe(new Delimiter({ delimiter: '\",0'}));
180 parserUSSD.on('data',function(dataUSSD){ 180 parserUSSD.on('data',function(dataUSSD){
181 let lastData = dataUSSD.toString(); 181 let lastData = dataUSSD.toString();
182 common.log("incoming dataUSSD "+lastData); 182 common.log("incoming dataUSSD "+lastData);
183 respUssd = lastData; 183 respUssd = lastData;
184 serialPort.unpipe(parserUSSD); 184 serialPort.unpipe(parserUSSD);
185 }); 185 });
186 186
187 let countDown = 10; 187 let countDown = 10;
188 let waiting = setInterval(async () => { 188 let waiting = setInterval(async () => {
189 countDown--; 189 countDown--;
190 console.log("wait respUssd "+countDown); 190 console.log("wait respUssd "+countDown);
191 //if (respUssd || countDown < 2 ){ 191 //if (respUssd || countDown < 2 ){
192 if (respUssd){ 192 if (respUssd){
193 new_respUssd = respUssd.replace(/^[^ ]+\+CUSD:/, '\+CUSD:'); 193 new_respUssd = respUssd.replace(/^[^ ]+\+CUSD:/, '\+CUSD:');
194 //to close command if resp CUSD: 1 194 //to close command if resp CUSD: 1
195 if (new_respUssd.match(/^\+CUSD: 1,/)){ 195 if (new_respUssd.match(/^\+CUSD: 1,/)){
196 await serialPortWrite("AT+CUSD=2\r"); 196 await serialPortWrite("AT+CUSD=2\r");
197 } 197 }
198 resolve(new_respUssd); 198 resolve(new_respUssd);
199 clearInterval(waiting); 199 clearInterval(waiting);
200 } else if (!respUssd && countDown < 2){ 200 } else if (!respUssd && countDown < 2){
201 new_respUssd = "+CUSD: 4"; 201 new_respUssd = "+CUSD: 4";
202 resolve(new_respUssd); 202 resolve(new_respUssd);
203 clearInterval(waiting); 203 clearInterval(waiting);
204 } 204 }
205 },1000); 205 },1000);
206 }); 206 });
207 } 207 }
208 208
209 async function deleteMessage(serialPort, slot){ 209 async function deleteMessage(serialPort, slot){
210 await serialPortWrite("AT+CMGD=" + slot); 210 await serialPortWrite("AT+CMGD=" + slot);
211 await serialPortWrite("\r"); 211 await serialPortWrite("\r");
212 common.log("delete pesan slot "+slot+" done!"); 212 common.log("delete pesan slot "+slot+" done!");
213 } 213 }
214 214
215 215