modems2.js
3.46 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
'use strict';
const logger = global.KOMODO_LOGGER;
if (logger) {
logger.verbose('MODEMS: logger set using global logger');
}
const modemList = {
by_name: {},
by_imsi: {},
by_msisdn: {},
};
/**
* Objek data sebuah modem.
*
* @typedef {Object} ModemData
* @property {string} name - nama modem
* @property {string} device - modem device path
* @property {string} imsi - IMSI modem
* @property {string} msisdn - MSISDN modem
* @property {string} networkId - operator network id
* @property {string} networkName - operator network name
* @property {string} signalStrength - modem signal strength
* @property {number} uptime - modem uptime in seconds
* @property {string} reportIp - IP modem
* @property {number} reportPort - TCP port modem
* @property {string} reportApikey - APIKEY modem
* @property {string} reportPathSms - url path untuk pengiriman SMS
*/
/**
* Update data sebuah modem berdasarkan nama modem.
*
* @param {ModemData} val - objek data modem
*/
function touchByName(val) {
if (!val || !val.name) return;
if (typeof val.name !== 'string') return;
if (!val.name.trim()) return;
modemList.by_name[val.name] = val;
}
/**
* Update data sebuah modem berdasarkan IMSI.
*
* @param {ModemData} val - objek data modem
*/
function touchByIMSI(val) {
if (!val || !val.imsi) return;
if (typeof val.imsi !== 'string') return;
if (!val.imsi.trim()) return;
if (logger && !modemList.by_imsi[val.imsi]) {
logger.verbose(`New IMSI registration`, {
imsi: val.imsi,
name: val.name,
ip: val.reportIp,
device: val.device,
imsi_registered: Object.keys(modemList.by_imsi).length + 1,
});
}
const oldName = modemList.by_imsi[val.imsi] ? modemList.by_imsi[val.imsi].name : null;
if (oldName && oldName !== val.name && modemList.by_name[oldName]) {
logger.info(`Modem with IMSI ${val.imsi} move detected`, {
old_name: oldName,
old_ip: modemList.by_name[oldName].reportIp,
old_device: modemList.by_name[oldName].device,
new_name: val.name,
new_ip: val.reportIp,
new_device: val.device,
});
delete modemList.by_name[oldName];
}
modemList.by_imsi[val.imsi] = val;
}
/**
* Update data sebuah modem berdasarkan MSISDN.
*
* @param {ModemData} val - objek data modem
*/
function touchByMSISDN(val) {
if (!val || !val.msisdn) return;
if (typeof val.msisdn !== 'string') return;
if (!val.msisdn.trim()) return;
modemList.by_msisdn[val.msisdn] = val;
}
/**
* Update data sebuah modem.
*
* @param {ModemData} val - objek data modem
* @see ModemData
*/
function touch(val) {
if (!val || typeof val !== 'object') return;
val._ts = new Date();
if (!val.reportIp) {
val.reportIp = '127.0.0.1';
}
touchByName(val);
touchByIMSI(val);
touchByMSISDN(val);
}
/**
* Ambil data sebuah modem.
*
* @param {string} selector - selector pencarian, valid jika bernilai salah satu dari: name, imsi, msisdn
* @param {string} keyword - kata kunci modem yang ingin diambil
* @returns {ModemData} data modem terkait
*/
function get(selector, keyword) {
if (!selector || !keyword) return null;
return modemList[`by_${selector}`] ? modemList[`by_${selector}`][keyword] : null;
}
function dump() {
return modemList;
}
exports.touch = touch;
exports.set = touch;
exports.get = get;
exports.dump = dump;