2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.unifi.internal.handler;
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;
22 import java.util.function.Predicate;
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;
46 * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
47 * updates for {@link UniFiSite} instances.
49 * @author Matthew Bowman - Initial contribution
50 * @author Hilbrand Bouwkamp - Initial contribution
51 * @author Mark Herwege - Added guest vouchers
54 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
56 private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
58 public UniFiSiteThingHandler(final Thing thing) {
63 protected boolean initialize(final UniFiSiteThingConfig config) {
65 if (!config.isValid()) {
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
67 "@text/error.thing.site.offline.configuration_error");
74 protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
75 return cache.getSite(config.getSiteID());
79 protected State getChannelState(final UniFiSite site, final String channelId) {
83 case CHANNEL_TOTAL_CLIENTS:
84 state = countClients(site, c -> true);
86 case CHANNEL_WIRELESS_CLIENTS:
87 state = countClients(site, c -> c.isWireless());
89 case CHANNEL_WIRED_CLIENTS:
90 state = countClients(site, c -> c.isWired());
92 case CHANNEL_GUEST_CLIENTS:
93 state = countClients(site, c -> c.isGuest());
95 case CHANNEL_GUEST_VOUCHER:
96 final String voucher = site.getVoucher();
97 state = (voucher != null) ? StringType.valueOf(voucher) : UnDefType.UNDEF;
99 case CHANNEL_GUEST_VOUCHERS_GENERATE:
100 state = OnOffType.OFF;
103 // Unsupported channel; nothing to update
104 return UnDefType.NULL;
109 private static State countClients(final UniFiSite site, final Predicate<UniFiClient> filter) {
110 return new DecimalType(site.getCache().countClients(site, filter));
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();
118 if (CHANNEL_GUEST_VOUCHERS_GENERATE.equals(channelID)) {
119 final Channel channel = getThing().getChannel(CHANNEL_GUEST_VOUCHERS_GENERATE);
120 if (channel == null) {
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);