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