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