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