]> git.basschouten.com Git - openhab-addons.git/blob
0764a061b89238bc75b71f5cc692f9e1712c2796
[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.leapmotion.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.HSBType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.thing.profiles.ProfileCallback;
21 import org.openhab.core.thing.profiles.ProfileTypeUID;
22 import org.openhab.core.thing.profiles.TriggerProfile;
23 import org.openhab.core.types.State;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link LeapMotionColorProfile} class implements the behavior when being linked to a Color item.
29  *
30  * @author Kai Kreuzer - Initial contribution
31  */
32 @NonNullByDefault
33 public class LeapMotionColorProfile implements TriggerProfile {
34
35     private final Logger logger = LoggerFactory.getLogger(LeapMotionColorProfile.class);
36
37     private ProfileCallback callback;
38     private HSBType lastState = HSBType.BLACK;
39
40     public LeapMotionColorProfile(ProfileCallback callback) {
41         this.callback = callback;
42     }
43
44     @Override
45     public ProfileTypeUID getProfileTypeUID() {
46         return LeapMotionProfileFactory.UID_COLOR;
47     }
48
49     @Override
50     public void onStateUpdateFromItem(State state) {
51         if (state instanceof HSBType hsbState) {
52             lastState = hsbState;
53         } else {
54             PercentType currentBrightness = state.as(PercentType.class);
55             if (currentBrightness != null) {
56                 lastState = new HSBType(lastState.getHue(), lastState.getSaturation(), currentBrightness);
57             }
58         }
59     }
60
61     @Override
62     public void onTriggerFromHandler(String event) {
63         if (event.equals(LeapMotionBindingConstants.GESTURE_TAP)) {
64             callback.sendCommand(OnOffType.from(lastState.getBrightness().equals(PercentType.ZERO)));
65         } else if (event.equals(LeapMotionBindingConstants.GESTURE_CLOCKWISE)) {
66             HSBType color = changeColor(lastState, true);
67             callback.sendCommand(color);
68             lastState = color;
69         } else if (event.equals(LeapMotionBindingConstants.GESTURE_ANTICLOCKWISE)) {
70             HSBType color = changeColor(lastState, false);
71             callback.sendCommand(color);
72             lastState = color;
73         } else if (event.startsWith(LeapMotionBindingConstants.GESTURE_FINGERS)) {
74             // the brightness is determined by the height of the palm over the sensor, where higher means brighter
75             int fingers = Integer
76                     .valueOf(Character.toString(event.charAt(LeapMotionBindingConstants.GESTURE_FINGERS.length())));
77             if (fingers == 5) {
78                 try {
79                     int height = Integer
80                             .valueOf(event.substring(LeapMotionBindingConstants.GESTURE_FINGERS.length() + 2));
81                     height = Math.min(100 * height / LeapMotionDimmerProfile.MAX_HEIGHT, 100); // don't use values over
82                                                                                                // 100
83                     PercentType brightness = new PercentType(height);
84                     callback.sendCommand(brightness);
85                     lastState = new HSBType(lastState.getHue(), lastState.getSaturation(), brightness);
86                 } catch (NumberFormatException e) {
87                     logger.error("Found illegal format of finger event: {}", event, e);
88                 }
89             }
90         }
91     }
92
93     private HSBType changeColor(HSBType color, boolean clockwise) {
94         int hue = clockwise ? (color.getHue().toBigDecimal().intValue() - 20 + 360) % 360
95                 : (color.getHue().toBigDecimal().intValue() + 20 + 360) % 360;
96         logger.debug("New hue value: {}", hue);
97         return new HSBType(new DecimalType(hue), color.getSaturation(), color.getBrightness());
98     }
99 }