partner-mkios.js
10.6 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"use strict";
const fs = require('fs');
const moment = require('moment');
const Modem = require('./modem');
const pullgw = require('komodo-sdk/gateway/pull');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const matrix = require('komodo-sdk/matrix');
const modemDashboard = require('./modem-dashboard');
const chipsJsonFile = process.cwd() + '/chips.json';
const chips = fs.existsSync(chipsJsonFile) ? require(chipsJsonFile) : {};
if (config && config.debug_modem) {
process.env.KOMODO_DEBUG_MODEM=1;
}
/*
if (!config || !config.partner || !config.partner.pin) {
logger.warn('Undefined PIN');
process.exit(1);
}
*/
matrix.modem = {};
matrix.not_ready = true;
matrix.not_ready_ts = null;
matrix.not_ready_ts_readable = null;
matrix.not_ready_max_age_secs = null;
matrix.stock = {};
const db = require('./local-db').getConnection();
const pendingArchive = require('./pending-archive');
const patternMatcher = require('./pattern-rule-matcher');
const smsHandler = require('./sms-handler');
const modem = new Modem(config.partner.modem.dev, {baudRate: 115200});
const resumeHandlers = {};
let last_trx_id = null;
function getPin() {
return ( config && config.partner && config.partner.pin ) || ( matrix && matrix.modem && matrix.modem.pin_trx );
}
modem.on('open', function() {
logger.info('Modem opened');
const ussd_command = '*776*' + getPin() + '#';
db.run("INSERT INTO ussd VALUES (?, ?, 'OUT', ?, ?)", moment().format('YYYY-MM-DD HH:mm:ss'), moment().format('YYYY-MM-DD'), matrix.modem.imsi, 'AT+CUSD=1,"' + ussd_command + '",15', function(err) {
if (err) {
logger.warn('Error inserting ussd command (stock check) to local database', {err: err});
}
});
modem.sendUSSD(ussd_command, function() {});
})
modem.on('imsi', function(imsi) {
logger.verbose('IMSI: ' + imsi);
matrix.modem.imsi = imsi;
matrix.modem.msisdn = chips && chips.by_imsi && chips.by_imsi[imsi] && chips.by_imsi[imsi].msisdn ? chips.by_imsi[imsi].msisdn : config.partner.msisdn;
matrix.modem.pin_trx = chips && chips.by_imsi && chips.by_imsi[imsi] && chips.by_imsi[imsi].pin ? chips.by_imsi[imsi].pin : null;
})
function onIncomingSMS(sms) {
logger.info('Incoming SMS', {sms: sms});
db.run("INSERT INTO sms VALUES (?, ?, 'IN', ?, ?, ?)", sms.created, moment(sms.created).format('YYYY-MM-DD'), matrix.modem.imsi, sms.sender, sms.msg, function(err) {
if (err) {
logger.warn('Error inserting sms to local database', {err: err});
}
});
if (!smsHandler.isAllowedSender(sms.sender)) {
logger.verbose('Ignoring SMS from unknown sender', {sender: sms.sender});
return;
}
const stocks = smsHandler.getMultiStockBalance(sms.msg);
if (stocks && Array.isArray(stocks) && stocks.length) {
stocks.forEach(function(stock) {
const vals = stock.split('=');
updateStock(vals[0], vals[1]);
})
}
else {
const stock = smsHandler.getStockBalance(sms.msg);
if (stock.name && stock.balance) {
updateStock(stock.name, stock.balance);
}
}
const destination = smsHandler.getDestination(sms.msg);
if (!destination) {
logger.verbose('Ignoring SMS with unknown trx destination');
return;
}
const product = smsHandler.getProduct(sms.msg);
if (!product) {
logger.verbose('Ignoring SMS with unknown trx product');
return;
}
const trx_date = smsHandler.getTrxDate(sms.msg);
if (!trx_date) {
logger.verbose('Ignoring SMS with unknown trx date');
return;
}
logger.verbose('SMS message parsed and extracted', {destination: destination, product: product, trx_date: trx_date});
pendingArchive.get(destination, product, trx_date, function(err, trx_id) {
if (!trx_id) {
logger.verbose('No pending trx suits with SMS', {destination: destination, product: product, trx_date: trx_date});
return;
}
deleteResumeHandler(trx_id);
pendingArchive.remove(trx_id);
report({
trx_id: trx_id,
rc: smsHandler.getRc(sms.msg) || '68',
sn: smsHandler.getSn(sms.msg),
message: 'SMS: ' + sms.msg
})
})
}
modem.on('incoming sms', onIncomingSMS);
modem.on('signal strength', function(signal_strength) {
matrix.modem.signal_strength = signal_strength;
logger.verbose('Signal strength: ' + signal_strength);
})
function onUSSDResponse(data) {
logger.verbose('Got USSD response', {response: data});
db.run("INSERT INTO ussd VALUES (?, ?, 'IN', ?, ?)", moment().format('YYYY-MM-DD HH:mm:ss'), moment().format('YYYY-MM-DD'), matrix.modem.imsi, data, function(err) {
if (err) {
logger.warn('Error inserting ussd response to local database', {err: err});
}
});
if (!last_trx_id) return;
const rc = getRcFromMessage(data, config.ussd_parser.rc) || '68';
if (rc !== '68') {
onTrxFinish(last_trx_id);
}
deleteResumeHandler(last_trx_id);;
unsuspendPull();
report({
trx_id: last_trx_id,
rc: rc,
sn: getSnFromMessage(data),
message: data
});
const stock_name = getStockProductFromMessage(data);
const stock_balance = getStockBalanceFromMesssage(data);
updateStock(stock_name, stock_balance);
last_trx_id = null;
}
modem.on('ussd response', onUSSDResponse);
logger.info('Opening MODEM');
modem.open(function(err) {
if (err) {
logger.warn('Error opening modem port', {err: err});
process.exit(1);
}
logger.info('Modem open successfully, going to ready in 30 secs');
setTimeout( unsuspendPull, 30 * 1000 )
})
function updateStock(stock_name, stock_balance) {
if (stock_name && (stock_balance !== undefined || stock_balance !== null)) {
logger.verbose('Updating stock', {stock_name: stock_name, stock_balance: stock_balance});
const new_stock_name = config && config.remote_product_alias && config.remote_product_alias[stock_name] ? config.remote_product_alias[stock_name] : stock_name;
matrix.stock[new_stock_name] = Number(stock_balance);
logger.verbose('Stock balance updated', {stock: matrix.stock});
}
}
function getRcFromMessage(msg, rules) {
if (!rules || !Array.isArray(rules)) {
return '68';
}
for (let rule of rules) {
if (!rule.pattern) return '68';
const re = new RegExp(rule.pattern);
if (msg.search(re) >= 0) {
return rule.rc ? rule.rc : '68';
}
}
return '68';
}
function getPatternMatchFromMessage(msg, rules) {
if (!rules || !Array.isArray(rules)) {
return;
}
for (let rule of rules) {
if (!rule.pattern) return;
const re = new RegExp(rule.pattern);
const matches = msg.match(re);
if (matches && matches.length >= 2) {
return matches[1];
}
}
}
function getSnFromMessage(msg, rules) {
return patternMatcher(msg, config.ussd_parser.sn);
}
function getStockProductFromMessage(msg, rules) {
return patternMatcher(msg, config.ussd_parser.stock.product);
}
function getStockBalanceFromMesssage(msg, rules) {
return patternMatcher(msg, config.ussd_parser.stock.balance);
}
function suspendPull(trx_id) {
logger.verbose('Set modem to not ready so no other task can be entered and registering delayed resume');
matrix.not_ready = true;
matrix.not_ready_ts = new Date();
matrix.not_ready_ts_readable = moment(matrix.not_ready_ts).format('YYYY-MM-DD HH:mm:ss');
resumeHandlers['trx' + trx_id] = setTimeout(function() {
logger.verbose('Resuming supsended modem', {trx_id: trx_id});
unsuspendPull();
report({
trx_id: trx_id,
rc: '68',
message: 'USSD timeout'
})
}, Number(config.partner.modem.suspend_time_ms) || 20 * 1000 );
}
function unsuspendPull() {
matrix.not_ready = false;
if (matrix.not_ready_ts) {
matrix.not_ready_max_age_secs = Math.max( (new Date() - matrix.not_ready_ts) / 1000, matrix.not_ready_max_age_secs );
}
logger.verbose('Modem is ready');
}
function deleteResumeHandler(trx_id) {
if (resumeHandlers['trx' + trx_id]) {
logger.verbose('Unregistering delayed resume of trx id ' + trx_id);
clearTimeout(resumeHandlers['trx' + trx_id]);
delete resumeHandlers['trx' + trx_id];
}
}
function onTrxFinish(trx_id) {
deleteResumeHandler(trx_id);
pendingArchive.remove(trx_id);
unsuspendPull();
}
function buy(task) {
if (!getPin()) {
report({
trx_id: task.trx_id,
rc: '40',
message: 'INTERNAL: Tidak dapat melakukan transaksi. PIN transaksi tidak terdefinisi.'
});
return;
}
if (task.product === task.remote_product) {
report({
trx_id: task.trx_id,
rc: '40',
message: 'INTERNAL: Tidak dapat melakukan transaksi. Kode USSD belum terdefinisi.'
});
return;
}
suspendPull(task.trx_id);
last_trx_id = task.trx_id;
pendingArchive.put(task, function(err) {
if (err) {
logger.verbose('Error inserting task to pending archive', {trx_id: task.trx_id, destination: task.destination, product: task.product, err: err});
onTrxFinish(task.trx_id);
report({
trx_id: task.trx_id,
rc: '40',
message: 'INTERNAL: Gagal melakukan transaksi. Mungkin ada transaksi dengan nomor dengan produk yang sama yang masih diproses. Silahkan ulangi beberapa saat lagi.'
});
return;
}
const ussd_command = task.remote_product.split(',')[0].replace("<DESTINATION>", task.destination).replace("<PIN>", getPin());
logger.verbose('Going to execute USSD', {trx_id: task.trx_id, destination: task.destination, product: task.product, ussd: ussd_command});
db.run("INSERT INTO ussd VALUES (?, ?, 'OUT', ?, ?)", moment().format('YYYY-MM-DD HH:mm:ss'), moment().format('YYYY-MM-DD'), matrix.modem.imsi, 'AT+CUSD=1,"' + ussd_command + '",15', function(err) {
if (err) {
logger.warn('Error inserting ussd command to local database', {err: err});
}
});
modem.sendUSSD(ussd_command, function() {});
})
}
function report(data) {
if (data.message) {
if (matrix.modem.imsi) {
data.message = 'CHIP-IMSI: ' + matrix.modem.imsi + '; ' + data.message;
}
if (matrix.modem.msisdn) {
data.message = 'CHIP-MSISDN: ' + matrix.modem.msisdn + '; ' + data.message;
}
}
pullgw.report(data);
}
exports.buy = buy;