From fb9a998f590dadd8f3918956cf9f4c525359fc30 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 10 Nov 2024 00:49:34 +0100 Subject: [PATCH] Simplify DateTimeType handling for SAICiSMART Signed-off-by: Jacob Laursen --- .../internal/ChargeStateUpdater.java | 4 +- .../internal/SAICiSMARTHandler.java | 40 +++++++------------ .../internal/SAICiSMARTHandlerFactory.java | 8 +--- .../internal/VehicleStateUpdater.java | 12 ++---- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/ChargeStateUpdater.java b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/ChargeStateUpdater.java index ebc3d9a556a..464b4da7188 100644 --- a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/ChargeStateUpdater.java +++ b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/ChargeStateUpdater.java @@ -17,7 +17,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants.CHANNEL_SOC; import java.net.URISyntaxException; -import java.time.ZonedDateTime; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -104,8 +103,7 @@ class ChargeStateUpdater implements Callable { saiCiSMARTHandler.updateState(CHANNEL_POWER, new QuantityType<>(power.intValue(), Units.WATT)); - saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_CHARGE_STATE_UPDATE, - new DateTimeType(ZonedDateTime.now(saiCiSMARTHandler.getTimeZone()))); + saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_CHARGE_STATE_UPDATE, new DateTimeType()); saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE); return chargingStatusResponseMessage.getApplicationData(); diff --git a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandler.java b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandler.java index 6d9f08c6069..1af347ff9e0 100644 --- a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandler.java +++ b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandler.java @@ -21,8 +21,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; @@ -33,7 +31,6 @@ import java.util.concurrent.TimeoutException; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.StringType; @@ -70,26 +67,22 @@ public class SAICiSMARTHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(SAICiSMARTHandler.class); - private final TimeZoneProvider timeZoneProvider; - @Nullable SAICiSMARTVehicleConfiguration config; private @Nullable Future pollingJob; - private ZonedDateTime lastAlarmMessage; - private ZonedDateTime lastCarActivity; + private Instant lastAlarmMessage; + private Instant lastCarActivity; /** * If the binding is initialized, treat the car as active (lastCarActivity = now) to get some first data. * * @param httpClientFactory - * @param timeZoneProvider * @param thing */ - public SAICiSMARTHandler(TimeZoneProvider timeZoneProvider, Thing thing) { + public SAICiSMARTHandler(Thing thing) { super(thing); - this.timeZoneProvider = timeZoneProvider; - lastAlarmMessage = ZonedDateTime.now(getTimeZone()); - lastCarActivity = ZonedDateTime.now(getTimeZone()); + lastAlarmMessage = Instant.now(); + lastCarActivity = Instant.now(); } @Override @@ -98,8 +91,8 @@ public class SAICiSMARTHandler extends BaseThingHandler { // reset channel to off updateState(CHANNEL_FORCE_REFRESH, OnOffType.from(false)); // update internal activity date, to query the car for about a minute - notifyCarActivity(ZonedDateTime.now(getTimeZone()).minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS - 1, - ChronoUnit.MINUTES), true); + notifyCarActivity( + Instant.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS - 1, ChronoUnit.MINUTES), true); } else if (channelUID.getId().equals(CHANNEL_SWITCH_AC) && command == OnOffType.ON) { // reset channel to off updateState(CHANNEL_SWITCH_AC, OnOffType.ON); @@ -121,7 +114,7 @@ public class SAICiSMARTHandler extends BaseThingHandler { } else if (channelUID.getId().equals(CHANNEL_LAST_ACTIVITY) && command instanceof DateTimeType commnadAsDateTimeType) { // update internal activity date from external date - notifyCarActivity(commnadAsDateTimeType.getZonedDateTime(), true); + notifyCarActivity(commnadAsDateTimeType.getInstant(), true); } } @@ -146,14 +139,14 @@ public class SAICiSMARTHandler extends BaseThingHandler { } // just started, make sure we start querying - notifyCarActivity(ZonedDateTime.now(getTimeZone()), true); + notifyCarActivity(Instant.now(), true); pollingJob = scheduler.scheduleWithFixedDelay(this::updateStatus, 2, SAICiSMARTBindingConstants.REFRESH_INTERVAL, TimeUnit.SECONDS); } private void updateStatus() { - if (lastCarActivity.isAfter( - ZonedDateTime.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS, ChronoUnit.MINUTES))) { + if (lastCarActivity + .isAfter(Instant.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS, ChronoUnit.MINUTES))) { if (this.getBridgeHandler().getUid() != null && this.getBridgeHandler().getToken() != null) { try { OTA_RVMVehicleStatusResp25857 otaRvmVehicleStatusResp25857 = new VehicleStateUpdater(this).call(); @@ -179,7 +172,7 @@ public class SAICiSMARTHandler extends BaseThingHandler { MessageCoder otaRvcReqMessageCoder = new MessageCoder<>(OTA_RVCReq.class); // we send a command end expect the car to wake up - notifyCarActivity(ZonedDateTime.now(getTimeZone()), false); + notifyCarActivity(Instant.now(), false); OTA_RVCReq req = new OTA_RVCReq(); req.setRvcReqType(new byte[] { 6 }); @@ -236,7 +229,7 @@ public class SAICiSMARTHandler extends BaseThingHandler { logger.trace("Got A/C message: {}", new GsonBuilder().setPrettyPrinting().create().toJson(enableACResponse)); } - public void notifyCarActivity(ZonedDateTime now, boolean force) { + public void notifyCarActivity(Instant now, boolean force) { // if the car activity changed, notify the channel if (force || lastCarActivity.isBefore(now)) { lastCarActivity = now; @@ -264,8 +257,7 @@ public class SAICiSMARTHandler extends BaseThingHandler { } public void handleMessage(Message message) { - ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochSecond(message.getMessageTime().getSeconds()), - getTimeZone()); + Instant time = Instant.ofEpochSecond(message.getMessageTime().getSeconds()); if (time.isAfter(lastAlarmMessage)) { lastAlarmMessage = time; @@ -276,8 +268,4 @@ public class SAICiSMARTHandler extends BaseThingHandler { notifyCarActivity(time, false); } - - public ZoneId getTimeZone() { - return timeZoneProvider.getTimeZone(); - } } diff --git a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandlerFactory.java b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandlerFactory.java index abaf4d9c526..27712338175 100644 --- a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandlerFactory.java +++ b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/SAICiSMARTHandlerFactory.java @@ -20,7 +20,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; import org.openhab.core.i18n.LocaleProvider; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Bridge; @@ -44,15 +43,12 @@ import org.osgi.service.component.annotations.Reference; public class SAICiSMARTHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_VEHICLE); - private final TimeZoneProvider timeZoneProvider; private HttpClient httpClient; @Activate public SAICiSMARTHandlerFactory(final @Reference TranslationProvider translationProvider, - final @Reference LocaleProvider localeProvider, final @Reference HttpClientFactory httpClientFactory, - final @Reference TimeZoneProvider timeZoneProvider) { + final @Reference LocaleProvider localeProvider, final @Reference HttpClientFactory httpClientFactory) { this.httpClient = httpClientFactory.getCommonHttpClient(); - this.timeZoneProvider = timeZoneProvider; } @Override @@ -67,7 +63,7 @@ public class SAICiSMARTHandlerFactory extends BaseThingHandlerFactory { if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) { return new SAICiSMARTBridgeHandler((Bridge) thing, httpClient); } else if (THING_TYPE_VEHICLE.equals(thingTypeUID)) { - return new SAICiSMARTHandler(timeZoneProvider, thing); + return new SAICiSMARTHandler(thing); } return null; diff --git a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/VehicleStateUpdater.java b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/VehicleStateUpdater.java index 2a66e3ec9f1..98e66333067 100644 --- a/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/VehicleStateUpdater.java +++ b/bundles/org.openhab.binding.saicismart/src/main/java/org/openhab/binding/saicismart/internal/VehicleStateUpdater.java @@ -24,7 +24,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants import java.net.URISyntaxException; import java.time.Instant; -import java.time.ZonedDateTime; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -224,16 +223,13 @@ class VehicleStateUpdater implements Callable { saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_REMOTE_AC_STATUS, new DecimalType( chargingStatusResponseMessage.getApplicationData().getBasicVehicleStatus().getRemoteClimateStatus())); - saiCiSMARTHandler - .updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_POSITION_UPDATE, - new DateTimeType(ZonedDateTime.ofInstant( - Instant.ofEpochSecond(chargingStatusResponseMessage.getApplicationData() - .getGpsPosition().getTimestamp4Short().getSeconds()), - saiCiSMARTHandler.getTimeZone()))); + saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_POSITION_UPDATE, + new DateTimeType(Instant.ofEpochSecond(chargingStatusResponseMessage.getApplicationData() + .getGpsPosition().getTimestamp4Short().getSeconds()))); if (isCharging || acActive || engineRunning) { // update activity date - saiCiSMARTHandler.notifyCarActivity(ZonedDateTime.now(saiCiSMARTHandler.getTimeZone()), true); + saiCiSMARTHandler.notifyCarActivity(Instant.now(), true); } saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE);