]> git.basschouten.com Git - openhab-addons.git/blob
cc838d75ef518dd1be20e148554a449a5d6b718b
[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.hdpowerview.internal.dto.gen3;
14
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.math.RoundingMode;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.hdpowerview.internal.dto.CoordinateSystem;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.UnDefType;
24
25 /**
26  * DTO for the position of a shade as returned by an HD PowerView Generation 3 Gateway.
27  *
28  * @author Andrew Fiddian-Green - Initial contribution
29  */
30 @NonNullByDefault
31 public class ShadePosition {
32     private static final MathContext MATH_CONTEXT = new MathContext(4, RoundingMode.HALF_UP);
33
34     private @NonNullByDefault({}) Double primary;
35     private @NonNullByDefault({}) Double secondary;
36     private @NonNullByDefault({}) Double tilt;
37
38     public State getState(CoordinateSystem posKindCoords) {
39         Double value;
40         switch (posKindCoords) {
41             case PRIMARY_POSITION:
42                 value = primary;
43                 break;
44             case SECONDARY_POSITION:
45                 value = secondary;
46                 break;
47             case VANE_TILT_POSITION:
48                 value = tilt;
49                 break;
50             default:
51                 value = null;
52         }
53         return value != null ? new PercentType(new BigDecimal(value * 100f, MATH_CONTEXT)) : UnDefType.UNDEF;
54     }
55
56     /**
57      * Set a new position value for this object based on the given coordinates, and the given new value.
58      *
59      * @param coordinates which of the position fields shall be set.
60      * @param percent the new value in percent.
61      * @return this object.
62      */
63     public ShadePosition setPosition(CoordinateSystem coordinates, PercentType percent) {
64         Double value = percent.doubleValue() / 100f;
65         switch (coordinates) {
66             case PRIMARY_POSITION:
67                 primary = value;
68                 break;
69             case SECONDARY_POSITION:
70                 secondary = value;
71                 break;
72             case VANE_TILT_POSITION:
73                 tilt = value;
74                 break;
75             default:
76         }
77         return this;
78     }
79 }