]> git.basschouten.com Git - openhab-addons.git/blob
5234fc49134d5ce6a178a33285964abf56f1f758
[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_GUEST_VOUCHER;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_GUEST_VOUCHERS_GENERATE;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_TOTAL_CLIENTS;
19 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRED_CLIENTS;
20 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRELESS_CLIENTS;
21
22 import java.util.function.Predicate;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.unifi.internal.UniFiSiteThingConfig;
27 import org.openhab.binding.unifi.internal.UniFiVoucherChannelConfig;
28 import org.openhab.binding.unifi.internal.api.UniFiController;
29 import org.openhab.binding.unifi.internal.api.UniFiException;
30 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
31 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
32 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.thing.Channel;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.State;
43 import org.openhab.core.types.UnDefType;
44
45 /**
46  * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
47  * updates for {@link UniFiSite} instances.
48  *
49  * @author Matthew Bowman - Initial contribution
50  * @author Hilbrand Bouwkamp - Initial contribution
51  * @author Mark Herwege - Added guest vouchers
52  */
53 @NonNullByDefault
54 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
55
56     private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
57
58     public UniFiSiteThingHandler(final Thing thing) {
59         super(thing);
60     }
61
62     @Override
63     protected boolean initialize(final UniFiSiteThingConfig config) {
64         this.config = config;
65         if (!config.isValid()) {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
67                     "@text/error.thing.site.offline.configuration_error");
68             return false;
69         }
70         return true;
71     }
72
73     @Override
74     protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
75         return cache.getSite(config.getSiteID());
76     }
77
78     @Override
79     protected State getChannelState(final UniFiSite site, final String channelId) {
80         final State state;
81
82         switch (channelId) {
83             case CHANNEL_TOTAL_CLIENTS:
84                 state = countClients(site, c -> true);
85                 break;
86             case CHANNEL_WIRELESS_CLIENTS:
87                 state = countClients(site, c -> c.isWireless());
88                 break;
89             case CHANNEL_WIRED_CLIENTS:
90                 state = countClients(site, c -> c.isWired());
91                 break;
92             case CHANNEL_GUEST_CLIENTS:
93                 state = countClients(site, c -> c.isGuest());
94                 break;
95             case CHANNEL_GUEST_VOUCHER:
96                 final String voucher = site.getVoucher();
97                 state = (voucher != null) ? StringType.valueOf(voucher) : UnDefType.UNDEF;
98                 break;
99             case CHANNEL_GUEST_VOUCHERS_GENERATE:
100                 state = OnOffType.OFF;
101                 break;
102             default:
103                 // Unsupported channel; nothing to update
104                 return UnDefType.NULL;
105         }
106         return state;
107     }
108
109     private static State countClients(final UniFiSite site, final Predicate<UniFiClient> filter) {
110         return new DecimalType(site.getCache().countClients(site, filter));
111     }
112
113     @Override
114     protected boolean handleCommand(final UniFiController controller, final UniFiSite entity,
115             final ChannelUID channelUID, final Command command) throws UniFiException {
116         final String channelID = channelUID.getId();
117
118         if (CHANNEL_GUEST_VOUCHERS_GENERATE.equals(channelID)) {
119             final Channel channel = getThing().getChannel(CHANNEL_GUEST_VOUCHERS_GENERATE);
120             if (channel == null) {
121                 return false;
122             }
123             final UniFiVoucherChannelConfig config = channel.getConfiguration().as(UniFiVoucherChannelConfig.class);
124             final int count = config.getCount();
125             final int expire = config.getExpiration();
126             final int users = config.getVoucherUsers();
127             final Integer upLimit = config.getUpLimit();
128             final Integer downLimit = config.getDownLimit();
129             final Integer dataQuota = config.getDataQuota();
130             controller.generateGuestVouchers(entity, count, expire, users, upLimit, downLimit, dataQuota);
131             return true;
132         }
133         return false;
134     }
135 }