]> git.basschouten.com Git - openhab-addons.git/blob
53ebb0969866236fdd49391619ce6e86ce193dfe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.unifi.internal.handler;
14
15 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_GUEST_CLIENTS;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_TOTAL_CLIENTS;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRED_CLIENTS;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRELESS_CLIENTS;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.unifi.internal.UniFiSiteThingConfig;
23 import org.openhab.binding.unifi.internal.api.UniFiController;
24 import org.openhab.binding.unifi.internal.api.UniFiException;
25 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
26 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35
36 /**
37  * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
38  * updates for {@link UniFiSite} instances.
39  *
40  * @author Matthew Bowman - Initial contribution
41  * @author Hilbrand Bouwkamp - Initial contribution
42  */
43 @NonNullByDefault
44 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
45
46     private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
47
48     public UniFiSiteThingHandler(final Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     protected boolean initialize(final UniFiSiteThingConfig config) {
54         this.config = config;
55         if (!config.isValid()) {
56             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
57                     "@text/error.thing.site.offline.configuration_error");
58             return false;
59         }
60         return true;
61     }
62
63     @Override
64     protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
65         return cache.getSite(config.getSiteID());
66     }
67
68     @Override
69     protected State getChannelState(final UniFiSite site, final String channelId) {
70         final UniFiControllerCache cache = site.getCache();
71         final long count;
72
73         switch (channelId) {
74             case CHANNEL_TOTAL_CLIENTS:
75                 count = cache.countClients(site, c -> true);
76                 break;
77             case CHANNEL_WIRELESS_CLIENTS:
78                 count = cache.countClients(site, c -> c.isWireless());
79                 break;
80             case CHANNEL_WIRED_CLIENTS:
81                 count = cache.countClients(site, c -> c.isWired());
82                 break;
83             case CHANNEL_GUEST_CLIENTS:
84                 count = cache.countClients(site, c -> c.isGuest());
85                 break;
86             default:
87                 // Unsupported channel; nothing to update
88                 return UnDefType.NULL;
89         }
90         return new DecimalType(count);
91     }
92
93     @Override
94     protected boolean handleCommand(final UniFiController controller, final UniFiSite entity,
95             final ChannelUID channelUID, final Command command) throws UniFiException {
96         return false;
97     }
98 }