]> git.basschouten.com Git - openhab-addons.git/blob
115ab6b53b44b249efd590885b94bdca0c7ca19d
[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.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.smartthings.internal.dto.SmartthingsStateData;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Converter class for Smartthings capability "Color Control".
33  * In this case the color being delivered by Smartthings is in the for #hhssbb where hh=hue in hex, ss=saturation in hex
34  * and bb=brightness in hex
35  * And, the hue is a value from 0 to 100% but openHAB expects the hue in 0 to 360
36  *
37  * @author Bob Raker - Initial contribution
38  */
39 @NonNullByDefault
40 public class SmartthingsColor100Converter extends SmartthingsConverter {
41
42     private Pattern rgbInputPattern = Pattern.compile("^#[0-9a-fA-F]{6}");
43
44     private final Logger logger = LoggerFactory.getLogger(SmartthingsColor100Converter.class);
45
46     public SmartthingsColor100Converter(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public String convertToSmartthings(ChannelUID channelUid, Command command) {
52         String jsonMsg;
53         // The command should be of HSBType. The hue component needs to be divided by 3.6 to convert 0-360 degrees to
54         // 0-100 percent
55         // The easiest way to do this is to create a new HSBType with the hue component changed.
56         if (command instanceof HSBType hsbCommand) {
57             double hue = Math.round((hsbCommand.getHue().doubleValue() / 3.60)); // add .5 to round
58             long hueInt = (long) hue;
59             HSBType hsb100 = new HSBType(new DecimalType(hueInt), hsbCommand.getSaturation(),
60                     hsbCommand.getBrightness());
61             // now use the default converter to convert to a JSON string
62             jsonMsg = defaultConvertToSmartthings(channelUid, hsb100);
63         } else {
64             jsonMsg = defaultConvertToSmartthings(channelUid, command);
65         }
66         return jsonMsg;
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.openhab.binding.smartthings.internal.converter.SmartthingsConverter#convertToOpenHab(java.lang.String,
73      * org.openhab.binding.smartthings.internal.SmartthingsStateData)
74      */
75     @Override
76     public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
77         // The color value from Smartthings will look like "#123456" which is the RGB color
78         // This needs to be converted into HSB type
79         String value = dataFromSmartthings.value;
80         if (value == null) {
81             logger.warn("Failed to convert color {} because Smartthings returned a null value.",
82                     dataFromSmartthings.deviceDisplayName);
83             return UnDefType.UNDEF;
84         }
85
86         // If the bulb is off the value maybe null, so better check
87         State state;
88         // First verify the format the string is valid
89         Matcher matcher = rgbInputPattern.matcher(value);
90         if (!matcher.matches()) {
91             logger.warn(
92                     "The \"value\" in the following message is not a valid color. Expected a value like \"#123456\" instead of {}",
93                     dataFromSmartthings.toString());
94             return UnDefType.UNDEF;
95         }
96
97         // Get the RGB colors
98         int[] rgb = new int[3];
99         for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
100             String c = value.substring(pos, pos + 2);
101             rgb[i] = Integer.parseInt(c, 16);
102         }
103
104         // Convert to state
105         state = HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);
106         return state;
107     }
108 }