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