]> git.basschouten.com Git - openhab-addons.git/blob
2b3c9be0c0141a2166ddc98bf726af90f7dd98d8
[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.mielecloud.internal.webservice.api;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceType;
23
24 /**
25  * Provides easy access to temperature values mapped for wine storage devices.
26  *
27  * @author Björn Lange - Initial contribution
28  */
29 @NonNullByDefault
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);
34
35     private final DeviceState deviceState;
36     private final List<Integer> effectiveTemperatures;
37     private final List<Integer> effectiveTargetTemperatures;
38
39     /**
40      * Creates a new {@link WineStorageDeviceTemperatureState}.
41      *
42      * @param deviceState Device state to query extended state information from.
43      */
44     public WineStorageDeviceTemperatureState(DeviceState deviceState) {
45         this.deviceState = deviceState;
46         effectiveTemperatures = getEffectiveTemperatures();
47         effectiveTargetTemperatures = getEffectiveTargetTemperatures();
48     }
49
50     private List<Integer> getEffectiveTemperatures() {
51         return Arrays
52                 .asList(deviceState.getTemperature(0), deviceState.getTemperature(1), deviceState.getTemperature(2))
53                 .stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
54     }
55
56     private List<Integer> getEffectiveTargetTemperatures() {
57         return Arrays
58                 .asList(deviceState.getTargetTemperature(0), deviceState.getTargetTemperature(1),
59                         deviceState.getTargetTemperature(2))
60                 .stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
61     }
62
63     /**
64      * Gets the current main temperature of the wine storage.
65      *
66      * @return The current main temperature of the wine storage.
67      */
68     public Optional<Integer> getTemperature() {
69         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
70             return Optional.empty();
71         }
72
73         return getTemperatureFromList(effectiveTemperatures);
74     }
75
76     /**
77      * Gets the target main temperature of the wine storage.
78      *
79      * @return The target main temperature of the wine storage.
80      */
81     public Optional<Integer> getTargetTemperature() {
82         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
83             return Optional.empty();
84         }
85
86         return getTemperatureFromList(effectiveTargetTemperatures);
87     }
88
89     private Optional<Integer> getTemperatureFromList(List<Integer> temperatures) {
90         if (temperatures.isEmpty()) {
91             return Optional.empty();
92         }
93
94         if (temperatures.size() > 1) {
95             return Optional.empty();
96         }
97
98         return Optional.of(temperatures.get(0));
99     }
100
101     /**
102      * Gets the current top temperature of the wine storage.
103      *
104      * @return The current top temperature of the wine storage.
105      */
106     public Optional<Integer> getTopTemperature() {
107         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
108             return Optional.empty();
109         }
110
111         return getTopTemperatureFromList(effectiveTemperatures);
112     }
113
114     /**
115      * Gets the target top temperature of the wine storage.
116      *
117      * @return The target top temperature of the wine storage.
118      */
119     public Optional<Integer> getTopTargetTemperature() {
120         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
121             return Optional.empty();
122         }
123
124         return getTopTemperatureFromList(effectiveTargetTemperatures);
125     }
126
127     private Optional<Integer> getTopTemperatureFromList(List<Integer> temperatures) {
128         if (temperatures.size() <= 1) {
129             return Optional.empty();
130         }
131
132         return Optional.of(temperatures.get(0));
133     }
134
135     /**
136      * Gets the current middle temperature of the wine storage.
137      *
138      * @return The current middle temperature of the wine storage.
139      */
140     public Optional<Integer> getMiddleTemperature() {
141         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
142             return Optional.empty();
143         }
144
145         return getMiddleTemperatureFromList(effectiveTemperatures);
146     }
147
148     /**
149      * Gets the target middle temperature of the wine storage.
150      *
151      * @return The target middle temperature of the wine storage.
152      */
153     public Optional<Integer> getMiddleTargetTemperature() {
154         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
155             return Optional.empty();
156         }
157
158         return getMiddleTemperatureFromList(effectiveTargetTemperatures);
159     }
160
161     private Optional<Integer> getMiddleTemperatureFromList(List<Integer> temperatures) {
162         if (temperatures.size() != 3) {
163             return Optional.empty();
164         }
165
166         return Optional.of(temperatures.get(1));
167     }
168
169     /**
170      * Gets the current bottom temperature of the wine storage.
171      *
172      * @return The current bottom temperature of the wine storage.
173      */
174     public Optional<Integer> getBottomTemperature() {
175         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
176             return Optional.empty();
177         }
178
179         return getBottomTemperatureFromList(effectiveTemperatures);
180     }
181
182     /**
183      * Gets the target bottom temperature of the wine storage.
184      *
185      * @return The target bottom temperature of the wine storage.
186      */
187     public Optional<Integer> getBottomTargetTemperature() {
188         if (!ALL_WINE_STORAGES.contains(deviceState.getRawType())) {
189             return Optional.empty();
190         }
191
192         return getBottomTemperatureFromList(effectiveTargetTemperatures);
193     }
194
195     private Optional<Integer> getBottomTemperatureFromList(List<Integer> temperatures) {
196         if (temperatures.size() == 3) {
197             return Optional.of(temperatures.get(2));
198         }
199
200         if (temperatures.size() == 2) {
201             return Optional.of(temperatures.get(1));
202         }
203
204         return Optional.empty();
205     }
206 }