]> git.basschouten.com Git - openhab-addons.git/blob
87b1a954b23275d3136793a4833e1051c1fa51de
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.tado.internal.builder;
14
15 import static org.openhab.binding.tado.internal.api.TadoApiTypeUtils.*;
16
17 import java.io.IOException;
18
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;
27
28 /**
29  * Builder for creation of overlay termination conditions.
30  *
31  * @author Dennis Frommknecht - Initial contribution
32  */
33 @NonNullByDefault
34 public class TerminationConditionBuilder {
35
36     private final TadoZoneHandler zoneHandler;
37
38     private @Nullable OverlayTerminationConditionType terminationType;
39     private int timerDurationInSeconds = 0;
40
41     protected TerminationConditionBuilder(TadoZoneHandler zoneHandler) {
42         this.zoneHandler = zoneHandler;
43     }
44
45     public static TerminationConditionBuilder of(TadoZoneHandler zoneHandler) {
46         return new TerminationConditionBuilder(zoneHandler);
47     }
48
49     public TerminationConditionBuilder withTerminationType(OverlayTerminationConditionType terminationType) {
50         this.terminationType = terminationType;
51         if (terminationType != OverlayTerminationConditionType.TIMER) {
52             timerDurationInSeconds = 0;
53         }
54         return this;
55     }
56
57     public TerminationConditionBuilder withTimerDurationInSeconds(int timerDurationInSeconds) {
58         this.terminationType = OverlayTerminationConditionType.TIMER;
59         this.timerDurationInSeconds = timerDurationInSeconds;
60         return this;
61     }
62
63     public OverlayTerminationCondition build(ZoneStateProvider zoneStateProvider) throws IOException, ApiException {
64         OverlayTerminationCondition terminationCondition;
65
66         OverlayTerminationConditionType terminationType = this.terminationType;
67         if (terminationType != null) {
68             if (terminationType != OverlayTerminationConditionType.TIMER || timerDurationInSeconds > 0) {
69                 terminationCondition = getTerminationCondition(terminationType, timerDurationInSeconds);
70             } else {
71                 terminationCondition = getCurrentOrDefaultTimerTermination(zoneStateProvider);
72             }
73         } else {
74             ZoneState zoneState = zoneStateProvider.getZoneState();
75             if (zoneState.getOverlay() != null) {
76                 terminationCondition = cleanTerminationCondition(zoneState.getOverlay().getTermination());
77             } else {
78                 // Default zone termination condition
79                 terminationCondition = getDefaultTerminationCondition();
80             }
81         }
82
83         return terminationCondition;
84     }
85
86     private OverlayTerminationCondition getDefaultTerminationCondition() throws IOException, ApiException {
87         return zoneHandler.getDefaultTerminationCondition();
88     }
89
90     private TimerTerminationCondition getCurrentOrDefaultTimerTermination(ZoneStateProvider zoneStateProvider)
91             throws IOException, ApiException {
92         // Timer without duration
93         Integer duration = zoneHandler.getFallbackTimerDuration() * 60;
94
95         ZoneState zoneState = zoneStateProvider.getZoneState();
96
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();
101         }
102
103         return timerTermination(duration);
104     }
105 }