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.List;
16 import java.util.Objects;
17 import java.util.Optional;
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;
26 * Provides convenient access to the list of actions that can be performed with a device.
28 * @author Roland Edelhoff - Initial contribution
31 public class ActionsState {
33 private final String deviceIdentifier;
34 private final Optional<Actions> actions;
36 public ActionsState(String deviceIdentifier, @Nullable Actions actions) {
37 this.deviceIdentifier = deviceIdentifier;
38 this.actions = Optional.ofNullable(actions);
42 * Gets the unique identifier of the device to which this state refers.
44 public String getDeviceIdentifier() {
45 return deviceIdentifier;
49 * Gets whether the device can be started.
51 public boolean canBeStarted() {
52 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START)).orElse(false);
56 * Gets whether the device can be stopped.
58 public boolean canBeStopped() {
59 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP)).orElse(false);
63 * Gets whether the device can be paused.
65 public boolean canBePaused() {
66 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.PAUSE)).orElse(false);
70 * Gets whether supercooling can be controlled.
72 public boolean canContolSupercooling() {
73 return canStartSupercooling() || canStopSupercooling();
77 * Gets whether supercooling can be started.
79 public boolean canStartSupercooling() {
80 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START_SUPERCOOLING))
85 * Gets whether supercooling can be stopped.
87 public boolean canStopSupercooling() {
88 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP_SUPERCOOLING))
93 * Gets whether superfreezing can be controlled.
95 public boolean canControlSuperfreezing() {
96 return canStartSuperfreezing() || canStopSuperfreezing();
100 * Gets whether superfreezing can be started.
102 public boolean canStartSuperfreezing() {
103 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.START_SUPERFREEZING))
108 * Gets whether superfreezing can be stopped.
110 public boolean canStopSuperfreezing() {
111 return actions.map(Actions::getProcessAction).map(a -> a.contains(ProcessAction.STOP_SUPERFREEZING))
116 * Gets whether light can be enabled.
118 public boolean canEnableLight() {
119 return actions.map(Actions::getLight).map(a -> a.contains(Light.ENABLE)).orElse(false);
123 * Gets whether light can be disabled.
125 public boolean canDisableLight() {
126 return actions.map(Actions::getLight).map(a -> a.contains(Light.DISABLE)).orElse(false);
130 * Gets whether the device can be switched on.
132 public boolean canBeSwitchedOn() {
133 return actions.flatMap(Actions::getPowerOn).map(Boolean.TRUE::equals).orElse(false);
137 * Gets whether the device can be switched off.
139 public boolean canBeSwitchedOff() {
140 return actions.flatMap(Actions::getPowerOff).map(Boolean.TRUE::equals).orElse(false);
144 * Gets whether the light can be controlled.
146 public boolean canControlLight() {
147 return canEnableLight() || canDisableLight();
151 * Gets whether the active program can be set.
153 public boolean canSetActiveProgramId() {
154 return !actions.map(Actions::getProgramId).map(List::isEmpty).orElse(true);
158 public int hashCode() {
159 return Objects.hash(actions, deviceIdentifier);
163 public boolean equals(@Nullable Object obj) {
170 if (getClass() != obj.getClass()) {
173 ActionsState other = (ActionsState) obj;
174 return Objects.equals(actions, other.actions) && Objects.equals(deviceIdentifier, other.deviceIdentifier);