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.powermax.internal.state;
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;
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;
31 * Base class for extensible state objects
33 * @author Ron Isaacson - Initial contribution
36 public abstract class PowermaxStateContainer {
38 protected final TimeZoneProvider timeZoneProvider;
39 protected List<Value<?>> values;
41 public abstract class Value<T> {
42 protected @Nullable T value;
43 protected final String channel;
45 public Value(PowermaxStateContainer parent, String channel) {
46 this.channel = channel;
49 parent.getValues().add(this);
52 public @Nullable T getValue() {
56 public void setValue(@Nullable T value) {
60 @SuppressWarnings("unchecked")
61 public void setValueUnsafe(@Nullable Object value) {
62 this.value = (T) value;
65 public String getChannel() {
69 public abstract State getState();
72 public class DynamicValue<T> extends Value<T> {
73 Supplier<@Nullable T> valueFunction;
74 Supplier<State> stateFunction;
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;
83 // Note: setValue() is still valid, but the saved value will be ignored
86 public @Nullable T getValue() {
87 return valueFunction.get();
91 public State getState() {
92 return stateFunction.get();
96 public class BooleanValue extends Value<Boolean> {
100 public BooleanValue(PowermaxStateContainer parent, String channel, State trueState, State falseState) {
101 super(parent, channel);
102 this.trueState = trueState;
103 this.falseState = falseState;
106 public BooleanValue(PowermaxStateContainer parent, String channel) {
107 this(parent, channel, OnOffType.ON, OnOffType.OFF);
111 public State getState() {
113 return val == null ? UnDefType.NULL : (val ? trueState : falseState);
117 public class StringValue extends Value<String> {
118 public StringValue(PowermaxStateContainer parent, String channel) {
119 super(parent, channel);
123 public State getState() {
125 return val == null ? UnDefType.NULL : new StringType(val);
129 public class DateTimeValue extends Value<Long> {
130 public DateTimeValue(PowermaxStateContainer parent, String channel) {
131 super(parent, channel);
135 public State getState() {
138 return UnDefType.NULL;
140 ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), timeZoneProvider.getTimeZone());
141 return new DateTimeType(zoned);
145 protected PowermaxStateContainer(TimeZoneProvider timeZoneProvider) {
146 this.timeZoneProvider = timeZoneProvider;
147 this.values = new ArrayList<>();
150 public List<Value<?>> getValues() {