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