2 * Copyright (c) 2010-2021 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.openhab.binding.tado.internal.api.ApiException;
20 import org.openhab.binding.tado.internal.api.model.OverlayTerminationCondition;
21 import org.openhab.binding.tado.internal.api.model.OverlayTerminationConditionType;
22 import org.openhab.binding.tado.internal.api.model.TimerTerminationCondition;
23 import org.openhab.binding.tado.internal.api.model.ZoneState;
24 import org.openhab.binding.tado.internal.handler.TadoZoneHandler;
27 * Builder for creation of overlay termination conditions.
29 * @author Dennis Frommknecht - Initial contribution
31 public class TerminationConditionBuilder {
32 private TadoZoneHandler zoneHandler;
34 private OverlayTerminationConditionType terminationType = null;
35 private Integer timerDurationInSeconds = null;
37 protected TerminationConditionBuilder(TadoZoneHandler zoneHandler) {
38 this.zoneHandler = zoneHandler;
41 public static TerminationConditionBuilder of(TadoZoneHandler zoneHandler) {
42 return new TerminationConditionBuilder(zoneHandler);
45 public TerminationConditionBuilder withTerminationType(OverlayTerminationConditionType terminationType) {
46 this.terminationType = terminationType;
47 if (terminationType != OverlayTerminationConditionType.TIMER) {
48 timerDurationInSeconds = null;
54 public TerminationConditionBuilder withTimerDurationInSeconds(Integer timerDurationInSeconds) {
55 this.terminationType = OverlayTerminationConditionType.TIMER;
56 this.timerDurationInSeconds = timerDurationInSeconds;
60 public OverlayTerminationCondition build(ZoneStateProvider zoneStateProvider) throws IOException, ApiException {
61 OverlayTerminationCondition terminationCondition = null;
63 if (terminationType != null) {
64 if (terminationType != OverlayTerminationConditionType.TIMER || timerDurationInSeconds != null) {
65 terminationCondition = getTerminationCondition(terminationType, timerDurationInSeconds);
67 terminationCondition = getCurrentOrDefaultTimerTermination(zoneStateProvider);
70 ZoneState zoneState = zoneStateProvider.getZoneState();
71 if (zoneState.getOverlay() != null) {
72 terminationCondition = cleanTerminationCondition(zoneState.getOverlay().getTermination());
74 // Default zone termination condition
75 terminationCondition = getDefaultTerminationCondition();
78 return terminationCondition;
81 private OverlayTerminationCondition getDefaultTerminationCondition() throws IOException, ApiException {
82 OverlayTerminationCondition defaultTerminationCondition = zoneHandler.getDefaultTerminationCondition();
83 return defaultTerminationCondition != null ? defaultTerminationCondition : manualTermination();
86 private TimerTerminationCondition getCurrentOrDefaultTimerTermination(ZoneStateProvider zoneStateProvider)
87 throws IOException, ApiException {
88 // Timer without duration
89 int duration = zoneHandler.getFallbackTimerDuration() * 60;
91 ZoneState zoneState = zoneStateProvider.getZoneState();
93 // If timer is currently running, use its time
94 if (zoneState.getOverlay() != null
95 && zoneState.getOverlay().getTermination().getType() == OverlayTerminationConditionType.TIMER) {
96 duration = ((TimerTerminationCondition) zoneState.getOverlay().getTermination()).getDurationInSeconds();
99 return timerTermination(duration);