]> git.basschouten.com Git - openhab-addons.git/blob
2be37f227f599a62f5f8401acce450d3075914f5
[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.wemo.internal.handler;
14
15 import java.net.URL;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.wemo.internal.WemoBindingConstants;
20 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
21 import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
22 import org.openhab.core.io.transport.upnp.UpnpIOService;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.types.Command;
27
28 /**
29  * {@link WemoBaseThingHandler} provides a base implementation for the
30  * concrete WeMo handlers for each thing type.
31  *
32  * @author Jacob Laursen - Initial contribution
33  */
34 @NonNullByDefault
35 public class WemoBaseThingHandler extends BaseThingHandler implements UpnpIOParticipant {
36
37     protected @Nullable UpnpIOService service;
38     protected WemoHttpCall wemoHttpCaller;
39     protected String host = "";
40
41     public WemoBaseThingHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
42         super(thing);
43         this.service = upnpIOService;
44         this.wemoHttpCaller = wemoHttpCaller;
45     }
46
47     @Override
48     public void initialize() {
49         // can be overridden by subclasses
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         // can be overridden by subclasses
55     }
56
57     @Override
58     public void onStatusChanged(boolean status) {
59         // can be overridden by subclasses
60     }
61
62     @Override
63     public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
64         // can be overridden by subclasses
65     }
66
67     @Override
68     public void onServiceSubscribed(@Nullable String service, boolean succeeded) {
69         // can be overridden by subclasses
70     }
71
72     @Override
73     public @Nullable String getUDN() {
74         return (String) this.getThing().getConfiguration().get(WemoBindingConstants.UDN);
75     }
76
77     protected boolean isUpnpDeviceRegistered() {
78         UpnpIOService service = this.service;
79         if (service != null) {
80             return service.isRegistered(this);
81         }
82         return false;
83     }
84
85     protected String getHost() {
86         String localHost = host;
87         if (!localHost.isEmpty()) {
88             return localHost;
89         }
90         UpnpIOService localService = service;
91         if (localService != null) {
92             URL descriptorURL = localService.getDescriptorURL(this);
93             if (descriptorURL != null) {
94                 return descriptorURL.getHost();
95             }
96         }
97         return "";
98     }
99 }