]> git.basschouten.com Git - openhab-addons.git/blob
91cac1917649ace9483b8220b64b95900e1dba2e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.gson.JsonObject;
40
41 /**
42  * @author Michael Geramb - Initial contribution
43  */
44 @NonNullByDefault
45 public abstract class HandlerBase {
46     // Logger
47     private final Logger logger = LoggerFactory.getLogger(HandlerBase.class);
48
49     protected SmartHomeDeviceHandler smartHomeDeviceHandler;
50     protected Map<String, ChannelInfo> channels = new HashMap<>();
51
52     public HandlerBase(SmartHomeDeviceHandler smartHomeDeviceHandler) {
53         this.smartHomeDeviceHandler = smartHomeDeviceHandler;
54     }
55
56     protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
57
58     public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
59
60     public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
61             List<SmartHomeCapability> capabilities, String channelId, Command command)
62             throws IOException, InterruptedException;
63
64     public abstract @Nullable StateDescription findStateDescription(String channelId,
65             StateDescription originalStateDescription, @Nullable Locale locale);
66
67     public boolean hasChannel(String channelId) {
68         return channels.containsKey(channelId);
69     }
70
71     public abstract String[] getSupportedInterface();
72
73     SmartHomeDeviceHandler getSmartHomeDeviceHandler() {
74         return smartHomeDeviceHandler;
75     }
76
77     public Collection<ChannelInfo> initialize(List<SmartHomeCapability> capabilities) {
78         Map<String, ChannelInfo> channels = new HashMap<>();
79         for (SmartHomeCapability capability : capabilities) {
80             Properties properties = capability.properties;
81             if (properties != null) {
82                 List<JsonSmartHomeCapabilities.Property> supported = Objects.requireNonNullElse(properties.supported,
83                         List.of());
84                 logger.trace("{} | {}", capability.toString(), supported.toString());
85                 for (Property property : supported) {
86                     String name = property.name;
87                     if (name != null) {
88                         ChannelInfo[] channelInfos = findChannelInfos(capability, name);
89                         if (channelInfos != null) {
90                             for (ChannelInfo channelInfo : channelInfos) {
91                                 if (channelInfo != null) {
92                                     channels.put(channelInfo.channelId, channelInfo);
93                                 }
94                             }
95                         }
96                     }
97
98                 }
99             }
100         }
101         this.channels = channels;
102         return channels.values();
103     }
104
105     protected boolean containsCapabilityProperty(List<SmartHomeCapability> capabilities, String propertyName) {
106         for (SmartHomeCapability capability : capabilities) {
107             Properties properties = capability.properties;
108             if (properties != null) {
109                 List<JsonSmartHomeCapabilities.Property> supported = Objects.requireNonNullElse(properties.supported,
110                         List.of());
111                 if (supported.stream().anyMatch(p -> propertyName.equals(p.name))) {
112                     return true;
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 }