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.semsportal.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.semsportal.internal.dto.StationStatus;
18 import org.openhab.core.library.types.DateTimeType;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
27 * Helper class to convert the POJOs of the SEMS portal response classes into openHAB State objects.
29 * @author Iwan Bron - Initial contribution
32 public class StateHelper {
34 private StateHelper() {
35 // hide constructor, no initialisation possible
38 public static State getOperational(@Nullable StationStatus currentStatus) {
39 if (currentStatus == null) {
40 return UnDefType.UNDEF;
42 return OnOffType.from(currentStatus.isOperational());
45 public static State getLastUpdate(@Nullable StationStatus currentStatus) {
46 return currentStatus == null ? UnDefType.UNDEF : new DateTimeType(currentStatus.getLastUpdate());
49 public static State getCurrentOutput(@Nullable StationStatus status) {
51 return UnDefType.UNDEF;
53 if (status.getCurrentOutput() == null) {
54 return UnDefType.NULL;
56 return new QuantityType<>(status.getCurrentOutput(), Units.WATT);
59 public static State getDayTotal(@Nullable StationStatus status) {
61 return UnDefType.UNDEF;
63 if (status.getDayTotal() == null) {
64 return UnDefType.NULL;
66 return new QuantityType<>(status.getDayTotal(), Units.KILOWATT_HOUR);
69 public static State getMonthTotal(@Nullable StationStatus status) {
71 return UnDefType.UNDEF;
73 if (status.getMonthTotal() == null) {
74 return UnDefType.NULL;
76 return new QuantityType<>(status.getMonthTotal(), Units.KILOWATT_HOUR);
79 public static State getOverallTotal(@Nullable StationStatus status) {
81 return UnDefType.UNDEF;
83 if (status.getOverallTotal() == null) {
84 return UnDefType.NULL;
86 return new QuantityType<>(status.getOverallTotal(), Units.KILOWATT_HOUR);
89 public static State getDayIncome(@Nullable StationStatus status) {
91 return UnDefType.UNDEF;
93 if (status.getDayIncome() == null) {
94 return UnDefType.NULL;
96 return new DecimalType(status.getDayIncome());
99 public static State getTotalIncome(@Nullable StationStatus status) {
100 if (status == null) {
101 return UnDefType.UNDEF;
103 if (status.getTotalIncome() == null) {
104 return UnDefType.NULL;
106 return new DecimalType(status.getTotalIncome());