]> git.basschouten.com Git - openhab-addons.git/blob
f6b4ed304ca11d00258814d656cc531c4b4b5f0f
[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.semsportal.internal;
14
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;
25
26 /**
27  * Helper class to convert the POJOs of the SEMS portal response classes into openHAB State objects.
28  *
29  * @author Iwan Bron - Initial contribution
30  */
31 @NonNullByDefault
32 public class StateHelper {
33
34     private StateHelper() {
35         // hide constructor, no initialisation possible
36     }
37
38     public static State getOperational(@Nullable StationStatus currentStatus) {
39         if (currentStatus == null) {
40             return UnDefType.UNDEF;
41         }
42         return OnOffType.from(currentStatus.isOperational());
43     }
44
45     public static State getLastUpdate(@Nullable StationStatus currentStatus) {
46         return currentStatus == null ? UnDefType.UNDEF : new DateTimeType(currentStatus.getLastUpdate());
47     }
48
49     public static State getCurrentOutput(@Nullable StationStatus status) {
50         if (status == null) {
51             return UnDefType.UNDEF;
52         }
53         if (status.getCurrentOutput() == null) {
54             return UnDefType.NULL;
55         }
56         return new QuantityType<>(status.getCurrentOutput(), Units.WATT);
57     }
58
59     public static State getDayTotal(@Nullable StationStatus status) {
60         if (status == null) {
61             return UnDefType.UNDEF;
62         }
63         if (status.getDayTotal() == null) {
64             return UnDefType.NULL;
65         }
66         return new QuantityType<>(status.getDayTotal(), Units.KILOWATT_HOUR);
67     }
68
69     public static State getMonthTotal(@Nullable StationStatus status) {
70         if (status == null) {
71             return UnDefType.UNDEF;
72         }
73         if (status.getMonthTotal() == null) {
74             return UnDefType.NULL;
75         }
76         return new QuantityType<>(status.getMonthTotal(), Units.KILOWATT_HOUR);
77     }
78
79     public static State getOverallTotal(@Nullable StationStatus status) {
80         if (status == null) {
81             return UnDefType.UNDEF;
82         }
83         if (status.getOverallTotal() == null) {
84             return UnDefType.NULL;
85         }
86         return new QuantityType<>(status.getOverallTotal(), Units.KILOWATT_HOUR);
87     }
88
89     public static State getDayIncome(@Nullable StationStatus status) {
90         if (status == null) {
91             return UnDefType.UNDEF;
92         }
93         if (status.getDayIncome() == null) {
94             return UnDefType.NULL;
95         }
96         return new DecimalType(status.getDayIncome());
97     }
98
99     public static State getTotalIncome(@Nullable StationStatus status) {
100         if (status == null) {
101             return UnDefType.UNDEF;
102         }
103         if (status.getTotalIncome() == null) {
104             return UnDefType.NULL;
105         }
106         return new DecimalType(status.getTotalIncome());
107     }
108 }