2 * Copyright (c) 2010-2023 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;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.JsonObject;
42 * @author Michael Geramb - Initial contribution
45 public abstract class HandlerBase {
47 private final Logger logger = LoggerFactory.getLogger(HandlerBase.class);
49 protected SmartHomeDeviceHandler smartHomeDeviceHandler;
50 protected Map<String, ChannelInfo> channels = new HashMap<>();
52 public HandlerBase(SmartHomeDeviceHandler smartHomeDeviceHandler) {
53 this.smartHomeDeviceHandler = smartHomeDeviceHandler;
56 protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
58 public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
60 public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
61 List<SmartHomeCapability> capabilities, String channelId, Command command)
62 throws IOException, InterruptedException;
64 public abstract @Nullable StateDescription findStateDescription(String channelId,
65 StateDescription originalStateDescription, @Nullable Locale locale);
67 public boolean hasChannel(String channelId) {
68 return channels.containsKey(channelId);
71 public abstract String[] getSupportedInterface();
73 SmartHomeDeviceHandler getSmartHomeDeviceHandler() {
74 return smartHomeDeviceHandler;
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,
84 logger.trace("{} | {}", capability.toString(), supported.toString());
85 for (Property property : supported) {
86 String name = property.name;
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);
101 this.channels = channels;
102 return channels.values();
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,
111 if (supported.stream().anyMatch(p -> propertyName.equals(p.name))) {
119 public void updateState(String channelId, State state) {
120 getSmartHomeDeviceHandler().updateState(channelId, state);
123 public static class ChannelInfo {
124 public final String propertyName;
125 public final String channelId;
126 public final String itemType;
127 public ChannelTypeUID channelTypeUID;
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;
137 public static class UpdateChannelResult {
138 public boolean needSingleUpdate;