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.Collection;
18 import java.util.Collections;
19 import java.util.function.Predicate;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.unifi.internal.UniFiSiteThingConfig;
24 import org.openhab.binding.unifi.internal.UniFiVoucherChannelConfig;
25 import org.openhab.binding.unifi.internal.action.UniFiSiteActions;
26 import org.openhab.binding.unifi.internal.api.UniFiController;
27 import org.openhab.binding.unifi.internal.api.UniFiException;
28 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
29 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
30 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.thing.Channel;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.State;
42 import org.openhab.core.types.UnDefType;
45 * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
46 * updates for {@link UniFiSite} instances.
48 * @author Matthew Bowman - Initial contribution
49 * @author Hilbrand Bouwkamp - Initial contribution
50 * @author Mark Herwege - Added guest vouchers
53 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
55 private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
57 public UniFiSiteThingHandler(final Thing thing) {
62 protected boolean initialize(final UniFiSiteThingConfig config) {
64 if (!config.isValid()) {
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66 "@text/error.thing.site.offline.configuration_error");
73 protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
74 return cache.getSite(config.getSiteID());
78 protected State getChannelState(final UniFiSite site, final String channelId) {
82 case CHANNEL_TOTAL_CLIENTS:
83 state = countClients(site, c -> true);
85 case CHANNEL_WIRELESS_CLIENTS:
86 state = countClients(site, c -> c.isWireless());
88 case CHANNEL_WIRED_CLIENTS:
89 state = countClients(site, c -> c.isWired());
91 case CHANNEL_GUEST_CLIENTS:
92 state = countClients(site, c -> c.isGuest());
94 case CHANNEL_GUEST_VOUCHER:
95 final String voucher = site.getVoucher();
96 state = (voucher != null) ? StringType.valueOf(voucher) : UnDefType.UNDEF;
98 case CHANNEL_GUEST_VOUCHERS_GENERATE:
99 state = OnOffType.OFF;
102 // Unsupported channel; nothing to update
103 return UnDefType.NULL;
108 private static State countClients(final UniFiSite site, final Predicate<UniFiClient> filter) {
109 return new DecimalType(site.getCache().countClients(site, filter));
113 protected boolean handleCommand(final UniFiController controller, final UniFiSite entity,
114 final ChannelUID channelUID, final Command command) throws UniFiException {
115 final String channelID = channelUID.getId();
117 if (CHANNEL_GUEST_VOUCHERS_GENERATE.equals(channelID)) {
118 final Channel channel = getThing().getChannel(CHANNEL_GUEST_VOUCHERS_GENERATE);
119 if (channel == null) {
122 final UniFiVoucherChannelConfig config = channel.getConfiguration().as(UniFiVoucherChannelConfig.class);
123 final int count = config.getCount();
124 final int expire = config.getExpiration();
125 final int users = config.getVoucherUsers();
126 final Integer upLimit = config.getUpLimit();
127 final Integer downLimit = config.getDownLimit();
128 final Integer dataQuota = config.getDataQuota();
129 controller.generateVouchers(entity, count, expire, users, upLimit, downLimit, dataQuota);
136 public Collection<Class<? extends ThingHandlerService>> getServices() {
137 return Collections.singleton(UniFiSiteActions.class);