]> git.basschouten.com Git - openhab-addons.git/blob
64d52c7c6e74027f86b45798aab02b024a27e6a2
[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.List;
16 import java.util.Objects;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mielecloud.internal.webservice.api.json.Actions;
22 import org.openhab.binding.mielecloud.internal.webservice.api.json.Light;
23 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProcessAction;
24
25 /**
26  * Provides convenient access to the list of actions that can be performed with a device.
27  *
28  * @author Roland Edelhoff - Initial contribution
29  */
30 @NonNullByDefault
31 public class ActionsState {
32
33     private final String deviceIdentifier;
34     private final Optional<Actions> actions;
35
36     public ActionsState(String deviceIdentifier, @Nullable Actions actions) {
37         this.deviceIdentifier = deviceIdentifier;
38         this.actions = Optional.ofNullable(actions);
39     }
40
41     /**
42      * Gets the unique identifier of the device to which this state refers.
43      */
44     public String getDeviceIdentifier() {
45         return deviceIdentifier;
46     }
47
48     /**
49      * Gets whether the device can be started.
50      */
51     public boolean canBeStarted() {
52         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START)).orElse(false);
53     }
54
55     /**
56      * Gets whether the device can be stopped.
57      */
58     public boolean canBeStopped() {
59         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP)).orElse(false);
60     }
61
62     /**
63      * Gets whether the device can be paused.
64      */
65     public boolean canBePaused() {
66         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.PAUSE)).orElse(false);
67     }
68
69     /**
70      * Gets whether supercooling can be controlled.
71      */
72     public boolean canContolSupercooling() {
73         return canStartSupercooling() || canStopSupercooling();
74     }
75
76     /**
77      * Gets whether supercooling can be started.
78      */
79     public boolean canStartSupercooling() {
80         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START_SUPERCOOLING))
81                 .orElse(false);
82     }
83
84     /**
85      * Gets whether supercooling can be stopped.
86      */
87     public boolean canStopSupercooling() {
88         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP_SUPERCOOLING))
89                 .orElse(false);
90     }
91
92     /**
93      * Gets whether superfreezing can be controlled.
94      */
95     public boolean canControlSuperfreezing() {
96         return canStartSuperfreezing() || canStopSuperfreezing();
97     }
98
99     /**
100      * Gets whether superfreezing can be started.
101      */
102     public boolean canStartSuperfreezing() {
103         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START_SUPERFREEZING))
104                 .orElse(false);
105     }
106
107     /**
108      * Gets whether superfreezing can be stopped.
109      */
110     public boolean canStopSuperfreezing() {
111         return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP_SUPERFREEZING))
112                 .orElse(false);
113     }
114
115     /**
116      * Gets whether light can be enabled.
117      */
118     public boolean canEnableLight() {
119         return actions.map(Actions::getLight).map(a -> a.contains(Light.ENABLE)).orElse(false);
120     }
121
122     /**
123      * Gets whether light can be disabled.
124      */
125     public boolean canDisableLight() {
126         return actions.map(Actions::getLight).map(a -> a.contains(Light.DISABLE)).orElse(false);
127     }
128
129     /**
130      * Gets whether the device can be switched on.
131      */
132     public boolean canBeSwitchedOn() {
133         return actions.flatMap(Actions::getPowerOn).map(Boolean.TRUE::equals).orElse(false);
134     }
135
136     /**
137      * Gets whether the device can be switched off.
138      */
139     public boolean canBeSwitchedOff() {
140         return actions.flatMap(Actions::getPowerOff).map(Boolean.TRUE::equals).orElse(false);
141     }
142
143     /**
144      * Gets whether the light can be controlled.
145      */
146     public boolean canControlLight() {
147         return canEnableLight() || canDisableLight();
148     }
149
150     /**
151      * Gets whether the active program can be set.
152      */
153     public boolean canSetActiveProgramId() {
154         return !actions.map(Actions::getProgramId).map(List::isEmpty).orElse(true);
155     }
156
157     @Override
158     public int hashCode() {
159         return Objects.hash(actions, deviceIdentifier);
160     }
161
162     @Override
163     public boolean equals(@Nullable Object obj) {
164         if (this == obj) {
165             return true;
166         }
167         if (obj == null) {
168             return false;
169         }
170         if (getClass() != obj.getClass()) {
171             return false;
172         }
173         ActionsState other = (ActionsState) obj;
174         return Objects.equals(actions, other.actions) && Objects.equals(deviceIdentifier, other.deviceIdentifier);
175     }
176 }