-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWiFiManager.js
220 lines (196 loc) · 5.93 KB
/
WiFiManager.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
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
const EventEmitter = require('events');
const Debounce = require('./utils/Debounce');
const DeviceInstanceManager = require('./DeviceInstanceManager');
class WiFiManager extends EventEmitter {
constructor() {
super();
this.stations = {};
}
start() {
const debounced = Debounce(() => {
this.updateWiFi();
});
this.deviceUpdate = evt => {
if (!evt.key || evt.key.startsWith('network.wireless.radio.') || evt.key.startsWith('network.wireless.station.')) {
debounced();
}
}
DeviceInstanceManager.on('update', this.deviceUpdate);
this.updateWiFi();
}
stop() {
DeviceInstanceManager.off('update', this.deviceUpdate);
}
updateWiFi() {
const devices = DeviceInstanceManager.getAllDevices();
const newstations = {};
devices.forEach(dev => {
if (!dev.description.properties.ap) {
return;
}
const radios = dev.readKV('network.wireless.radio') || {};
const stations = dev.readKV('network.wireless.station') || {};
for (let idx in stations) {
const station = stations[idx];
if (station.ssid) {
station.bands = String(station.bands).split(',').map(band => radios[band]);
const options = dev.readKV(`network.wireless.station.${idx}.security.mode`, { info: true, value: false, selection: true });
station.security.options = options && options.selection || [ 'none' ];
const steering = dev.readKV(`network.wireless.station.${idx}.steering.preference`, { info: true, value: false, selection: true });
if (steering) {
station.steering.options = steering.selection || [ 'balance' ];
}
const cstation = newstations[station.ssid] || (newstations[station.ssid] = { instances: [] });
cstation.instances.push({
device: dev,
station: station
});
}
}
});
this.stations = newstations;
this.emit('update');
}
getAllStations() {
return Object.values(this.stations);
}
addDeviceToStation(station, device) {
for (let i = 0; i < station.instances.length; i++) {
if (station.instances[i].device === device) {
return false;
}
}
station.instances.push({
device: device,
station: station
});
return true;
}
removeDeviceFromStation(station, device) {
for (let i = 0; i < station.instances.length; i++) {
if (station.instances[i].device === device) {
station.instances.splice(i, 1);
return true;
}
}
return false;
}
getStationConfig(station) {
const config = {
station: station
};
const primary = station.instances[0] && station.instances[0].station;
if (!primary) {
return config;
}
config.ssid = primary.ssid;
config.enable = primary.enable;
config.hidden = primary.hidden;
config.security = {
mode: primary.security.mode,
options: [].concat(primary.security.options),
passphrase: primary.security.passphrase
};
if (primary.steering) {
config.steering = {
preference: primary.steering.preference,
options: primary.steering.options
};
}
config.vlan = primary.vlan;
config.isolate = primary.isolate;
config.bands = {};
for (let i = 1; i < station.instances.length; i++) {
const stat = station.instances[i].station;
// Make all bands available to station, even if all devices dont support them.
stat.bands.forEach(band => {
const name = band.band;
if (!config.bands[name]) {
config.bands[name] = {
band: name
};
}
});
// Only support security modes available on all devices
for (let j = config.security.options.length - 1; j >= 0; j--) {
if (stat.security.options.indexOf(config.security.options[j]) === -1) {
config.security.options.splice(j, 1);
}
}
// Only support vlan if all instances support vlan
if (!('vlan' in stat)) {
delete config.vlan;
}
// Same for isolate
if (!('isolate' in stat)) {
delete config.isolate;
}
// Support as much steering as possible
if ('steering' in stat) {
if (!config.steering) {
config.steering = {};
}
if ('enable' in stat.steering) {
config.steering.enable = stat.steering.enable;
}
if ('preference' in stat.steering) {
config.steering.preference = stat.steering.preference;
}
if ('minrssi' in stat.steering) {
config.steering.minrssi = stat.steering.minrssi
}
if ('options' in stat.steering) {
for (let i = config.steering.options.length - 1; i >= 0; i--) {
if (stat.steering.options.indexOf(config.steering.options[i]) === -1) {
config.steering.options.splice(i, 1);
}
}
}
}
// Same for fast roaming
if ('fastroaming' in stat) {
if (!config.fastroaming) {
config.fastroaming = {};
}
if ('enable' in stat.fastroaming) {
config.fastroaming.enable = stat.fastroaming.enable;
}
}
}
if (config.security.options.indexOf(config.security.mode) === -1) {
config.security.mode = 'none';
}
if (config.steering && config.steering.options.indexOf(config.steering.preference) === -1) {
config.steering.preference = 'balance';
}
return config;
}
createStation(ssid) {
this.stations[ssid] = {
ssid: {
name: ssid,
enable: false,
hidden: false
},
instances: [],
bands: [],
security: {
mode: 'none',
passphrase: ''
},
vlan: 0,
isolate: {
enable: false,
},
fastroaming: {
enable: false
},
steering: {
enable: false,
preference: '',
minrssi: 0
}
};
}
}
module.exports = new WiFiManager();