2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.leapmotion.internal;
15 import java.math.BigDecimal;
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;
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.
33 * @author Kai Kreuzer - Initial contribution
36 public class LeapMotionDimmerProfile implements TriggerProfile {
38 static final int MAX_HEIGHT = 400; // in mm over controller
39 private static final String MODE = "mode";
41 private final Logger logger = LoggerFactory.getLogger(LeapMotionDimmerProfile.class);
43 private ProfileCallback callback;
44 private BigDecimal lastState = BigDecimal.ZERO;
45 private boolean fingerMode = false;
47 public LeapMotionDimmerProfile(ProfileCallback callback, ProfileContext profileContext) {
48 this.callback = callback;
49 fingerMode = "fingers".equals(profileContext.getConfiguration().get(MODE));
53 public ProfileTypeUID getProfileTypeUID() {
54 return LeapMotionProfileFactory.UID_DIMMER;
58 public void onStateUpdateFromItem(State state) {
59 PercentType currentState = state.as(PercentType.class);
60 if (currentState != null) {
61 lastState = currentState.toBigDecimal();
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)) {
71 .valueOf(Character.toString(event.charAt(LeapMotionBindingConstants.GESTURE_FINGERS.length())));
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
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);