diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsColorConverter.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsColorConverter.java index c4a94d4eb92e2..ba219890910bf 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsColorConverter.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsColorConverter.java @@ -40,32 +40,43 @@ public class SmartthingsColorConverter extends SmartthingsConverter { private final Logger logger = LoggerFactory.getLogger(SmartthingsColorConverter.class); - public SmartthingsColorConverter(SmartthingsTypeRegistry typeRegistry, Thing thing) { - super(typeRegistry, thing); + public SmartthingsColorConverter(SmartthingsTypeRegistry typeRegistry) { + super(typeRegistry); } @Override - public String convertToSmartthings(Thing thing, ChannelUID channelUid, Command command) { + public void convertToSmartthingsInternal(Thing thing, ChannelUID channelUid, Command command) { String jsonMsg; if (command instanceof HSBType hsbCommand) { double hue = hsbCommand.getHue().doubleValue() / 3.60; + double sat = hsbCommand.getSaturation().doubleValue(); + int level = hsbCommand.getBrightness().intValue(); String componentKey = "main"; String capaKey = "colorControl"; String cmdName = "setColor"; - String arguments = String.format("[ { 'hue':%s, 'saturation':%s }]", hue, - hsbCommand.getSaturation().intValue()); + Object[] arguments = new Object[1]; + ColorObject colorObj = new ColorObject(); + colorObj.hue = hue; + colorObj.saturation = sat; + arguments[0] = colorObj; - jsonMsg = String.format( - "{'commands': [{'component': '%s', 'capability': '%s', 'command': '%s', 'arguments': %s }]}", - componentKey, capaKey, cmdName, arguments); + this.pushCommand(componentKey, capaKey, cmdName, arguments); + + // setLevel is not working correctly on colorControl object + // call setLevel of switchLevel instead + arguments = new Object[1]; + arguments[0] = level; + this.pushCommand(componentKey, "switchLevel", "setLevel", arguments); - } else { - jsonMsg = defaultConvertToSmartthings(thing, channelUid, command); } - return jsonMsg; + } + + private class ColorObject { + public Double hue = 0.0; + public Double saturation = 0.0; } /* @@ -75,7 +86,7 @@ public String convertToSmartthings(Thing thing, ChannelUID channelUid, Command c * org.openhab.binding.smartthings.internal.SmartthingsStateData) */ @Override - public State convertToOpenHab(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { + public State convertToOpenHabInternal(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { // The color value from Smartthings will look like "#123456" which is the RGB color // This needs to be converted into HSB type if (dataFromSmartthings == null) { diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverter.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverter.java index 34435bea09f95..a814dcedf730e 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverter.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverter.java @@ -12,39 +12,29 @@ */ package org.openhab.binding.smartthings.internal.converter; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.binding.smartthings.internal.dto.SmartthingsArgument; -import org.openhab.binding.smartthings.internal.dto.SmartthingsAttribute; -import org.openhab.binding.smartthings.internal.dto.SmartthingsCapabilitie; -import org.openhab.binding.smartthings.internal.dto.SmartthingsCommand; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.smartthings.internal.type.SmartthingsTypeRegistry; -import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; -import org.openhab.core.library.types.HSBType; -import org.openhab.core.library.types.IncreaseDecreaseType; -import org.openhab.core.library.types.NextPreviousType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OpenClosedType; import org.openhab.core.library.types.PercentType; -import org.openhab.core.library.types.PlayPauseType; -import org.openhab.core.library.types.PointType; -import org.openhab.core.library.types.RewindFastforwardType; -import org.openhab.core.library.types.StopMoveType; -import org.openhab.core.library.types.StringListType; import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.UpDownType; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; import org.openhab.core.types.Command; -import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; import org.openhab.core.types.UnDefType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; import com.google.gson.internal.LinkedTreeMap; /** @@ -59,135 +49,68 @@ public abstract class SmartthingsConverter { private final Logger logger = LoggerFactory.getLogger(SmartthingsConverter.class); - protected String thingTypeId; protected SmartthingsTypeRegistry typeRegistry; + private Gson gson = new Gson(); - SmartthingsConverter(SmartthingsTypeRegistry typeRegistry, Thing thing) { - thingTypeId = thing.getThingTypeUID().getId(); + SmartthingsConverter(SmartthingsTypeRegistry typeRegistry) { this.typeRegistry = typeRegistry; } - public abstract String convertToSmartthings(Thing thing, ChannelUID channelUid, Command command); + public String convertToSmartthings(Thing thing, ChannelUID channelUid, Command command) { + convertToSmartthingsInternal(thing, channelUid, command); + String jsonMsg = getJSonCommands(); + return jsonMsg; + } - public abstract State convertToOpenHab(Thing thing, ChannelUID channelUid, Object dataFromSmartthings); + public State convertToOpenHab(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { + State result = convertToOpenHabInternal(thing, channelUid, dataFromSmartthings); + return result; + } - /** - * Provide a default converter in the base call so it can be used in sub-classes if needed - * - * @param command - * @return The json string to send to Smartthings - */ - protected String defaultConvertToSmartthings(Thing thing, ChannelUID channelUid, Command command) { - String value; + public abstract void convertToSmartthingsInternal(Thing thing, ChannelUID channelUid, Command command); - if (command instanceof DateTimeType dateTimeCommand) { - value = dateTimeCommand.format("%m/%d/%Y %H.%M.%S"); - } else if (command instanceof HSBType hsbCommand) { - value = String.format("[%d, %d, %d ]", hsbCommand.getHue().intValue(), - hsbCommand.getSaturation().intValue(), hsbCommand.getBrightness().intValue()); - } else if (command instanceof DecimalType) { - value = command.toString(); - } else if (command instanceof IncreaseDecreaseType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof NextPreviousType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof OnOffType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof OpenClosedType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof PercentType) { - value = command.toString(); - } else if (command instanceof PointType) { // There is not a comparable type in Smartthings, log and send value - logger.warn( - "Warning - PointType Command is not supported by Smartthings. Please configure to use a different command type. CapabilityKey: {}, capabilityAttribute {}", - thingTypeId, channelUid.getId()); - value = command.toFullString(); - } else if (command instanceof RefreshType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof RewindFastforwardType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof StopMoveType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof PlayPauseType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else if (command instanceof StringListType) { - value = command.toString(); - } else if (command instanceof StringType) { - value = command.toString(); - } else if (command instanceof UpDownType) { // Need to surround with double quotes - value = command.toString().toLowerCase(); - } else { - logger.warn( - "Warning - The Smartthings converter does not know how to handle the {} command. The Smartthingsonverter class should be updated. CapabilityKey: {}, capabilityAttribute {}", - command.getClass().getName(), thingTypeId, channelUid.getId()); - value = command.toString().toLowerCase(); - } + public abstract State convertToOpenHabInternal(Thing thing, ChannelUID channelUid, Object dataFromSmartthings); - String jsonMsg = ""; + private SmartthingsActions smartthingsActions = new SmartthingsActions(); - Channel channel = thing.getChannel(channelUid); - Map properties = channel.getProperties(); - String componentKey = properties.get("component"); - String capaKey = properties.get("capability"); - String attrKey = properties.get("attribute"); + private class SmartthingsActions { + Queue commands = new LinkedList(); - SmartthingsCapabilitie capa = null; - SmartthingsAttribute attr = null; - if (capaKey != null) { - capa = typeRegistry.getCapabilities(capaKey); - } - if (attrKey != null) { - attr = capa.attributes.get(attrKey); - } - - Boolean extendedFormat = false; - String cmdName = ""; - String arguments = "[ "; - - if (capa != null && attr != null) { - if (attrKey.equals("color")) { - attr.setter = "setColor"; - } - - if (attr.setter != null) { - SmartthingsCommand cmd = capa.commands.get(attr.setter); - cmdName = cmd.name; - int i = 0; - for (SmartthingsArgument arg : cmd.arguments) { - if (arg.optional) { - continue; - } - if (i > 0) { - arguments = arguments + " ,"; - } - arguments += value; - i++; - } - arguments = arguments + "]"; - extendedFormat = true; - } + public void pushCommand(@Nullable String component, @Nullable String capability, @Nullable String cmdName, + Object @Nullable [] arguments) { + commands.add(new SmartthingsAction(component, capability, cmdName, arguments)); } + } - if (extendedFormat) { - jsonMsg = String.format( - "{'commands': [{'component': '%s', 'capability': '%s', 'command': '%s', 'arguments': %s }]}", - componentKey, capaKey, cmdName, arguments); - } else { - jsonMsg = String.format("{'commands': [{'component': '%s', 'capability': '%s', 'command': '%s'}]}", - componentKey, capaKey, value); + private class SmartthingsAction { + @Nullable + public String component; + @Nullable + public String capability; + @Nullable + public String command; + @Nullable + public Object @Nullable [] arguments; + + public SmartthingsAction(@Nullable String component, @Nullable String capability, @Nullable String command, + Object @Nullable [] arguments) { + this.component = component; + this.capability = capability; + this.command = command; + this.arguments = arguments; } + } - // switchLevel - // setLevel - // jsonMsg = String.format( - // "{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}", - // thingTypeId, smartthingsName, channelUid.getId(), value); + public void pushCommand(@Nullable String component, @Nullable String capability, @Nullable String command, + Object @Nullable [] arguments) { + smartthingsActions.pushCommand(component, capability, command, arguments); + } - // "{'commands': [{'component': '%s', 'capability': '%s', 'command': 'setLevel', 'arguments': [%s, 2]}]}", - // jsonMsg = String.format("{'commands': [{'component': 'main', 'capability': 'switch', 'command': '%s'}]}", - // value); + private String getJSonCommands() { - return jsonMsg; + String result = gson.toJson(smartthingsActions); + smartthingsActions.commands.clear(); + return result; } protected String surroundWithQuotes(String param) { diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverterFactory.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverterFactory.java new file mode 100644 index 0000000000000..cbdc26b9a2bd0 --- /dev/null +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsConverterFactory.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.smartthings.internal.converter; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.smartthings.internal.type.SmartthingsTypeRegistry; + +/** + * A factory for creating converters based on the itemType. + * + * @author Laurent Arnal - Initial contribution + */ + +@NonNullByDefault +public class SmartthingsConverterFactory { + private static Map converterCache = new HashMap<>(); + + public static void registerConverters(SmartthingsTypeRegistry typeRegistry) { + registerConverter("color", new SmartthingsColorConverter(typeRegistry)); + registerConverter("default", new SmartthingsDefaultConverter(typeRegistry)); + } + + private static void registerConverter(String key, SmartthingsConverter tp) { + converterCache.put(key, tp); + } + + /** + * Returns the converter for an itemType. + */ + public static @Nullable SmartthingsConverter getConverter(String itemType) { + SmartthingsConverter converter = converterCache.get(itemType); + return converter; + } +} diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsDefaultConverter.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsDefaultConverter.java index baca019bf5359..1ad12acf6360b 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsDefaultConverter.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsDefaultConverter.java @@ -12,12 +12,38 @@ */ package org.openhab.binding.smartthings.internal.converter; +import java.util.Map; +import java.util.Stack; + import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.smartthings.internal.dto.SmartthingsArgument; +import org.openhab.binding.smartthings.internal.dto.SmartthingsAttribute; +import org.openhab.binding.smartthings.internal.dto.SmartthingsCapabilitie; +import org.openhab.binding.smartthings.internal.dto.SmartthingsCommand; import org.openhab.binding.smartthings.internal.type.SmartthingsTypeRegistry; +import org.openhab.core.library.types.DateTimeType; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.HSBType; +import org.openhab.core.library.types.IncreaseDecreaseType; +import org.openhab.core.library.types.NextPreviousType; +import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.OpenClosedType; +import org.openhab.core.library.types.PercentType; +import org.openhab.core.library.types.PlayPauseType; +import org.openhab.core.library.types.PointType; +import org.openhab.core.library.types.RewindFastforwardType; +import org.openhab.core.library.types.StopMoveType; +import org.openhab.core.library.types.StringListType; +import org.openhab.core.library.types.StringType; +import org.openhab.core.library.types.UpDownType; +import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; import org.openhab.core.types.Command; +import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This "Converter" is assigned to a channel when a special converter is not needed. @@ -33,18 +59,108 @@ */ @NonNullByDefault public class SmartthingsDefaultConverter extends SmartthingsConverter { + private final Logger logger = LoggerFactory.getLogger(SmartthingsConverter.class); - public SmartthingsDefaultConverter(SmartthingsTypeRegistry typeRegistry, Thing thing) { - super(typeRegistry, thing); + public SmartthingsDefaultConverter(SmartthingsTypeRegistry typeRegistry) { + super(typeRegistry); } @Override - public String convertToSmartthings(Thing thing, ChannelUID channelUid, Command command) { - return defaultConvertToSmartthings(thing, channelUid, command); + public void convertToSmartthingsInternal(Thing thing, ChannelUID channelUid, Command command) { + Object value; + + if (command instanceof DateTimeType dateTimeCommand) { + value = dateTimeCommand.format("%m/%d/%Y %H.%M.%S"); + } else if (command instanceof HSBType hsbCommand) { + value = String.format("[%d, %d, %d ]", hsbCommand.getHue().intValue(), + hsbCommand.getSaturation().intValue(), hsbCommand.getBrightness().intValue()); + } else if (command instanceof DecimalType) { + DecimalType dc = (DecimalType) command; + value = dc.intValue(); + } else if (command instanceof IncreaseDecreaseType) { + value = command.toString().toLowerCase(); + } else if (command instanceof NextPreviousType) { + value = command.toString().toLowerCase(); + } else if (command instanceof OnOffType) { + value = command.toString().toLowerCase(); + } else if (command instanceof OpenClosedType) { + value = command.toString().toLowerCase(); + } else if (command instanceof PercentType) { + value = command.toString(); + } else if (command instanceof PointType) { + logger.warn( + "Warning - PointType Command is not supported by Smartthings. Please configure to use a different command type. CapabilityKey: {}, capabilityAttribute {}", + thing.getThingTypeUID(), channelUid.getId()); + value = command.toFullString(); + } else if (command instanceof RefreshType) { + value = command.toString().toLowerCase(); + } else if (command instanceof RewindFastforwardType) { + value = command.toString().toLowerCase(); + } else if (command instanceof StopMoveType) { + value = command.toString().toLowerCase(); + } else if (command instanceof PlayPauseType) { + value = command.toString().toLowerCase(); + } else if (command instanceof StringListType) { + value = command.toString(); + } else if (command instanceof StringType) { + value = command.toString(); + } else if (command instanceof UpDownType) { + value = command.toString().toLowerCase(); + } else { + logger.warn( + "Warning - The Smartthings converter does not know how to handle the {} command. The Smartthingsonverter class should be updated. CapabilityKey: {}, capabilityAttribute {}", + command.getClass().getName(), thing.getThingTypeUID(), channelUid.getId()); + value = command.toString().toLowerCase(); + } + + String jsonMsg = ""; + + Channel channel = thing.getChannel(channelUid); + Map properties = channel.getProperties(); + String componentKey = properties.get("component"); + String capaKey = properties.get("capability"); + String attrKey = properties.get("attribute"); + + SmartthingsCapabilitie capa = null; + SmartthingsAttribute attr = null; + if (capaKey != null) { + capa = typeRegistry.getCapabilities(capaKey); + } + if (attrKey != null) { + attr = capa.attributes.get(attrKey); + } + + String cmdName = ""; + Object[] arguments = null; + + if (capa != null && attr != null) { + if (attrKey.equals("color")) { + attr.setter = "setColor"; + } + + if (attr.setter != null) { + SmartthingsCommand cmd = capa.commands.get(attr.setter); + cmdName = cmd.name; + + Stack stack = new Stack(); + for (SmartthingsArgument arg : cmd.arguments) { + if (arg.optional) { + continue; + } + stack.push(value); + } + arguments = stack.toArray(); + } else { + cmdName = value.toString(); + } + } + + pushCommand(componentKey, capaKey, cmdName, arguments); + } @Override - public State convertToOpenHab(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { + public State convertToOpenHabInternal(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { return defaultConvertToOpenHab(thing, channelUid, dataFromSmartthings); } } diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsOpenCloseControlConverter.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsOpenCloseControlConverter.java index 0bbc3887e405b..18a6ba6b3ffb3 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsOpenCloseControlConverter.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/converter/SmartthingsOpenCloseControlConverter.java @@ -30,21 +30,21 @@ @NonNullByDefault public class SmartthingsOpenCloseControlConverter extends SmartthingsConverter { - public SmartthingsOpenCloseControlConverter(SmartthingsTypeRegistry typeRegistry, Thing thing) { - super(typeRegistry, thing); + public SmartthingsOpenCloseControlConverter(SmartthingsTypeRegistry typeRegistry) { + super(typeRegistry); } @Override - public String convertToSmartthings(Thing thing, ChannelUID channelUid, Command command) { + public void convertToSmartthingsInternal(Thing thing, ChannelUID channelUid, Command command) { String smartthingsValue = ("open".equals(command.toString().toLowerCase())) ? "open" : "close"; smartthingsValue = surroundWithQuotes(smartthingsValue); - return String.format("{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"value\": %s}", thingTypeId, - "smartthingsName", smartthingsValue); + String msg = String.format("{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"value\": %s}", + thing.getThingTypeUID(), "smartthingsName", smartthingsValue); } @Override - public State convertToOpenHab(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { + public State convertToOpenHabInternal(Thing thing, ChannelUID channelUid, Object dataFromSmartthings) { return defaultConvertToOpenHab(thing, channelUid, dataFromSmartthings); } } diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java index 3ddf925a0c54b..f591afcebeeb0 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.smartthings.internal.handler; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,6 +23,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.smartthings.internal.api.SmartthingsApi; import org.openhab.binding.smartthings.internal.converter.SmartthingsConverter; +import org.openhab.binding.smartthings.internal.converter.SmartthingsConverterFactory; import org.openhab.binding.smartthings.internal.dto.SmartthingsStatus; import org.openhab.binding.smartthings.internal.dto.SmartthingsStatusCapabilities; import org.openhab.binding.smartthings.internal.dto.SmartthingsStatusComponent; @@ -117,9 +116,10 @@ public void refreshDevice(String deviceType, String componentId, String capa, St String groupId = deviceType + "_" + componentId; ChannelUID channelUID = new ChannelUID(this.getThing().getUID(), groupId, channelName); Channel chan = thing.getChannel(channelUID); - SmartthingsConverter converter = converters.get(channelUID); if (converters.containsKey(channelUID)) { + SmartthingsConverter converter = converters.get(channelUID); + State state = converter.convertToOpenHab(thing, channelUID, value); updateState(channelUID, state); } @@ -130,6 +130,7 @@ public void refreshDevice() { SmartthingsCloudBridgeHandler cloudBridge = (SmartthingsCloudBridgeHandler) bridge.getHandler(); SmartthingsApi api = cloudBridge.getSmartthingsApi(); + SmartthingsTypeRegistry typeRegistry = cloudBridge.getSmartthingsTypeRegistry(); Map properties = this.getThing().getProperties(); String deviceId = properties.get("deviceId"); @@ -168,17 +169,20 @@ public void refreshDevice() { @Override public void initialize() { - - if (thing.getLabel().indexOf("Petrole") >= 0) { - logger.info("toto"); - } // Create converters for each channel + + Bridge bridge = getBridge(); + SmartthingsCloudBridgeHandler cloudBridge = (SmartthingsCloudBridgeHandler) bridge.getHandler(); + SmartthingsTypeRegistry typeRegistry = cloudBridge.getSmartthingsTypeRegistry(); + + SmartthingsConverterFactory.registerConverters(typeRegistry); + for (Channel ch : thing.getChannels()) { @Nullable String converterName = ch.getProperties().get(smartthingsConverterName); // Will be null if no explicit converter was specified if (converterName == null || converterName.isEmpty()) { - // A converter was Not specified so use the channel id + // A converter was Not specified so usef the channel id converterName = ch.getProperties().get("attribute"); } @@ -239,38 +243,7 @@ private void pollingCode() { } private @Nullable SmartthingsConverter getConverter(String converterName) { - // Converter name will be a name such as "switch" which has to be converted into the full class name such as - // org.openhab.binding.smartthings.internal.converter.SmartthingsSwitchConveter - StringBuffer converterClassName = new StringBuffer( - "org.openhab.binding.smartthings.internal.converter.Smartthings"); - converterClassName.append(Character.toUpperCase(converterName.charAt(0))); - converterClassName.append(converterName.substring(1)); - converterClassName.append("Converter"); - try { - Constructor constr = Class.forName(converterClassName.toString()) - .getDeclaredConstructor(SmartthingsTypeRegistry.class, Thing.class); - constr.setAccessible(true); - Bridge bridge = getBridge(); - SmartthingsCloudBridgeHandler cloudBridge = (SmartthingsCloudBridgeHandler) bridge.getHandler(); - SmartthingsTypeRegistry typeRegistry = cloudBridge.getSmartthingsTypeRegistry(); - return (SmartthingsConverter) constr.newInstance(typeRegistry, thing); - } catch (ClassNotFoundException e) { - // Most of the time there is no channel specific converter, the default converter is all that is needed. - logger.trace("No Custom converter exists for {} ({})", converterName, converterClassName); - } catch (NoSuchMethodException e) { - logger.warn("NoSuchMethodException occurred for {} ({}) {}", converterName, converterClassName, - e.getMessage()); - } catch (InvocationTargetException e) { - logger.warn("InvocationTargetException occurred for {} ({}) {}", converterName, converterClassName, - e.getMessage()); - } catch (IllegalAccessException e) { - logger.warn("IllegalAccessException occurred for {} ({}) {}", converterName, converterClassName, - e.getMessage()); - } catch (InstantiationException e) { - logger.warn("InstantiationException occurred for {} ({}) {}", converterName, converterClassName, - e.getMessage()); - } - return null; + return SmartthingsConverterFactory.getConverter(converterName); } @Override