]> git.basschouten.com Git - openhab-addons.git/blob
293d7ff349cbce2dee904a9917c56fb3cb52f9e2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.netatmo.internal.handler.capability;
14
15 import java.util.List;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
20 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
21 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
22 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventChannelHelper;
23 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
24 import org.openhab.binding.netatmo.internal.servlet.WebhookServlet;
25
26 /**
27  * {@link HomeSecurityThingCapability} is the ancestor of capabilities hosted by a security home
28  * e.g. person and camera capabilities
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class HomeSecurityThingCapability extends Capability {
35     protected final NetatmoDescriptionProvider descriptionProvider;
36     protected final EventChannelHelper eventHelper;
37
38     private Optional<WebhookServlet> webhook = Optional.empty();
39     private Optional<SecurityCapability> securityCapability = Optional.empty();
40     private Optional<HomeCapability> homeCapability = Optional.empty();
41
42     public HomeSecurityThingCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
43             List<ChannelHelper> channelHelpers) {
44         super(handler);
45         this.descriptionProvider = descriptionProvider;
46         this.eventHelper = (EventChannelHelper) channelHelpers.stream().filter(c -> c instanceof EventChannelHelper)
47                 .findFirst().orElseThrow(() -> new IllegalArgumentException(
48                         "HomeSecurityThingCapability must find an EventChannelHelper, please file a bug report."));
49         eventHelper.setModuleType(moduleType);
50     }
51
52     protected Optional<SecurityCapability> getSecurityCapability() {
53         if (securityCapability.isEmpty()) {
54             securityCapability = handler.getHomeCapability(SecurityCapability.class);
55             ApiBridgeHandler accountHandler = handler.getAccountHandler();
56             if (accountHandler != null) {
57                 webhook = accountHandler.getWebHookServlet();
58                 webhook.ifPresent(servlet -> servlet.registerDataListener(handler.getId(), this));
59             }
60         }
61         return securityCapability;
62     }
63
64     protected Optional<HomeCapability> getHomeCapability() {
65         if (homeCapability.isEmpty()) {
66             homeCapability = handler.getHomeCapability(HomeCapability.class);
67         }
68         return homeCapability;
69     }
70
71     @Override
72     public void dispose() {
73         webhook.ifPresent(servlet -> servlet.unregisterDataListener(handler.getId()));
74         super.dispose();
75     }
76 }