]> git.basschouten.com Git - openhab-addons.git/blob
e578bc8e1ccd036edeaa61b5204709c1e56c4a43
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.amazonechocontrol.internal.smarthome;
14
15 import java.io.IOException;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.amazonechocontrol.internal.Connection;
25 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.Properties;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.Property;
28 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
29 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.StateDescription;
34
35 import com.google.gson.JsonObject;
36
37 /**
38  * @author Michael Geramb - Initial contribution
39  */
40 @NonNullByDefault
41 public abstract class HandlerBase {
42     protected @Nullable SmartHomeDeviceHandler smartHomeDeviceHandler;
43     protected Map<String, ChannelInfo> channels = new HashMap<>();
44
45     protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
46
47     public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
48
49     public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
50             SmartHomeCapability[] capabilties, String channelId, Command command)
51             throws IOException, InterruptedException;
52
53     public abstract @Nullable StateDescription findStateDescription(String channelId,
54             StateDescription originalStateDescription, @Nullable Locale locale);
55
56     public boolean hasChannel(String channelId) {
57         return channels.containsKey(channelId);
58     }
59
60     public abstract String[] getSupportedInterface();
61
62     SmartHomeDeviceHandler getSmartHomeDeviceHandler() throws IllegalStateException {
63         SmartHomeDeviceHandler smartHomeDeviceHandler = this.smartHomeDeviceHandler;
64         if (smartHomeDeviceHandler == null) {
65             throw new IllegalStateException("Handler not initialized");
66         }
67         return smartHomeDeviceHandler;
68     }
69
70     public Collection<ChannelInfo> initialize(SmartHomeDeviceHandler smartHomeDeviceHandler,
71             List<SmartHomeCapability> capabilities) {
72         this.smartHomeDeviceHandler = smartHomeDeviceHandler;
73         Map<String, ChannelInfo> channels = new HashMap<>();
74         for (SmartHomeCapability capability : capabilities) {
75             Properties properties = capability.properties;
76             if (properties != null) {
77                 Property @Nullable [] supported = properties.supported;
78                 if (supported != null) {
79                     for (Property property : supported) {
80                         if (property != null) {
81                             String name = property.name;
82                             if (name != null) {
83                                 ChannelInfo[] channelInfos = findChannelInfos(capability, name);
84                                 if (channelInfos != null) {
85                                     for (ChannelInfo channelInfo : channelInfos) {
86                                         if (channelInfo != null) {
87                                             channels.put(channelInfo.channelId, channelInfo);
88                                         }
89                                     }
90                                 }
91                             }
92                         }
93                     }
94                 }
95             }
96         }
97         this.channels = channels;
98         return channels.values();
99     }
100
101     protected boolean containsCapabilityProperty(SmartHomeCapability[] capabilties, String propertyName) {
102         for (SmartHomeCapability capability : capabilties) {
103             Properties properties = capability.properties;
104             if (properties != null) {
105                 Property @Nullable [] supportedProperties = properties.supported;
106                 if (supportedProperties != null) {
107                     for (Property property : supportedProperties) {
108                         if (property != null) {
109                             if (propertyName != null && propertyName.equals(property.name)) {
110                                 return true;
111                             }
112                         }
113                     }
114                 }
115             }
116         }
117         return false;
118     }
119
120     public void updateState(String channelId, State state) {
121         getSmartHomeDeviceHandler().updateState(channelId, state);
122     }
123
124     public static class ChannelInfo {
125         public final String propertyName;
126         public final String channelId;
127         public final String itemType;
128         public ChannelTypeUID channelTypeUID;
129
130         public ChannelInfo(String propertyName, String channelId, ChannelTypeUID channelTypeUID, String itemType) {
131             this.propertyName = propertyName;
132             this.channelId = channelId;
133             this.itemType = itemType;
134             this.channelTypeUID = channelTypeUID;
135         }
136     }
137
138     public static class UpdateChannelResult {
139         public boolean needSingleUpdate;
140     }
141 }