2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.amazonechocontrol.internal.smarthome;
15 import java.io.IOException;
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;
32 import com.google.gson.JsonObject;
35 * @author Michael Geramb - Initial contribution
38 public abstract class HandlerBase {
39 protected SmartHomeDeviceHandler smartHomeDeviceHandler;
40 protected Map<String, ChannelInfo> channels = new HashMap<>();
42 public HandlerBase(SmartHomeDeviceHandler smartHomeDeviceHandler) {
43 this.smartHomeDeviceHandler = smartHomeDeviceHandler;
46 protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
48 public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
50 public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
51 List<SmartHomeCapability> capabilities, String channelId, Command command)
52 throws IOException, InterruptedException;
54 public abstract @Nullable StateDescription findStateDescription(String channelId,
55 StateDescription originalStateDescription, @Nullable Locale locale);
57 public boolean hasChannel(String channelId) {
58 return channels.containsKey(channelId);
61 public abstract String[] getSupportedInterface();
63 SmartHomeDeviceHandler getSmartHomeDeviceHandler() {
64 return smartHomeDeviceHandler;
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,
74 for (Property property : supported) {
75 String name = property.name;
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);
90 this.channels = channels;
91 return channels.values();
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,
100 if (supported.stream().anyMatch(p -> propertyName.equals(p.name))) {
108 public void updateState(String channelId, State state) {
109 getSmartHomeDeviceHandler().updateState(channelId, state);
112 public static class ChannelInfo {
113 public final String propertyName;
114 public final String channelId;
115 public final String itemType;
116 public ChannelTypeUID channelTypeUID;
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;
126 public static class UpdateChannelResult {
127 public boolean needSingleUpdate;