]> git.basschouten.com Git - openhab-addons.git/blob
aa4e294981d1747b61ec4f908100912d4816d2c4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
19 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
20 import org.openhab.binding.netatmo.internal.servlet.WebhookServlet;
21
22 /**
23  * {@link EventCapability} is the base class for handlers subject to receive event notifications.
24  * This class registers to NetatmoServletService so it can be notified when an event arrives.
25  *
26  * @author GaĆ«l L'hopital - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public class EventCapability extends Capability {
31     private Optional<WebhookServlet> webhook = Optional.empty();
32
33     public EventCapability(CommonInterface handler) {
34         super(handler);
35     }
36
37     @Override
38     public void initialize() {
39         ApiBridgeHandler accountHandler = handler.getAccountHandler();
40         if (accountHandler != null) {
41             webhook = accountHandler.getWebHookServlet();
42             webhook.ifPresent(servlet -> servlet.registerDataListener(handler.getId(), this));
43         }
44     }
45
46     @Override
47     public void dispose() {
48         webhook.ifPresent(servlet -> servlet.unregisterDataListener(handler.getId()));
49     }
50 }