2 * Copyright (c) 2010-2024 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.siemenshvac.internal.converter.type;
15 import java.time.ZonedDateTime;
16 import java.util.Locale;
18 import javax.measure.Unit;
19 import javax.measure.quantity.Time;
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;
34 import com.google.gson.JsonElement;
37 * Converts between a SiemensHvac datapoint value and an openHAB DecimalType.
39 * @author Laurent Arnal - Initial contribution
42 public class TimeOfDayTypeConverter extends AbstractTypeConverter {
43 private final TimeZoneProvider timeZoneProvider;
45 public TimeOfDayTypeConverter(final TimeZoneProvider timeZoneProvider) {
46 this.timeZoneProvider = timeZoneProvider;
50 protected boolean toBindingValidation(Type type) {
55 protected @Nullable Object toBinding(Type type, ChannelType tp) throws ConverterException {
56 Object valUpdate = null;
58 if (type instanceof DateTimeType dateTime) {
59 valUpdate = dateTime.toString();
66 protected boolean fromBindingValidation(JsonElement value, String unit, String type) {
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()));
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]);
83 Unit<Time> targetUnit = Units.MINUTE;
84 return new QuantityType<>(h * 60 + m, targetUnit);
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]);
92 Unit<Time> targetUnit = Units.SECOND;
93 return new QuantityType<>(m * 60 + s, targetUnit);
95 } else if (unit.equals("h")) {
96 int val = Integer.parseInt(value.getAsString());
98 Unit<Time> targetUnit = Units.HOUR;
99 return new QuantityType<>(val, targetUnit);
102 throw new ConverterException("unsupported unit type:" + unit);
108 public String getChannelType(SiemensHvacMetadataDataPoint dpt) {
113 public String getItemType(SiemensHvacMetadataDataPoint dpt) {
114 return CoreItemFactory.NUMBER + ":Time";
118 public boolean hasVariant() {