]> git.basschouten.com Git - openhab-addons.git/blob
6d02dca6d8baa142068c0f312525266000db2bda
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.siemenshvac.internal.converter.type;
14
15 import java.time.ZonedDateTime;
16 import java.util.Locale;
17
18 import javax.measure.Unit;
19 import javax.measure.quantity.Time;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.siemenshvac.internal.converter.ConverterException;
24 import org.openhab.binding.siemenshvac.internal.metadata.SiemensHvacMetadataDataPoint;
25 import org.openhab.core.i18n.TimeZoneProvider;
26 import org.openhab.core.library.CoreItemFactory;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.type.ChannelType;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.Type;
33
34 import com.google.gson.JsonElement;
35
36 /**
37  * Converts between a SiemensHvac datapoint value and an openHAB DecimalType.
38  *
39  * @author Laurent Arnal - Initial contribution
40  */
41 @NonNullByDefault
42 public class TimeOfDayTypeConverter extends AbstractTypeConverter {
43     private final TimeZoneProvider timeZoneProvider;
44
45     public TimeOfDayTypeConverter(final TimeZoneProvider timeZoneProvider) {
46         this.timeZoneProvider = timeZoneProvider;
47     }
48
49     @Override
50     protected boolean toBindingValidation(Type type) {
51         return true;
52     }
53
54     @Override
55     protected @Nullable Object toBinding(Type type, ChannelType tp) throws ConverterException {
56         Object valUpdate = null;
57
58         if (type instanceof DateTimeType dateTime) {
59             valUpdate = dateTime.toString();
60         }
61
62         return valUpdate;
63     }
64
65     @Override
66     protected boolean fromBindingValidation(JsonElement value, String unit, String type) {
67         return true;
68     }
69
70     @Override
71     protected State fromBinding(JsonElement value, String unit, String type, ChannelType tp, Locale locale)
72             throws ConverterException {
73         if ("----".equals(value.getAsString())) {
74             return new DateTimeType(ZonedDateTime.now(this.timeZoneProvider.getTimeZone()));
75         } else {
76
77             if (unit.equals("h:m")) {
78                 String st = value.getAsString();
79                 String[] parts = st.split(":");
80                 int h = Integer.parseInt(parts[0]);
81                 int m = Integer.parseInt(parts[1]);
82
83                 Unit<Time> targetUnit = Units.MINUTE;
84                 return new QuantityType<>(h * 60 + m, targetUnit);
85
86             } else if (unit.equals("m:s")) {
87                 String st = value.getAsString();
88                 String[] parts = st.split(":");
89                 int m = Integer.parseInt(parts[0]);
90                 int s = Integer.parseInt(parts[1]);
91
92                 Unit<Time> targetUnit = Units.SECOND;
93                 return new QuantityType<>(m * 60 + s, targetUnit);
94
95             } else if (unit.equals("h")) {
96                 int val = Integer.parseInt(value.getAsString());
97
98                 Unit<Time> targetUnit = Units.HOUR;
99                 return new QuantityType<>(val, targetUnit);
100
101             } else {
102                 throw new ConverterException("unsupported unit type:" + unit);
103             }
104         }
105     }
106
107     @Override
108     public String getChannelType(SiemensHvacMetadataDataPoint dpt) {
109         return "number";
110     }
111
112     @Override
113     public String getItemType(SiemensHvacMetadataDataPoint dpt) {
114         return CoreItemFactory.NUMBER + ":Time";
115     }
116
117     @Override
118     public boolean hasVariant() {
119         return false;
120     }
121 }