2 * Copyright (c) 2010-2020 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;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.amazonechocontrol.internal.Connection;
25 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.Properties;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.Property;
28 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
29 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.StateDescription;
35 import com.google.gson.JsonObject;
38 * @author Michael Geramb - Initial contribution
41 public abstract class HandlerBase {
42 protected @Nullable SmartHomeDeviceHandler smartHomeDeviceHandler;
43 protected Map<String, ChannelInfo> channels = new HashMap<>();
45 protected abstract ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property);
47 public abstract void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result);
49 public abstract boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
50 SmartHomeCapability[] capabilties, String channelId, Command command)
51 throws IOException, InterruptedException;
53 public abstract @Nullable StateDescription findStateDescription(String channelId,
54 StateDescription originalStateDescription, @Nullable Locale locale);
56 public boolean hasChannel(String channelId) {
57 return channels.containsKey(channelId);
60 public abstract String[] getSupportedInterface();
62 SmartHomeDeviceHandler getSmartHomeDeviceHandler() throws IllegalStateException {
63 SmartHomeDeviceHandler smartHomeDeviceHandler = this.smartHomeDeviceHandler;
64 if (smartHomeDeviceHandler == null) {
65 throw new IllegalStateException("Handler not initialized");
67 return smartHomeDeviceHandler;
70 public Collection<ChannelInfo> initialize(SmartHomeDeviceHandler smartHomeDeviceHandler,
71 List<SmartHomeCapability> capabilities) {
72 this.smartHomeDeviceHandler = smartHomeDeviceHandler;
73 Map<String, ChannelInfo> channels = new HashMap<>();
74 for (SmartHomeCapability capability : capabilities) {
75 Properties properties = capability.properties;
76 if (properties != null) {
77 Property @Nullable [] supported = properties.supported;
78 if (supported != null) {
79 for (Property property : supported) {
80 if (property != null) {
81 String name = property.name;
83 ChannelInfo[] channelInfos = findChannelInfos(capability, name);
84 if (channelInfos != null) {
85 for (ChannelInfo channelInfo : channelInfos) {
86 if (channelInfo != null) {
87 channels.put(channelInfo.channelId, channelInfo);
97 this.channels = channels;
98 return channels.values();
101 protected boolean containsCapabilityProperty(SmartHomeCapability[] capabilties, String propertyName) {
102 for (SmartHomeCapability capability : capabilties) {
103 Properties properties = capability.properties;
104 if (properties != null) {
105 Property @Nullable [] supportedProperties = properties.supported;
106 if (supportedProperties != null) {
107 for (Property property : supportedProperties) {
108 if (property != null) {
109 if (propertyName != null && propertyName.equals(property.name)) {
120 public void updateState(String channelId, State state) {
121 getSmartHomeDeviceHandler().updateState(channelId, state);
124 public static class ChannelInfo {
125 public final String propertyName;
126 public final String channelId;
127 public final String itemType;
128 public ChannelTypeUID channelTypeUID;
130 public ChannelInfo(String propertyName, String channelId, ChannelTypeUID channelTypeUID, String itemType) {
131 this.propertyName = propertyName;
132 this.channelId = channelId;
133 this.itemType = itemType;
134 this.channelTypeUID = channelTypeUID;
138 public static class UpdateChannelResult {
139 public boolean needSingleUpdate;