-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-RouterClients.js
More file actions
146 lines (133 loc) · 3.75 KB
/
MMM-RouterClients.js
File metadata and controls
146 lines (133 loc) · 3.75 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
//Can't import normally since this is not a module...
var RouterInterfaceType;
(async () => {
RouterInterfaceType = await (await import("./routerprovider.mjs")).RouterInterfaceType;
})();
Module.register("MMM-RouterClients",{
defaults: {
showClientNames: true,
showInterfaceName: true,
showProtected: true,
showSSID: true,
alertNewClients: true,
newDuration: 1000 * 60 * 60 * 24, // One full day
updateInterval: 1000 * 60 * 60, // Every hour
persistData: false,
provider: null,
host: null,
port: null,
protocol: null,
username: null,
password: null,
privateKey: null,
privateKeyPath: null
},
start: function() {
let self = this;
self.interfaces = [];
self.sendSocketNotification("GET_ROUTER_DATA", self.config);
setInterval(() => {
self.sendSocketNotification("GET_ROUTER_DATA", self.config);
}, self.config.updateInterval);
},
getTemplate: function () {
return "MMM-RouterClients.njk";
},
getStyles: function () {
return ["MMM-RouterClients.css", "font-awesome.css"];
},
getTemplateData: function () {
let self = this;
return {
config: self.config,
interfaces: self.interfaces
}
},
getTranslations: function() {
return {
sv: "translations/sv.json",
en: "translations/en.json"
};
},
socketNotificationReceived: function (notification, payload) {
let self = this;
if (notification == "ROUTER_DATA") {
self.interfaces = self.transformInterfaces(payload.interfaces);
if (self.config.alertNewClients) {
let newClients = self.newClients(self.interfaces);
self.alertNewClients(newClients);
}
self.updateDom();
} else if (notification == "ROUTER_DATA_ERROR") {
this.sendNotification("SHOW_ALERT", {
title : this.name + ": " + self.translate("ROUTER_DATA_ERROR"),
message : payload,
timer: self.config.updateInterval
});
}
},
transformInterfaces: function(interfaces) {
let self = this;
return interfaces.map(interface => self.transformInterface(interface));
},
transformInterface: function(interface) {
let self = this;
let result = {protected: interface.protected, clients: self.transformClients(interface.clients), ssid: interface.ssid};
switch (interface.type) {
case RouterInterfaceType.WIFI_2_4G:
return Object.assign(result, {
"icon" : "fa-wifi",
"iconSub" : "2.4G",
"name" : self.translate("WIFI_2.4G")
});
case RouterInterfaceType.WIFI_5G:
return Object.assign(result, {
"icon" : "fa-wifi",
"iconSub" : "5G",
"name" : self.translate("WIFI_5G")
});
case RouterInterfaceType.GUEST_WIFI_2_4G:
return Object.assign(result, {
"icon" : "fa-wifi",
"iconSub" : "2.4G",
"name" : self.translate("GUEST_WIFI_2.4G")
});
case RouterInterfaceType.GUEST_WIFI_5G:
return Object.assign(result, {
"icon" : "fa-wifi",
"iconSub" : "5G",
"name" : self.translate("GUEST_WIFI_5G")
});
case RouterInterfaceType.WIRED:
return Object.assign(result, {
"icon" : "fa-ethernet",
"name" : self.translate("WIRED")
});
default:
return result;
}
},
transformClients: function(clients) {
let self = this;
return clients.map(client => self.transformClient(client));
},
transformClient: function(client) {
let self = this;
let name = client.name ? client.name : self.translate("NO_CLIENT_NAME");
return {
name : name,
new : client.new
}
},
alertNewClients: function(newClients) {
let self = this;
this.sendNotification("SHOW_ALERT", {
title : this.name + ": " + self.translate("NEW_CLIENTS_TITLE"),
message : newClients.join(", "),
timer: self.config.newDuration
});
},
newClients: function(interfaces) {
return interfaces.flatMap(interface => interface.clients).filter(client => client.new).map(client => client.name);
}
});