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