Simplify DateTimeType handling for SAICiSMART

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-10 00:49:34 +01:00 committed by lsiepel
parent 1af326dbb7
commit fb9a998f59
4 changed files with 21 additions and 43 deletions

View File

@ -17,7 +17,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants
import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants.CHANNEL_SOC; import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants.CHANNEL_SOC;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.time.ZonedDateTime;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -104,8 +103,7 @@ class ChargeStateUpdater implements Callable<OTA_ChrgMangDataResp> {
saiCiSMARTHandler.updateState(CHANNEL_POWER, new QuantityType<>(power.intValue(), Units.WATT)); saiCiSMARTHandler.updateState(CHANNEL_POWER, new QuantityType<>(power.intValue(), Units.WATT));
saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_CHARGE_STATE_UPDATE, saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_CHARGE_STATE_UPDATE, new DateTimeType());
new DateTimeType(ZonedDateTime.now(saiCiSMARTHandler.getTimeZone())));
saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE); saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE);
return chargingStatusResponseMessage.getApplicationData(); return chargingStatusResponseMessage.getApplicationData();

View File

@ -21,8 +21,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -33,7 +31,6 @@ import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; 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.DateTimeType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType; 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 Logger logger = LoggerFactory.getLogger(SAICiSMARTHandler.class);
private final TimeZoneProvider timeZoneProvider;
@Nullable @Nullable
SAICiSMARTVehicleConfiguration config; SAICiSMARTVehicleConfiguration config;
private @Nullable Future<?> pollingJob; private @Nullable Future<?> pollingJob;
private ZonedDateTime lastAlarmMessage; private Instant lastAlarmMessage;
private ZonedDateTime lastCarActivity; private Instant lastCarActivity;
/** /**
* If the binding is initialized, treat the car as active (lastCarActivity = now) to get some first data. * If the binding is initialized, treat the car as active (lastCarActivity = now) to get some first data.
* *
* @param httpClientFactory * @param httpClientFactory
* @param timeZoneProvider
* @param thing * @param thing
*/ */
public SAICiSMARTHandler(TimeZoneProvider timeZoneProvider, Thing thing) { public SAICiSMARTHandler(Thing thing) {
super(thing); super(thing);
this.timeZoneProvider = timeZoneProvider; lastAlarmMessage = Instant.now();
lastAlarmMessage = ZonedDateTime.now(getTimeZone()); lastCarActivity = Instant.now();
lastCarActivity = ZonedDateTime.now(getTimeZone());
} }
@Override @Override
@ -98,8 +91,8 @@ public class SAICiSMARTHandler extends BaseThingHandler {
// reset channel to off // reset channel to off
updateState(CHANNEL_FORCE_REFRESH, OnOffType.from(false)); updateState(CHANNEL_FORCE_REFRESH, OnOffType.from(false));
// update internal activity date, to query the car for about a minute // update internal activity date, to query the car for about a minute
notifyCarActivity(ZonedDateTime.now(getTimeZone()).minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS - 1, notifyCarActivity(
ChronoUnit.MINUTES), true); Instant.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS - 1, ChronoUnit.MINUTES), true);
} else if (channelUID.getId().equals(CHANNEL_SWITCH_AC) && command == OnOffType.ON) { } else if (channelUID.getId().equals(CHANNEL_SWITCH_AC) && command == OnOffType.ON) {
// reset channel to off // reset channel to off
updateState(CHANNEL_SWITCH_AC, OnOffType.ON); updateState(CHANNEL_SWITCH_AC, OnOffType.ON);
@ -121,7 +114,7 @@ public class SAICiSMARTHandler extends BaseThingHandler {
} else if (channelUID.getId().equals(CHANNEL_LAST_ACTIVITY) } else if (channelUID.getId().equals(CHANNEL_LAST_ACTIVITY)
&& command instanceof DateTimeType commnadAsDateTimeType) { && command instanceof DateTimeType commnadAsDateTimeType) {
// update internal activity date from external date // 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 // just started, make sure we start querying
notifyCarActivity(ZonedDateTime.now(getTimeZone()), true); notifyCarActivity(Instant.now(), true);
pollingJob = scheduler.scheduleWithFixedDelay(this::updateStatus, 2, pollingJob = scheduler.scheduleWithFixedDelay(this::updateStatus, 2,
SAICiSMARTBindingConstants.REFRESH_INTERVAL, TimeUnit.SECONDS); SAICiSMARTBindingConstants.REFRESH_INTERVAL, TimeUnit.SECONDS);
} }
private void updateStatus() { private void updateStatus() {
if (lastCarActivity.isAfter( if (lastCarActivity
ZonedDateTime.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS, ChronoUnit.MINUTES))) { .isAfter(Instant.now().minus(SAICiSMARTBindingConstants.POLLING_ACTIVE_MINS, ChronoUnit.MINUTES))) {
if (this.getBridgeHandler().getUid() != null && this.getBridgeHandler().getToken() != null) { if (this.getBridgeHandler().getUid() != null && this.getBridgeHandler().getToken() != null) {
try { try {
OTA_RVMVehicleStatusResp25857 otaRvmVehicleStatusResp25857 = new VehicleStateUpdater(this).call(); OTA_RVMVehicleStatusResp25857 otaRvmVehicleStatusResp25857 = new VehicleStateUpdater(this).call();
@ -179,7 +172,7 @@ public class SAICiSMARTHandler extends BaseThingHandler {
MessageCoder<OTA_RVCReq> otaRvcReqMessageCoder = new MessageCoder<>(OTA_RVCReq.class); MessageCoder<OTA_RVCReq> otaRvcReqMessageCoder = new MessageCoder<>(OTA_RVCReq.class);
// we send a command end expect the car to wake up // 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(); OTA_RVCReq req = new OTA_RVCReq();
req.setRvcReqType(new byte[] { 6 }); 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)); 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 the car activity changed, notify the channel
if (force || lastCarActivity.isBefore(now)) { if (force || lastCarActivity.isBefore(now)) {
lastCarActivity = now; lastCarActivity = now;
@ -264,8 +257,7 @@ public class SAICiSMARTHandler extends BaseThingHandler {
} }
public void handleMessage(Message message) { public void handleMessage(Message message) {
ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochSecond(message.getMessageTime().getSeconds()), Instant time = Instant.ofEpochSecond(message.getMessageTime().getSeconds());
getTimeZone());
if (time.isAfter(lastAlarmMessage)) { if (time.isAfter(lastAlarmMessage)) {
lastAlarmMessage = time; lastAlarmMessage = time;
@ -276,8 +268,4 @@ public class SAICiSMARTHandler extends BaseThingHandler {
notifyCarActivity(time, false); notifyCarActivity(time, false);
} }
public ZoneId getTimeZone() {
return timeZoneProvider.getTimeZone();
}
} }

View File

@ -20,7 +20,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
@ -44,15 +43,12 @@ import org.osgi.service.component.annotations.Reference;
public class SAICiSMARTHandlerFactory extends BaseThingHandlerFactory { public class SAICiSMARTHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_VEHICLE); private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_VEHICLE);
private final TimeZoneProvider timeZoneProvider;
private HttpClient httpClient; private HttpClient httpClient;
@Activate @Activate
public SAICiSMARTHandlerFactory(final @Reference TranslationProvider translationProvider, public SAICiSMARTHandlerFactory(final @Reference TranslationProvider translationProvider,
final @Reference LocaleProvider localeProvider, final @Reference HttpClientFactory httpClientFactory, final @Reference LocaleProvider localeProvider, final @Reference HttpClientFactory httpClientFactory) {
final @Reference TimeZoneProvider timeZoneProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient(); this.httpClient = httpClientFactory.getCommonHttpClient();
this.timeZoneProvider = timeZoneProvider;
} }
@Override @Override
@ -67,7 +63,7 @@ public class SAICiSMARTHandlerFactory extends BaseThingHandlerFactory {
if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) { if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
return new SAICiSMARTBridgeHandler((Bridge) thing, httpClient); return new SAICiSMARTBridgeHandler((Bridge) thing, httpClient);
} else if (THING_TYPE_VEHICLE.equals(thingTypeUID)) { } else if (THING_TYPE_VEHICLE.equals(thingTypeUID)) {
return new SAICiSMARTHandler(timeZoneProvider, thing); return new SAICiSMARTHandler(thing);
} }
return null; return null;

View File

@ -24,7 +24,6 @@ import static org.openhab.binding.saicismart.internal.SAICiSMARTBindingConstants
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -224,16 +223,13 @@ class VehicleStateUpdater implements Callable<OTA_RVMVehicleStatusResp25857> {
saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_REMOTE_AC_STATUS, new DecimalType( saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_REMOTE_AC_STATUS, new DecimalType(
chargingStatusResponseMessage.getApplicationData().getBasicVehicleStatus().getRemoteClimateStatus())); chargingStatusResponseMessage.getApplicationData().getBasicVehicleStatus().getRemoteClimateStatus()));
saiCiSMARTHandler saiCiSMARTHandler.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_POSITION_UPDATE,
.updateState(SAICiSMARTBindingConstants.CHANNEL_LAST_POSITION_UPDATE, new DateTimeType(Instant.ofEpochSecond(chargingStatusResponseMessage.getApplicationData()
new DateTimeType(ZonedDateTime.ofInstant( .getGpsPosition().getTimestamp4Short().getSeconds())));
Instant.ofEpochSecond(chargingStatusResponseMessage.getApplicationData()
.getGpsPosition().getTimestamp4Short().getSeconds()),
saiCiSMARTHandler.getTimeZone())));
if (isCharging || acActive || engineRunning) { if (isCharging || acActive || engineRunning) {
// update activity date // update activity date
saiCiSMARTHandler.notifyCarActivity(ZonedDateTime.now(saiCiSMARTHandler.getTimeZone()), true); saiCiSMARTHandler.notifyCarActivity(Instant.now(), true);
} }
saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE); saiCiSMARTHandler.updateStatus(ThingStatus.ONLINE);