]> git.basschouten.com Git - openhab-addons.git/blob
9552eea959cb434a50974a1f9baa8396552ecd03
[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.nio.charset.StandardCharsets;
16 import java.util.Base64;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.hdpowerview.internal.dto.CoordinateSystem;
21 import org.openhab.binding.hdpowerview.internal.dto.Firmware;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  * DTO for a shade as returned by an HD PowerView Generation 3 Gateway.
32  *
33  * @author Andrew Fiddian-Green - Initial contribution
34  */
35 @NonNullByDefault
36 public class Shade {
37     private @Nullable Integer id;
38     private @Nullable Integer type;
39     private @Nullable String name;
40     private @Nullable @SuppressWarnings("unused") String ptName;
41     private @Nullable Integer capabilities;
42     private @Nullable Integer powerType;
43     private @Nullable Integer batteryStatus;
44     private @Nullable Integer signalStrength;
45     private @Nullable String bleName;
46     private @Nullable Firmware firmware;
47     private @Nullable ShadePosition positions;
48
49     private transient boolean partialState;
50
51     public State getBatteryLevel() {
52         Integer batteryStatus = this.batteryStatus;
53         return batteryStatus == null ? UnDefType.UNDEF
54                 : new DecimalType(Math.max(0, Math.min(100, (100 * batteryStatus) / 3)));
55     }
56
57     public @Nullable String getBleName() {
58         return bleName;
59     }
60
61     public @Nullable Integer getCapabilities() {
62         return capabilities;
63     }
64
65     public @Nullable String getCapabilitieString() {
66         Integer capabilities = this.capabilities;
67         return capabilities == null ? null : Integer.toString(capabilities);
68     }
69
70     public @Nullable String getFirmware() {
71         Firmware firmware = this.firmware;
72         return firmware == null ? null
73                 : String.format("%d.%d.%d", firmware.revision, firmware.subRevision, firmware.build);
74     }
75
76     public int getId() {
77         Integer id = this.id;
78         return id != null ? id.intValue() : 0;
79     }
80
81     public State getLowBattery() {
82         Integer batteryStatus = this.batteryStatus;
83         return batteryStatus == null ? UnDefType.UNDEF : OnOffType.from(batteryStatus == 0);
84     }
85
86     public String getName() {
87         return new String(Base64.getDecoder().decode(name), StandardCharsets.UTF_8);
88     }
89
90     public State getPosition(CoordinateSystem posKindCoords) {
91         ShadePosition positions = this.positions;
92         return positions == null ? UnDefType.UNDEF : positions.getState(posKindCoords);
93     }
94
95     public PowerType getPowerType() {
96         Integer powerType = this.powerType;
97         return PowerType.values()[powerType != null ? powerType.intValue() : 0];
98     }
99
100     public @Nullable ShadePosition getShadePositions() {
101         return positions;
102     }
103
104     public State getSignalStrength() {
105         Integer signalStrength = this.signalStrength;
106         return signalStrength != null ? new QuantityType<>(signalStrength, Units.DECIBEL_MILLIWATTS) : UnDefType.UNDEF;
107     }
108
109     public @Nullable Integer getType() {
110         return type;
111     }
112
113     public @Nullable String getTypeString() {
114         Integer type = this.type;
115         return type == null ? null : Integer.toString(type);
116     }
117
118     public boolean hasFullState() {
119         return !partialState;
120     }
121
122     public boolean isMainsPowered() {
123         return getPowerType() == PowerType.HARDWIRED;
124     }
125
126     public Shade setCapabilities(int capabilities) {
127         this.capabilities = capabilities;
128         return this;
129     }
130
131     public Shade setId(int id) {
132         this.id = id;
133         return this;
134     }
135
136     public Shade setPartialState() {
137         this.partialState = true;
138         return this;
139     }
140
141     public Shade setPosition(CoordinateSystem coordinates, PercentType percent) {
142         ShadePosition positions = this.positions;
143         if (positions == null) {
144             positions = new ShadePosition();
145             this.positions = positions;
146         }
147         positions.setPosition(coordinates, percent);
148         return this;
149     }
150
151     public Shade setShadePosition(ShadePosition position) {
152         this.positions = position;
153         return this;
154     }
155
156     public Shade setType(int type) {
157         this.type = type;
158         return this;
159     }
160 }