mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
Simplify DateTimeType handling for IHC
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
fb7ba1feb1
commit
e898312e9e
@ -12,10 +12,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.ihc.internal.converters;
|
package org.openhab.binding.ihc.internal.converters;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZoneId;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.TimeZone;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
|
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,
|
public DateTimeType convertFromResourceValue(@NonNull WSDateValue from,
|
||||||
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
||||||
Calendar cal = dateTimeToCalendar(from, null);
|
Calendar cal = dateTimeToCalendar(from, null);
|
||||||
return new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), TimeZone.getDefault().toZoneId()));
|
return new DateTimeType(cal.toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WSDateValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSDateValue value,
|
public WSDateValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSDateValue value,
|
||||||
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
@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);
|
short year = (short) cal.get(Calendar.YEAR);
|
||||||
byte month = (byte) (cal.get(Calendar.MONTH) + 1);
|
byte month = (byte) (cal.get(Calendar.MONTH) + 1);
|
||||||
byte day = (byte) cal.get(Calendar.DAY_OF_MONTH);
|
byte day = (byte) cal.get(Calendar.DAY_OF_MONTH);
|
||||||
|
@ -12,10 +12,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.ihc.internal.converters;
|
package org.openhab.binding.ihc.internal.converters;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZoneId;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.TimeZone;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
|
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,
|
public DateTimeType convertFromResourceValue(@NonNull WSTimeValue from,
|
||||||
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
||||||
Calendar cal = dateTimeToCalendar(null, from);
|
Calendar cal = dateTimeToCalendar(null, from);
|
||||||
return new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), TimeZone.getDefault().toZoneId()));
|
return new DateTimeType(cal.toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WSTimeValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSTimeValue value,
|
public WSTimeValue convertFromOHType(@NonNull DateTimeType from, @NonNull WSTimeValue value,
|
||||||
@NonNull ConverterAdditionalInfo convertData) throws ConversionException {
|
@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),
|
return new WSTimeValue(value.resourceID, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
|
||||||
cal.get(Calendar.SECOND));
|
cal.get(Calendar.SECOND));
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,9 @@ import static org.openhab.binding.ihc.internal.IhcBindingConstants.*;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -343,8 +343,8 @@ public class IhcHandler extends BaseThingHandler implements IhcEventListener {
|
|||||||
WSTimeManagerSettings timeSettings = ihc.getTimeSettings();
|
WSTimeManagerSettings timeSettings = ihc.getTimeSettings();
|
||||||
logger.debug("Controller time settings: {}", timeSettings);
|
logger.debug("Controller time settings: {}", timeSettings);
|
||||||
|
|
||||||
ZonedDateTime time = timeSettings.getTimeAndDateInUTC().getAsZonedDateTime(ZoneId.of("Z"))
|
Instant time = timeSettings.getTimeAndDateInUTC().getAsInstant(ZoneId.of("Z"));
|
||||||
.withZoneSameInstant(ZoneId.systemDefault());
|
|
||||||
updateState(new ChannelUID(getThing().getUID(), CHANNEL_CONTROLLER_TIME), new DateTimeType(time));
|
updateState(new ChannelUID(getThing().getUID(), CHANNEL_CONTROLLER_TIME), new DateTimeType(time));
|
||||||
} catch (IhcExecption e) {
|
} catch (IhcExecption e) {
|
||||||
logger.warn("Controller uptime information fetch failed, reason: {}.", e.getMessage(), e);
|
logger.warn("Controller uptime information fetch failed, reason: {}.", e.getMessage(), e);
|
||||||
|
@ -12,9 +12,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.ihc.internal.ws.datatypes;
|
package org.openhab.binding.ihc.internal.ws.datatypes;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for WSDate complex type.
|
* 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) {
|
public Instant getAsInstant(ZoneId zoneId) {
|
||||||
return ZonedDateTime.of(getAsLocalDateTime(), zoneId);
|
return getAsLocalDateTime().atZone(zoneId).toInstant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user