2 * Copyright (c) 2010-2022 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;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Locale;
21 import java.util.Objects;
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;
37 import com.google.gson.JsonObject;
40 * @author Michael Geramb - Initial contribution
43 public abstract class HandlerBase {
44 protected SmartHomeDeviceHandler smartHomeDeviceHandler;
45 protected Map<String, ChannelInfo> channels = new HashMap<>();
47 public HandlerBase(SmartHomeDeviceHandler smartHomeDeviceHandler) {
48 this.smartHomeDeviceHandler = smartHomeDeviceHandler;
51 protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
53 public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
55 public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
56 List<SmartHomeCapability> capabilities, String channelId, Command command)
57 throws IOException, InterruptedException;
59 public abstract @Nullable StateDescription findStateDescription(String channelId,
60 StateDescription originalStateDescription, @Nullable Locale locale);
62 public boolean hasChannel(String channelId) {
63 return channels.containsKey(channelId);
66 public abstract String[] getSupportedInterface();
68 SmartHomeDeviceHandler getSmartHomeDeviceHandler() {
69 return smartHomeDeviceHandler;
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,
79 for (Property property : supported) {
80 String name = property.name;
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);
95 this.channels = channels;
96 return channels.values();
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,
105 if (supported.stream().anyMatch(p -> propertyName.equals(p.name))) {
113 public void updateState(String channelId, State state) {
114 getSmartHomeDeviceHandler().updateState(channelId, state);
117 public static class ChannelInfo {
118 public final String propertyName;
119 public final String channelId;
120 public final String itemType;
121 public ChannelTypeUID channelTypeUID;
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;
131 public static class UpdateChannelResult {
132 public boolean needSingleUpdate;