|
14 | 14 |
|
15 | 15 | import java.util.Collection;
|
16 | 16 | import java.util.List;
|
| 17 | +import java.util.Map; |
17 | 18 |
|
18 | 19 | import org.eclipse.jdt.annotation.NonNullByDefault;
|
19 | 20 | import org.eclipse.jdt.annotation.Nullable;
|
|
23 | 24 | import org.openhab.binding.unifi.internal.api.dto.UnfiPortOverrideJsonObject;
|
24 | 25 | import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
|
25 | 26 | import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
|
| 27 | +import org.openhab.binding.unifi.internal.api.dto.UniFiNetwork; |
26 | 28 | import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
|
27 | 29 | import org.openhab.binding.unifi.internal.api.dto.UniFiSwitchPorts;
|
28 | 30 | import org.openhab.binding.unifi.internal.api.dto.UniFiUnknownClient;
|
|
34 | 36 | import org.openhab.binding.unifi.internal.api.util.UniFiClientDeserializer;
|
35 | 37 | import org.openhab.binding.unifi.internal.api.util.UniFiClientInstanceCreator;
|
36 | 38 | import org.openhab.binding.unifi.internal.api.util.UniFiDeviceInstanceCreator;
|
| 39 | +import org.openhab.binding.unifi.internal.api.util.UniFiNetworkInstanceCreator; |
37 | 40 | import org.openhab.binding.unifi.internal.api.util.UniFiSiteInstanceCreator;
|
38 | 41 | import org.openhab.binding.unifi.internal.api.util.UniFiVoucherInstanceCreator;
|
39 | 42 | import org.openhab.binding.unifi.internal.api.util.UniFiWlanInstanceCreator;
|
@@ -87,12 +90,14 @@ public UniFiController(final HttpClient httpClient, final String host, final int
|
87 | 90 | this.timeoutSeconds = timeoutSeconds;
|
88 | 91 | this.csrfToken = "";
|
89 | 92 | final UniFiSiteInstanceCreator siteInstanceCreator = new UniFiSiteInstanceCreator(cache);
|
| 93 | + final UniFiNetworkInstanceCreator networkInstanceCreator = new UniFiNetworkInstanceCreator(cache); |
90 | 94 | final UniFiWlanInstanceCreator wlanInstanceCreator = new UniFiWlanInstanceCreator(cache);
|
91 | 95 | final UniFiDeviceInstanceCreator deviceInstanceCreator = new UniFiDeviceInstanceCreator(cache);
|
92 | 96 | final UniFiClientInstanceCreator clientInstanceCreator = new UniFiClientInstanceCreator(cache);
|
93 | 97 | final UniFiVoucherInstanceCreator voucherInstanceCreator = new UniFiVoucherInstanceCreator(cache);
|
94 | 98 | this.gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
|
95 | 99 | .registerTypeAdapter(UniFiSite.class, siteInstanceCreator)
|
| 100 | + .registerTypeAdapter(UniFiNetwork.class, networkInstanceCreator) |
96 | 101 | .registerTypeAdapter(UniFiWlan.class, wlanInstanceCreator)
|
97 | 102 | .registerTypeAdapter(UniFiDevice.class, deviceInstanceCreator)
|
98 | 103 | .registerTypeAdapter(UniFiClient.class, new UniFiClientDeserializer())
|
@@ -149,6 +154,7 @@ public void refresh() throws UniFiException {
|
149 | 154 | synchronized (this) {
|
150 | 155 | cache.clear();
|
151 | 156 | final Collection<UniFiSite> sites = refreshSites();
|
| 157 | + refreshNetworks(sites); |
152 | 158 | refreshWlans(sites);
|
153 | 159 | refreshDevices(sites);
|
154 | 160 | refreshClients(sites);
|
@@ -208,8 +214,17 @@ public void poePowerCycle(final UniFiDevice device, final Integer portIdx) throw
|
208 | 214 | refresh();
|
209 | 215 | }
|
210 | 216 |
|
| 217 | + public void enableNetwork(final UniFiNetwork network, final boolean enable) throws UniFiException { |
| 218 | + final UniFiControllerRequest<Void> req = newRequest(Void.class, HttpMethod.PUT, gson); |
| 219 | + req.setAPIPath(String.format("/api/s/%s/group/networkconf", network.getSite().getName())); |
| 220 | + req.setBodyParameter("data", Map.of("enabled", enable)); |
| 221 | + req.setBodyParameter("id", new String[] { network.getId() }); |
| 222 | + executeRequest(req); |
| 223 | + refresh(); |
| 224 | + } |
| 225 | + |
211 | 226 | public void enableWifi(final UniFiWlan wlan, final boolean enable) throws UniFiException {
|
212 |
| - final UniFiControllerRequest<Void> req = newRequest(Void.class, HttpMethod.PUT, poeGson); |
| 227 | + final UniFiControllerRequest<Void> req = newRequest(Void.class, HttpMethod.PUT, gson); |
213 | 228 | req.setAPIPath(String.format("/api/s/%s/rest/wlanconf/%s", wlan.getSite().getName(), wlan.getId()));
|
214 | 229 | req.setBodyParameter("_id", wlan.getId());
|
215 | 230 | req.setBodyParameter("enabled", enable ? "true" : "false");
|
@@ -309,6 +324,18 @@ private List<UniFiSite> refreshSites() throws UniFiException {
|
309 | 324 | return cache.setSites(executeRequest(req));
|
310 | 325 | }
|
311 | 326 |
|
| 327 | + private void refreshNetworks(final Collection<UniFiSite> sites) throws UniFiException { |
| 328 | + for (final UniFiSite site : sites) { |
| 329 | + cache.putNetworks(getNetworks(site)); |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + private UniFiNetwork @Nullable [] getNetworks(final UniFiSite site) throws UniFiException { |
| 334 | + final UniFiControllerRequest<UniFiNetwork[]> req = newRequest(UniFiNetwork[].class, HttpMethod.GET, gson); |
| 335 | + req.setAPIPath(String.format("/api/s/%s/rest/networkconf", site.getName())); |
| 336 | + return executeRequest(req); |
| 337 | + } |
| 338 | + |
312 | 339 | private void refreshWlans(final Collection<UniFiSite> sites) throws UniFiException {
|
313 | 340 | for (final UniFiSite site : sites) {
|
314 | 341 | cache.putWlans(getWlans(site));
|
|
0 commit comments