Skip to content

Commit 0cd3757

Browse files
committed
Telma USSD from HTTP
1 parent c569ac6 commit 0cd3757

File tree

7 files changed

+2091
-76
lines changed

7 files changed

+2091
-76
lines changed

config/account.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// (4 int) pwd : Your Mvola Password
2+
module.exports = {
3+
pwd : 'not_set'
4+
}

config/modem.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
(string) com: 'COMx' (Windows), '/dev/ttyUSBx' (Linux)
3+
(array) options: { read_time : 'Max timeout for wait AT Command response' }
4+
*/
5+
module.exports = {
6+
com : 'COM3',
7+
option: {
8+
read_time: 10000,
9+
}
10+
}

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ var cors = require('cors');
88
// Config
99
var server_config = require('./config/server');
1010

11+
// API Routes
12+
var ussd_api = require('./routes/ussd');
13+
1114
var app = express();
1215

1316
app.use(cors());
17+
app.use('/api/ussd', ussd_api);
1418

1519
var server = require('http').Server(app);
1620

libs/ModemLib.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict'
2+
3+
var Port = require('serial-at');
4+
var modem_config = require('../config/modem');
5+
var account_config = require('../config/account');
6+
7+
const port = new Port(modem_config.com, modem_config.option);
8+
var ussd_code = "";
9+
var need_mvola_pwd = false;
10+
11+
const ModemLib = function() {
12+
13+
const executeUSSD = function(ussd_type, callback) {
14+
15+
if (ussd_type == 'mvola_balance') {
16+
need_mvola_pwd = true;
17+
ussd_code = '"#111*1*6*1*' + account_config.pwd + '#"';
18+
}
19+
else if(ussd_type == 'account_balance') {
20+
ussd_code = '"#357#"';
21+
}
22+
else if(ussd_type == 'forfait_balance') {
23+
ussd_code = '"#358#"';
24+
}
25+
26+
if (need_mvola_pwd && checkMvolPwd(account_config.pwd) == false) {
27+
console.log("Mvola Password not set");
28+
return callback({ 'status': 401, 'error': { 'message': 'Please set your Mvola password' } });
29+
}
30+
31+
port.open()
32+
.then(result => {
33+
console.log("Executing AT Command");
34+
35+
port.at('AT+CUSD=1, ' + ussd_code + ', 15')
36+
.then(result => {
37+
var result = ModemLib.hexToString(result.split(',')[1].replace(/^"|"$/g, ''));
38+
let payload = {
39+
status: 200,
40+
success: {
41+
message: result
42+
}
43+
}
44+
45+
console.log(result);
46+
return callback(null, payload);
47+
})
48+
.catch(err => {
49+
let payload = {
50+
status: 500,
51+
error: {
52+
message: "An error has occured when excuting USSD Command. Please try later"
53+
}
54+
}
55+
56+
console.log(`.catch(${err})`);
57+
return callback(payload);
58+
});
59+
})
60+
.catch(err => {
61+
62+
let payload = {
63+
status: 302,
64+
error: {
65+
message: 'Service permanently unnavaible. Please try later'
66+
}
67+
}
68+
69+
console.log(`.catch(${err})`);
70+
return callback(payload);
71+
});
72+
}
73+
74+
const hexToString = function(hex) {
75+
return Buffer.from(hex, 'hex').toString('utf8');
76+
}
77+
78+
const checkMvolPwd = function(pwd) {
79+
if (pwd == 'not_set')
80+
return false;
81+
}
82+
83+
return {
84+
executeUSSD: executeUSSD,
85+
hexToString: hexToString,
86+
}
87+
}()
88+
89+
module.exports = ModemLib

0 commit comments

Comments
 (0)