]> git.basschouten.com Git - openhab-addons.git/blob
e6e6f18d45186ff068d48021238b1bc64bfb8ff7
[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.hue.internal.dto.clip2;
14
15 import java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.hue.internal.dto.clip2.enums.DirectionType;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23
24 /**
25  * DTO for rotation element of a tap dial switch.
26  *
27  * @author Andrew Fiddian-Green - Initial contribution
28  */
29 @NonNullByDefault
30 public class Rotation {
31     private @Nullable String direction;
32     private @Nullable Integer duration;
33     private @Nullable Integer steps;
34
35     public @Nullable DirectionType getDirection() {
36         String direction = this.direction;
37         return Objects.nonNull(direction) ? DirectionType.valueOf(direction.toUpperCase()) : null;
38     }
39
40     public int getDuration() {
41         Integer duration = this.duration;
42         return Objects.nonNull(duration) ? duration.intValue() : 0;
43     }
44
45     public int getSteps() {
46         Integer steps = this.steps;
47         return Objects.nonNull(steps) ? steps.intValue() : 0;
48     }
49
50     /**
51      * Get the state corresponding to a relative rotary dial's last steps value. Clockwise rotations are positive, and
52      * counter clockwise rotations negative.
53      *
54      * @return the state or UNDEF.
55      */
56     public State getStepsState() {
57         DirectionType direction = getDirection();
58         Integer steps = this.steps;
59         if (Objects.nonNull(direction) && Objects.nonNull(steps)) {
60             return new DecimalType(DirectionType.CLOCK_WISE.equals(direction) ? steps.intValue() : -steps.intValue());
61         }
62         return UnDefType.NULL;
63     }
64 }