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.*;
17 import java.util.function.Predicate;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.unifi.internal.UniFiSiteThingConfig;
22 import org.openhab.binding.unifi.internal.UniFiVoucherChannelConfig;
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.UniFiClient;
27 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.State;
38 import org.openhab.core.types.UnDefType;
41 * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
42 * updates for {@link UniFiSite} instances.
44 * @author Matthew Bowman - Initial contribution
45 * @author Hilbrand Bouwkamp - Initial contribution
46 * @author Mark Herwege - Added guest vouchers
49 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
51 private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
53 public UniFiSiteThingHandler(final Thing thing) {
58 protected boolean initialize(final UniFiSiteThingConfig config) {
60 if (!config.isValid()) {
61 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
62 "@text/error.thing.site.offline.configuration_error");
69 protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
70 return cache.getSite(config.getSiteID());
74 protected State getChannelState(final UniFiSite site, final String channelId) {
78 case CHANNEL_TOTAL_CLIENTS:
79 state = countClients(site, c -> true);
81 case CHANNEL_WIRELESS_CLIENTS:
82 state = countClients(site, c -> c.isWireless());
84 case CHANNEL_WIRED_CLIENTS:
85 state = countClients(site, c -> c.isWired());
87 case CHANNEL_GUEST_CLIENTS:
88 state = countClients(site, c -> c.isGuest());
90 case CHANNEL_GUEST_VOUCHER:
91 String voucher = site.getVoucher();
92 state = (voucher != null) ? StringType.valueOf(voucher) : UnDefType.UNDEF;
94 case CHANNEL_GUEST_VOUCHERS_GENERATE:
95 state = OnOffType.OFF;
98 // Unsupported channel; nothing to update
99 return UnDefType.NULL;
104 private static State countClients(final UniFiSite site, final Predicate<UniFiClient> filter) {
105 return new DecimalType(site.getCache().countClients(site, filter));
109 protected boolean handleCommand(final UniFiController controller, final UniFiSite entity,
110 final ChannelUID channelUID, final Command command) throws UniFiException {
111 final String channelID = channelUID.getId();
113 if (CHANNEL_GUEST_VOUCHERS_GENERATE.equals(channelID)) {
114 Channel channel = getThing().getChannel(CHANNEL_GUEST_VOUCHERS_GENERATE);
115 if (channel == null) {
118 UniFiVoucherChannelConfig config = channel.getConfiguration().as(UniFiVoucherChannelConfig.class);
119 final int count = config.getCount();
120 final int expire = config.getExpiration();
121 final int users = config.getVoucherUsers();
122 final Integer upLimit = config.getUpLimit();
123 final Integer downLimit = config.getDownLimit();
124 final Integer dataQuota = config.getDataQuota();
125 controller.generateGuestVouchers(entity, count, expire, users, upLimit, downLimit, dataQuota);