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) throws IOException;
52 public abstract @Nullable StateDescription findStateDescription(String channelId,
53 StateDescription originalStateDescription, @Nullable Locale locale);
55 public boolean hasChannel(String channelId) {
56 return channels.containsKey(channelId);
59 public abstract String[] getSupportedInterface();
61 SmartHomeDeviceHandler getSmartHomeDeviceHandler() throws IllegalStateException {
62 SmartHomeDeviceHandler smartHomeDeviceHandler = this.smartHomeDeviceHandler;
63 if (smartHomeDeviceHandler == null) {
64 throw new IllegalStateException("Handler not initialized");
66 return smartHomeDeviceHandler;
69 public Collection<ChannelInfo> initialize(SmartHomeDeviceHandler smartHomeDeviceHandler,
70 List<SmartHomeCapability> capabilities) {
71 this.smartHomeDeviceHandler = smartHomeDeviceHandler;
72 Map<String, ChannelInfo> channels = new HashMap<>();
73 for (SmartHomeCapability capability : capabilities) {
74 Properties properties = capability.properties;
75 if (properties != null) {
76 Property @Nullable [] supported = properties.supported;
77 if (supported != null) {
78 for (Property property : supported) {
79 if (property != null) {
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);
96 this.channels = channels;
97 return channels.values();
100 protected boolean containsCapabilityProperty(SmartHomeCapability[] capabilties, String propertyName) {
101 for (SmartHomeCapability capability : capabilties) {
102 Properties properties = capability.properties;
103 if (properties != null) {
104 Property @Nullable [] supportedProperties = properties.supported;
105 if (supportedProperties != null) {
106 for (Property property : supportedProperties) {
107 if (property != null) {
108 if (propertyName != null && propertyName.equals(property.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;