Simplify DateTimeType handling for IHC

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-11-09 22:24:31 +01:00 committed by Ciprian Pascu
parent d671886de1
commit 8509293b14
4 changed files with 14 additions and 16 deletions

View File

@ -12,10 +12,9 @@
*/
package org.openhab.binding.ihc.internal.converters;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
@ -34,13 +33,13 @@ public class DateTimeTypeWSDateValueConverter implements Converter<WSDateValue,
public DateTimeType convertFromResourceValue(@NonNull WSDateValue from,
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
Calendar cal = dateTimeToCalendar(from, null);
return new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), TimeZone.getDefault().toZoneId()));
return new DateTimeType(cal.toInstant());
}
@Override
public WSDateValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSDateValue value,
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
Calendar cal = GregorianCalendar.from(from.getZonedDateTime());
Calendar cal = GregorianCalendar.from(from.getZonedDateTime(ZoneId.systemDefault()));
short year = (short) cal.get(Calendar.YEAR);
byte month = (byte) (cal.get(Calendar.MONTH) + 1);
byte day = (byte) cal.get(Calendar.DAY_OF_MONTH);

View File

@ -12,10 +12,9 @@
*/
package org.openhab.binding.ihc.internal.converters;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
@ -34,13 +33,13 @@ public class DateTimeTypeWSTimeValueConverter implements Converter<WSTimeValue,
public DateTimeType convertFromResourceValue(@NonNull WSTimeValue from,
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
Calendar cal = dateTimeToCalendar(null, from);
return new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), TimeZone.getDefault().toZoneId()));
return new DateTimeType(cal.toInstant());
}
@Override
public WSTimeValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSTimeValue value,
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
Calendar cal = GregorianCalendar.from(from.getZonedDateTime());
Calendar cal = GregorianCalendar.from(from.getZonedDateTime(ZoneId.systemDefault()));
return new WSTimeValue(value.resourceID, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
cal.get(Calendar.SECOND));
}

View File

@ -17,9 +17,9 @@ import static org.openhab.binding.ihc.internal.IhcBindingConstants.*;
import java.io.File;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -343,8 +343,8 @@ public class IhcHandler extends BaseThingHandler implements IhcEventListener {
WSTimeManagerSettings timeSettings = ihc.getTimeSettings();
logger.debug("Controller time settings: {}", timeSettings);
ZonedDateTime time = timeSettings.getTimeAndDateInUTC().getAsZonedDateTime(ZoneId.of("Z"))
.withZoneSameInstant(ZoneId.systemDefault());
Instant time = timeSettings.getTimeAndDateInUTC().getAsInstant(ZoneId.of("Z"));
updateState(new ChannelUID(getThing().getUID(), CHANNEL_CONTROLLER_TIME), new DateTimeType(time));
} catch (IhcExecption e) {
logger.warn("Controller uptime information fetch failed, reason: {}.", e.getMessage(), e);

View File

@ -12,9 +12,9 @@
*/
package org.openhab.binding.ihc.internal.ws.datatypes;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* Class for WSDate complex type.
@ -159,11 +159,11 @@ public class WSDate {
}
/**
* Gets WSDate as ZonedDateTime.
* Gets WSDate as {@link Instant}.
*
* @return LocalDateTime
* @return Instant
*/
public ZonedDateTime getAsZonedDateTime(ZoneId zoneId) {
return ZonedDateTime.of(getAsLocalDateTime(), zoneId);
public Instant getAsInstant(ZoneId zoneId) {
return getAsLocalDateTime().atZone(zoneId).toInstant();
}
}