]> git.basschouten.com Git - openhab-addons.git/blob
5011c34d1730375eabf866af8b026e462ae3fe94
[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.*;
16
17 import java.util.Collection;
18 import java.util.Set;
19 import java.util.function.Predicate;
20
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;
43
44 /**
45  * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
46  * updates for {@link UniFiSite} instances.
47  *
48  * @author Matthew Bowman - Initial contribution
49  * @author Hilbrand Bouwkamp - Initial contribution
50  * @author Mark Herwege - Added guest vouchers
51  */
52 @NonNullByDefault
53 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
54
55     private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
56
57     public UniFiSiteThingHandler(final Thing thing) {
58         super(thing);
59     }
60
61     @Override
62     protected boolean initialize(final UniFiSiteThingConfig config) {
63         this.config = config;
64         if (!config.isValid()) {
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66                     "@text/error.thing.site.offline.configuration_error");
67             return false;
68         }
69         return true;
70     }
71
72     @Override
73     protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
74         return cache.getSite(config.getSiteID());
75     }
76
77     @Override
78     protected State getChannelState(final UniFiSite site, final String channelId) {
79         final State state;
80
81         switch (channelId) {
82             case CHANNEL_TOTAL_CLIENTS:
83                 state = countClients(site, c -> true);
84                 break;
85             case CHANNEL_WIRELESS_CLIENTS:
86                 state = countClients(site, c -> c.isWireless());
87                 break;
88             case CHANNEL_WIRED_CLIENTS:
89                 state = countClients(site, c -> c.isWired());
90                 break;
91             case CHANNEL_GUEST_CLIENTS:
92                 state = countClients(site, c -> c.isGuest());
93                 break;
94             case CHANNEL_GUEST_VOUCHER:
95                 final String voucher = site.getVoucher();
96                 state = (voucher != null) ? StringType.valueOf(voucher) : UnDefType.UNDEF;
97                 break;
98             case CHANNEL_GUEST_VOUCHERS_GENERATE:
99                 state = OnOffType.OFF;
100                 break;
101             default:
102                 // Unsupported channel; nothing to update
103                 return UnDefType.NULL;
104         }
105         return state;
106     }
107
108     private static State countClients(final UniFiSite site, final Predicate<UniFiClient> filter) {
109         return new DecimalType(site.getCache().countClients(site, filter));
110     }
111
112     @Override
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();
116
117         if (CHANNEL_GUEST_VOUCHERS_GENERATE.equals(channelID)) {
118             final Channel channel = getThing().getChannel(CHANNEL_GUEST_VOUCHERS_GENERATE);
119             if (channel == null) {
120                 return false;
121             }
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);
130             return true;
131         }
132         return false;
133     }
134
135     @Override
136     public Collection<Class<? extends ThingHandlerService>> getServices() {
137         return Set.of(UniFiSiteActions.class);
138     }
139 }