]> git.basschouten.com Git - openhab-addons.git/blob
81bd43695883b056488db91944f9e8f20a650c08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.smartthings.internal.converter;
14
15 import java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.smartthings.internal.dto.SmartthingsStateData;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.HSBType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Converter class for Smartthings capability "Color Control".
32  * The Smartthings "Color Control" capability represents the hue values in the 0-100% range. OH2 uses 0-360 degrees
33  * For this converter only the hue is coming into openHAB and it is a number
34  *
35  * @author Bob Raker - Initial contribution
36  */
37 @NonNullByDefault
38 public class SmartthingsHue100Converter extends SmartthingsConverter {
39
40     private final Logger logger = LoggerFactory.getLogger(SmartthingsHue100Converter.class);
41
42     public SmartthingsHue100Converter(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public String convertToSmartthings(ChannelUID channelUid, Command command) {
48         String jsonMsg;
49
50         if (command instanceof HSBType) {
51             HSBType hsb = (HSBType) command;
52             double hue = hsb.getHue().doubleValue() / 3.60;
53             String value = String.format("[%.0f, %d, %d ]", hue, hsb.getSaturation().intValue(),
54                     hsb.getBrightness().intValue());
55             jsonMsg = String.format(
56                     "{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}",
57                     thingTypeId, smartthingsName, channelUid.getId(), value);
58         } else {
59             jsonMsg = defaultConvertToSmartthings(channelUid, command);
60         }
61
62         return jsonMsg;
63     }
64
65     @Override
66     public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
67         // Here we have to multiply the value from Smartthings by 3.6 to convert from 0-100 to 0-360
68         String deviceType = dataFromSmartthings.capabilityAttribute;
69         Object deviceValue = dataFromSmartthings.value;
70
71         if (deviceValue == null) {
72             logger.warn("Failed to convert Number {} because Smartthings returned a null value.", deviceType);
73             return UnDefType.UNDEF;
74         }
75
76         if (acceptedChannelType != null && "Number".contentEquals(acceptedChannelType)) {
77             if (deviceValue instanceof String) {
78                 double d = Double.parseDouble((String) deviceValue);
79                 d *= 3.6;
80                 return new DecimalType(d);
81             } else if (deviceValue instanceof Long) {
82                 double d = ((Long) deviceValue).longValue();
83                 d *= 3.6;
84                 return new DecimalType(d);
85             } else if (deviceValue instanceof BigDecimal) {
86                 double d = ((BigDecimal) deviceValue).doubleValue();
87                 d *= 3.6;
88                 return new DecimalType(d);
89             } else if (deviceValue instanceof Number) {
90                 double d = ((Number) deviceValue).doubleValue();
91                 d *= 3.6;
92                 return new DecimalType(d);
93             } else {
94                 logger.warn("Failed to convert Number {} with a value of {} from class {} to an appropriate type.",
95                         deviceType, deviceValue, deviceValue.getClass().getName());
96                 return UnDefType.UNDEF;
97             }
98         } else {
99             return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
100         }
101     }
102 }