]> git.basschouten.com Git - openhab-addons.git/blob
009aa174a6c91c20492d300356fc24259751f8de
[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) throws IOException;
51
52     public abstract @Nullable StateDescription findStateDescription(String channelId,
53             StateDescription originalStateDescription, @Nullable Locale locale);
54
55     public boolean hasChannel(String channelId) {
56         return channels.containsKey(channelId);
57     }
58
59     public abstract String[] getSupportedInterface();
60
61     SmartHomeDeviceHandler getSmartHomeDeviceHandler() throws IllegalStateException {
62         SmartHomeDeviceHandler smartHomeDeviceHandler = this.smartHomeDeviceHandler;
63         if (smartHomeDeviceHandler == null) {
64             throw new IllegalStateException("Handler not initialized");
65         }
66         return smartHomeDeviceHandler;
67     }
68
69     public Collection<ChannelInfo> initialize(SmartHomeDeviceHandler smartHomeDeviceHandler,
70             List<SmartHomeCapability> capabilities) {
71         this.smartHomeDeviceHandler = smartHomeDeviceHandler;
72         Map<String, ChannelInfo> channels = new HashMap<>();
73         for (SmartHomeCapability capability : capabilities) {
74             Properties properties = capability.properties;
75             if (properties != null) {
76                 Property @Nullable [] supported = properties.supported;
77                 if (supported != null) {
78                     for (Property property : supported) {
79                         if (property != null) {
80                             String name = property.name;
81                             if (name != null) {
82                                 ChannelInfo[] channelInfos = findChannelInfos(capability, name);
83                                 if (channelInfos != null) {
84                                     for (ChannelInfo channelInfo : channelInfos) {
85                                         if (channelInfo != null) {
86                                             channels.put(channelInfo.channelId, channelInfo);
87                                         }
88                                     }
89                                 }
90                             }
91                         }
92                     }
93                 }
94             }
95         }
96         this.channels = channels;
97         return channels.values();
98     }
99
100     protected boolean containsCapabilityProperty(SmartHomeCapability[] capabilties, String propertyName) {
101         for (SmartHomeCapability capability : capabilties) {
102             Properties properties = capability.properties;
103             if (properties != null) {
104                 Property @Nullable [] supportedProperties = properties.supported;
105                 if (supportedProperties != null) {
106                     for (Property property : supportedProperties) {
107                         if (property != null) {
108                             if (propertyName != null && propertyName.equals(property.name)) {
109                                 return true;
110                             }
111                         }
112                     }
113                 }
114             }
115         }
116         return false;
117     }
118
119     public void updateState(String channelId, State state) {
120         getSmartHomeDeviceHandler().updateState(channelId, state);
121     }
122
123     public static class ChannelInfo {
124         public final String propertyName;
125         public final String channelId;
126         public final String itemType;
127         public ChannelTypeUID channelTypeUID;
128
129         public ChannelInfo(String propertyName, String channelId, ChannelTypeUID channelTypeUID, String itemType) {
130             this.propertyName = propertyName;
131             this.channelId = channelId;
132             this.itemType = itemType;
133             this.channelTypeUID = channelTypeUID;
134         }
135     }
136
137     public static class UpdateChannelResult {
138         public boolean needSingleUpdate;
139     }
140 }