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.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.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;
29 * Base class for extensible state objects
31 * @author Ron Isaacson - Initial contribution
33 public abstract class PowermaxStateContainer {
35 protected final TimeZoneProvider timeZoneProvider;
36 protected List<Value<?>> values;
38 public abstract class Value<T> {
39 protected @Nullable T value;
40 protected final String channel;
42 public Value(PowermaxStateContainer parent, String channel) {
43 this.channel = channel;
46 parent.getValues().add(this);
53 public void setValue(@Nullable T value) {
57 @SuppressWarnings("unchecked")
58 public void setValueUnsafe(@Nullable Object value) {
59 this.value = (T) value;
62 public String getChannel() {
66 public abstract State getState();
69 public class DynamicValue<T> extends Value<T> {
70 Supplier<T> valueFunction;
71 Supplier<State> stateFunction;
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;
80 // Note: setValue() is still valid, but the saved value will be ignored
84 return valueFunction.get();
88 public State getState() {
89 return stateFunction.get();
93 public class BooleanValue extends Value<Boolean> {
97 public BooleanValue(PowermaxStateContainer parent, String channel, State trueState, State falseState) {
98 super(parent, channel);
99 this.trueState = trueState;
100 this.falseState = falseState;
103 public BooleanValue(PowermaxStateContainer parent, String channel) {
104 this(parent, channel, OnOffType.ON, OnOffType.OFF);
108 public State getState() {
109 return value ? trueState : falseState;
113 public class StringValue extends Value<String> {
114 public StringValue(PowermaxStateContainer parent, String channel) {
115 super(parent, channel);
119 public State getState() {
120 return new StringType(value);
124 public class DateTimeValue extends Value<Long> {
125 public DateTimeValue(PowermaxStateContainer parent, String channel) {
126 super(parent, channel);
130 public State getState() {
131 ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), timeZoneProvider.getTimeZone());
132 return new DateTimeType(zoned);
136 protected PowermaxStateContainer(TimeZoneProvider timeZoneProvider) {
137 this.timeZoneProvider = timeZoneProvider;
138 this.values = new ArrayList<>();
141 public List<Value<?>> getValues() {