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