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.hdpowerview.internal.dto.gen3;
15 import java.nio.charset.StandardCharsets;
16 import java.util.Base64;
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;
31 * DTO for a shade as returned by an HD PowerView Generation 3 Gateway.
33 * @author Andrew Fiddian-Green - Initial contribution
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;
49 private transient boolean partialState;
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)));
57 public @Nullable String getBleName() {
61 public @Nullable Integer getCapabilities() {
65 public @Nullable String getCapabilitieString() {
66 Integer capabilities = this.capabilities;
67 return capabilities == null ? null : Integer.toString(capabilities);
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);
78 return id != null ? id.intValue() : 0;
81 public State getLowBattery() {
82 Integer batteryStatus = this.batteryStatus;
83 return batteryStatus == null ? UnDefType.UNDEF : OnOffType.from(batteryStatus == 0);
86 public String getName() {
87 return new String(Base64.getDecoder().decode(name), StandardCharsets.UTF_8);
90 public State getPosition(CoordinateSystem posKindCoords) {
91 ShadePosition positions = this.positions;
92 return positions == null ? UnDefType.UNDEF : positions.getState(posKindCoords);
95 public PowerType getPowerType() {
96 Integer powerType = this.powerType;
97 return PowerType.values()[powerType != null ? powerType.intValue() : 0];
100 public @Nullable ShadePosition getShadePositions() {
104 public State getSignalStrength() {
105 Integer signalStrength = this.signalStrength;
106 return signalStrength != null ? new QuantityType<>(signalStrength, Units.DECIBEL_MILLIWATTS) : UnDefType.UNDEF;
109 public @Nullable Integer getType() {
113 public @Nullable String getTypeString() {
114 Integer type = this.type;
115 return type == null ? null : Integer.toString(type);
118 public boolean hasFullState() {
119 return !partialState;
122 public boolean isMainsPowered() {
123 return getPowerType() == PowerType.HARDWIRED;
126 public Shade setCapabilities(int capabilities) {
127 this.capabilities = capabilities;
131 public Shade setId(int id) {
136 public Shade setPartialState() {
137 this.partialState = true;
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;
147 positions.setPosition(coordinates, percent);
151 public Shade setShadePosition(ShadePosition position) {
152 this.positions = position;
156 public Shade setType(int type) {