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.tado.internal.builder;
15 import static org.openhab.binding.tado.internal.api.TadoApiTypeUtils.*;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tado.internal.api.ApiException;
22 import org.openhab.binding.tado.internal.api.model.OverlayTerminationCondition;
23 import org.openhab.binding.tado.internal.api.model.OverlayTerminationConditionType;
24 import org.openhab.binding.tado.internal.api.model.TimerTerminationCondition;
25 import org.openhab.binding.tado.internal.api.model.ZoneState;
26 import org.openhab.binding.tado.internal.handler.TadoZoneHandler;
29 * Builder for creation of overlay termination conditions.
31 * @author Dennis Frommknecht - Initial contribution
34 public class TerminationConditionBuilder {
36 private final TadoZoneHandler zoneHandler;
38 private @Nullable OverlayTerminationConditionType terminationType;
39 private int timerDurationInSeconds = 0;
41 protected TerminationConditionBuilder(TadoZoneHandler zoneHandler) {
42 this.zoneHandler = zoneHandler;
45 public static TerminationConditionBuilder of(TadoZoneHandler zoneHandler) {
46 return new TerminationConditionBuilder(zoneHandler);
49 public TerminationConditionBuilder withTerminationType(OverlayTerminationConditionType terminationType) {
50 this.terminationType = terminationType;
51 if (terminationType != OverlayTerminationConditionType.TIMER) {
52 timerDurationInSeconds = 0;
57 public TerminationConditionBuilder withTimerDurationInSeconds(int timerDurationInSeconds) {
58 this.terminationType = OverlayTerminationConditionType.TIMER;
59 this.timerDurationInSeconds = timerDurationInSeconds;
63 public OverlayTerminationCondition build(ZoneStateProvider zoneStateProvider) throws IOException, ApiException {
64 OverlayTerminationCondition terminationCondition;
66 OverlayTerminationConditionType terminationType = this.terminationType;
67 if (terminationType != null) {
68 if (terminationType != OverlayTerminationConditionType.TIMER || timerDurationInSeconds > 0) {
69 terminationCondition = getTerminationCondition(terminationType, timerDurationInSeconds);
71 terminationCondition = getCurrentOrDefaultTimerTermination(zoneStateProvider);
74 ZoneState zoneState = zoneStateProvider.getZoneState();
75 if (zoneState.getOverlay() != null) {
76 terminationCondition = cleanTerminationCondition(zoneState.getOverlay().getTermination());
78 // Default zone termination condition
79 terminationCondition = getDefaultTerminationCondition();
83 return terminationCondition;
86 private OverlayTerminationCondition getDefaultTerminationCondition() throws IOException, ApiException {
87 return zoneHandler.getDefaultTerminationCondition();
90 private TimerTerminationCondition getCurrentOrDefaultTimerTermination(ZoneStateProvider zoneStateProvider)
91 throws IOException, ApiException {
92 // Timer without duration
93 Integer duration = zoneHandler.getFallbackTimerDuration() * 60;
95 ZoneState zoneState = zoneStateProvider.getZoneState();
97 // If timer is currently running, use its time
98 if (zoneState.getOverlay() != null
99 && zoneState.getOverlay().getTermination().getType() == OverlayTerminationConditionType.TIMER) {
100 duration = ((TimerTerminationCondition) zoneState.getOverlay().getTermination()).getDurationInSeconds();
103 return timerTermination(duration);