]> git.basschouten.com Git - openhab-addons.git/blob
f5cb70b918a4543036ec73c3d07c7cb0e2668fbb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.powermax.internal.state;
14
15 import java.time.Instant;
16 import java.time.ZonedDateTime;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.function.Supplier;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.i18n.TimeZoneProvider;
23 import org.openhab.core.library.types.DateTimeType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27
28 /**
29  * Base class for extensible state objects
30  *
31  * @author Ron Isaacson - Initial contribution
32  */
33 public abstract class PowermaxStateContainer {
34
35     protected final TimeZoneProvider timeZoneProvider;
36     protected List<Value<?>> values;
37
38     public abstract class Value<T> {
39         protected @Nullable T value;
40         protected final String channel;
41
42         public Value(PowermaxStateContainer parent, String channel) {
43             this.channel = channel;
44             this.value = null;
45
46             parent.getValues().add(this);
47         }
48
49         public T getValue() {
50             return value;
51         }
52
53         public void setValue(@Nullable T value) {
54             this.value = value;
55         }
56
57         @SuppressWarnings("unchecked")
58         public void setValueUnsafe(@Nullable Object value) {
59             this.value = (T) value;
60         }
61
62         public String getChannel() {
63             return channel;
64         }
65
66         public abstract State getState();
67     }
68
69     public class DynamicValue<T> extends Value<T> {
70         Supplier<T> valueFunction;
71         Supplier<State> stateFunction;
72
73         public DynamicValue(PowermaxStateContainer parent, String channel, Supplier<T> valueFunction,
74                 Supplier<State> stateFunction) {
75             super(parent, channel);
76             this.valueFunction = valueFunction;
77             this.stateFunction = stateFunction;
78         }
79
80         // Note: setValue() is still valid, but the saved value will be ignored
81
82         @Override
83         public T getValue() {
84             return valueFunction.get();
85         }
86
87         @Override
88         public State getState() {
89             return stateFunction.get();
90         }
91     }
92
93     public class BooleanValue extends Value<Boolean> {
94         State trueState;
95         State falseState;
96
97         public BooleanValue(PowermaxStateContainer parent, String channel, State trueState, State falseState) {
98             super(parent, channel);
99             this.trueState = trueState;
100             this.falseState = falseState;
101         }
102
103         public BooleanValue(PowermaxStateContainer parent, String channel) {
104             this(parent, channel, OnOffType.ON, OnOffType.OFF);
105         }
106
107         @Override
108         public State getState() {
109             return value ? trueState : falseState;
110         }
111     }
112
113     public class StringValue extends Value<String> {
114         public StringValue(PowermaxStateContainer parent, String channel) {
115             super(parent, channel);
116         }
117
118         @Override
119         public State getState() {
120             return new StringType(value);
121         }
122     }
123
124     public class DateTimeValue extends Value<Long> {
125         public DateTimeValue(PowermaxStateContainer parent, String channel) {
126             super(parent, channel);
127         }
128
129         @Override
130         public State getState() {
131             ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), timeZoneProvider.getTimeZone());
132             return new DateTimeType(zoned);
133         }
134     }
135
136     protected PowermaxStateContainer(TimeZoneProvider timeZoneProvider) {
137         this.timeZoneProvider = timeZoneProvider;
138         this.values = new ArrayList<>();
139     }
140
141     public List<Value<?>> getValues() {
142         return values;
143     }
144 }