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.mielecloud.internal.webservice.api;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceType;
25 * Provides easy access to temperature values mapped for wine storage devices.
27 * @author Björn Lange - Initial contribution
30 public class WineStorageDeviceTemperatureState {
31 private static final Set<DeviceType> ALL_WINE_STORAGES = Set.of(DeviceType.WINE_CABINET,
32 DeviceType.WINE_CABINET_FREEZER_COMBINATION, DeviceType.WINE_CONDITIONING_UNIT,
33 DeviceType.WINE_STORAGE_CONDITIONING_UNIT);
35 private final DeviceState deviceState;
36 private final List<Integer> effectiveTemperatures;
37 private final List<Integer> effectiveTargetTemperatures;
40 * Creates a new {@link WineStorageDeviceTemperatureState}.
42 * @param deviceState Device state to query extended state information from.
44 public WineStorageDeviceTemperatureState(DeviceState deviceState) {
45 this.deviceState = deviceState;
46 effectiveTemperatures = getEffectiveTemperatures();
47 effectiveTargetTemperatures = getEffectiveTargetTemperatures();
50 private List<Integer> getEffectiveTemperatures() {
52 .asList(deviceState.getTemperature(0), deviceState.getTemperature(1), deviceState.getTemperature(2))
53 .stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
56 private List<Integer> getEffectiveTargetTemperatures() {
58 .asList(deviceState.getTargetTemperature(0), deviceState.getTargetTemperature(1),
59 deviceState.getTargetTemperature(2))
60 .stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
64 * Gets the current main temperature of the wine storage.
66 * @return The current main temperature of the wine storage.
68 public Optional<Integer> getTemperature() {
69 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
70 return Optional.empty();
73 return getTemperatureFromList(effectiveTemperatures);
77 * Gets the target main temperature of the wine storage.
79 * @return The target main temperature of the wine storage.
81 public Optional<Integer> getTargetTemperature() {
82 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
83 return Optional.empty();
86 return getTemperatureFromList(effectiveTargetTemperatures);
89 private Optional<Integer> getTemperatureFromList(List<Integer> temperatures) {
90 if (temperatures.isEmpty()) {
91 return Optional.empty();
94 if (temperatures.size() > 1) {
95 return Optional.empty();
98 return Optional.of(temperatures.get(0));
102 * Gets the current top temperature of the wine storage.
104 * @return The current top temperature of the wine storage.
106 public Optional<Integer> getTopTemperature() {
107 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
108 return Optional.empty();
111 return getTopTemperatureFromList(effectiveTemperatures);
115 * Gets the target top temperature of the wine storage.
117 * @return The target top temperature of the wine storage.
119 public Optional<Integer> getTopTargetTemperature() {
120 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
121 return Optional.empty();
124 return getTopTemperatureFromList(effectiveTargetTemperatures);
127 private Optional<Integer> getTopTemperatureFromList(List<Integer> temperatures) {
128 if (temperatures.size() <= 1) {
129 return Optional.empty();
132 return Optional.of(temperatures.get(0));
136 * Gets the current middle temperature of the wine storage.
138 * @return The current middle temperature of the wine storage.
140 public Optional<Integer> getMiddleTemperature() {
141 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
142 return Optional.empty();
145 return getMiddleTemperatureFromList(effectiveTemperatures);
149 * Gets the target middle temperature of the wine storage.
151 * @return The target middle temperature of the wine storage.
153 public Optional<Integer> getMiddleTargetTemperature() {
154 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
155 return Optional.empty();
158 return getMiddleTemperatureFromList(effectiveTargetTemperatures);
161 private Optional<Integer> getMiddleTemperatureFromList(List<Integer> temperatures) {
162 if (temperatures.size() != 3) {
163 return Optional.empty();
166 return Optional.of(temperatures.get(1));
170 * Gets the current bottom temperature of the wine storage.
172 * @return The current bottom temperature of the wine storage.
174 public Optional<Integer> getBottomTemperature() {
175 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
176 return Optional.empty();
179 return getBottomTemperatureFromList(effectiveTemperatures);
183 * Gets the target bottom temperature of the wine storage.
185 * @return The target bottom temperature of the wine storage.
187 public Optional<Integer> getBottomTargetTemperature() {
188 if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
189 return Optional.empty();
192 return getBottomTemperatureFromList(effectiveTargetTemperatures);
195 private Optional<Integer> getBottomTemperatureFromList(List<Integer> temperatures) {
196 if (temperatures.size() == 3) {
197 return Optional.of(temperatures.get(2));
200 if (temperatures.size() == 2) {
201 return Optional.of(temperatures.get(1));
204 return Optional.empty();