]> git.basschouten.com Git - openhab-addons.git/blob
4171b9645fc06d3d2d5b760db5c739b33457cdeb
[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 java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
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.ProfileContext;
22 import org.openhab.core.thing.profiles.ProfileTypeUID;
23 import org.openhab.core.thing.profiles.TriggerProfile;
24 import org.openhab.core.types.State;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link LeapMotionDimmerProfile} class implements the behavior when being linked to a Dimmer item.
30  * It supports two modes: Either the dim level is determined by the number of shown fingers or by the height of the hand
31  * over the controller.
32  *
33  * @author Kai Kreuzer - Initial contribution
34  */
35 @NonNullByDefault
36 public class LeapMotionDimmerProfile implements TriggerProfile {
37
38     static final int MAX_HEIGHT = 400; // in mm over controller
39     private static final String MODE = "mode";
40
41     private final Logger logger = LoggerFactory.getLogger(LeapMotionDimmerProfile.class);
42
43     private ProfileCallback callback;
44     private BigDecimal lastState = BigDecimal.ZERO;
45     private boolean fingerMode = false;
46
47     public LeapMotionDimmerProfile(ProfileCallback callback, ProfileContext profileContext) {
48         this.callback = callback;
49         fingerMode = "fingers".equals(profileContext.getConfiguration().get(MODE));
50     }
51
52     @Override
53     public ProfileTypeUID getProfileTypeUID() {
54         return LeapMotionProfileFactory.UID_DIMMER;
55     }
56
57     @Override
58     public void onStateUpdateFromItem(State state) {
59         PercentType currentState = state.as(PercentType.class);
60         if (currentState != null) {
61             lastState = currentState.toBigDecimal();
62         }
63     }
64
65     @Override
66     public void onTriggerFromHandler(String event) {
67         if (event.equals(LeapMotionBindingConstants.GESTURE_TAP)) {
68             callback.sendCommand(OnOffType.from(lastState.equals(BigDecimal.ZERO)));
69         } else if (event.startsWith(LeapMotionBindingConstants.GESTURE_FINGERS)) {
70             int fingers = Integer
71                     .valueOf(Character.toString(event.charAt(LeapMotionBindingConstants.GESTURE_FINGERS.length())));
72             if (fingerMode) {
73                 // the brightness is determined by the number of shown fingers, 20% for each.
74                 callback.sendCommand(new PercentType(fingers * 20));
75             } else if (fingers == 5) {
76                 // the brightness is determined by the height of the palm over the sensor, where higher means brighter
77                 try {
78                     int height = Integer
79                             .valueOf(event.substring(LeapMotionBindingConstants.GESTURE_FINGERS.length() + 2));
80                     height = Math.min(100 * height / MAX_HEIGHT, 100); // don't use values over 100
81                     callback.sendCommand(new PercentType(height));
82                 } catch (NumberFormatException e) {
83                     logger.error("Found illegal format of finger event: {}", event, e);
84                 }
85             }
86         }
87     }
88 }