-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
162 lines (158 loc) · 5.42 KB
/
app.js
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
const smpp = require("smpp"),
iconv = require("iconv-lite"),
_ = require("lodash"),
winston = require('winston');
delete smpp.encodings.UCS2;
module.exports = function Client(name, host, port, username, password, enquire, publish) {
let _isConnected = false;
let _status = false;
let _session = undefined;
let _enable = false;
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: name },
transports: [
new winston.transports.Console({ format: winston.format.simple() })
]
});
const connection_data = {
url: `smpp://${host}:${port}`,
auto_enquire_link_period: 1000 * enquire
};
function init() {
_session = smpp.connect(connection_data);
_session.on("connect", () => {
_isConnected = true;
_status = true;
});
_session.on("deliver_sm", (pdu) => {
if (_.isString(pdu.short_message.message) && pdu.esm_class === 4) {
let responseStatus = pdu.short_message.message.replace(/ date/g, "_date").split(" ");
let responseStatusObj = {};
_.each(responseStatus, (elements) => {
const element = elements.split(":");
responseStatusObj[element[0]] = element[1];
});
responseStatusObj["destination_addr"] = pdu.destination_addr;
responseStatusObj["source_addr"] = pdu.source_addr;
publish(`dlr.${name}`, {
name,
responseStatusObj
});
_session.send(pdu.response());
} else {
if (pdu.data_coding === 8) {
pdu.short_message.message = iconv.decode(pdu.short_message.message, 'latin1');
}
if (pdu.short_message.message.indexOf('dlvrd:') === -1) {
publish(`mo.${name}`, {
name,
pdu
});
_session.send(pdu.response());
}
}
});
_session.on("error", (error) => {
_isConnected = false;
logger.error(error.message);
_session.close();
});
_session.on("unknown", (pdu) => {
});
_session.on("enquire_link", (pdu) => {
_session.send(pdu.response());
});
_session.on("close", () => {
if (_enable === true) {
_isConnected = false;
const delay = process.env.SMPP_RETRAY_DELAY || 30;
logger.info(`${name}: This session is close. Trying to reconnect in ${delay}...`);
setTimeout(() => {
logger.info(`${name}: reconnecting...`);
init();
}, 1000 * delay);
} else {
_session.destroy();
logger.info(`Stop client ${name}`);
}
});
}
function start() {
if(_status === false){
_session.resume();
_status = true;
}
}
function pause() {
if(_status === true) {
_session.pause();
_status = false;
}
}
function enable() {
if(_enable === false){
_session.bind_transceiver({
system_id: username,
password
}, async (pdu) => {
const state = lookupPDUStatusKey(pdu.command_status);
if (state === "ESME_ROK") {
logger.info(`${name} is connected.`);
_enable = true;
} else {
logger.error(`${name} is not connected. smpp error: ${state}`);
_enable = false;
}
});
}
}
function disable() {
if(_enable === true) _enable = false;
}
function send(message, destine) {
return new Promise(resolve => {
if(_enable === false) throw new Error("Can send a message. Connector is not enable.");
let pdu;
if (message.length > 160) {
pdu = new smpp.PDU("submit_sm", {
destination_addr: destine,
short_message: "",
message_payload: message,
registered_delivery: 1
});
} else {
pdu = new smpp.PDU("submit_sm", {
destination_addr: destine,
short_message: message,
message_payload: "",
registered_delivery: 1
});
}
_session.send(pdu, (responsePdu) => {
const command_status = lookupPDUStatusKey(responsePdu.command_status);
if (responsePdu.command_status === 0) {
resolve({message_id: responsePdu.message_id, command_status: command_status});
} else {
resolve({message_id: undefined, command_status: command_status});
}
});
});
}
function lookupPDUStatusKey(pduCommandStatus) {
for (var k in smpp.errors) {
if (smpp.errors[k] === pduCommandStatus) {
return k;
}
}
}
return {
init,
send,
start,
enable,
disable,
pause
}
};