Simplify DateTimeType handling for EVCC
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>pull/17928/head
parent
d5d01b0623
commit
68527c03f5
|
@ -14,8 +14,10 @@ package org.openhab.binding.evcc.internal;
|
||||||
|
|
||||||
import static org.openhab.binding.evcc.internal.EvccBindingConstants.*;
|
import static org.openhab.binding.evcc.internal.EvccBindingConstants.*;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -79,7 +81,7 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
private Set<String> vehicleFeatureHeating = new HashSet<String>();
|
private Set<String> vehicleFeatureHeating = new HashSet<String>();
|
||||||
private Set<String> loadpointFeatureHeating = new HashSet<String>();
|
private Set<String> loadpointFeatureHeating = new HashSet<String>();
|
||||||
|
|
||||||
Map<String, Triple<Boolean, Float, ZonedDateTime>> vehiclePlans = new HashMap<>();
|
Map<String, Triple<Boolean, Float, Instant>> vehiclePlans = new HashMap<>();
|
||||||
|
|
||||||
public EvccHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
|
public EvccHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
|
||||||
super(thing);
|
super(thing);
|
||||||
|
@ -284,7 +286,7 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case CHANNEL_VEHICLE_PLAN_ENABLED, CHANNEL_HEATING_PLAN_ENABLED -> {
|
case CHANNEL_VEHICLE_PLAN_ENABLED, CHANNEL_HEATING_PLAN_ENABLED -> {
|
||||||
Triple<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
|
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
|
||||||
if (command == OnOffType.ON) {
|
if (command == OnOffType.ON) {
|
||||||
evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(),
|
evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(),
|
||||||
planValues.getRight());
|
planValues.getRight());
|
||||||
|
@ -299,7 +301,7 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case CHANNEL_VEHICLE_PLAN_SOC -> {
|
case CHANNEL_VEHICLE_PLAN_SOC -> {
|
||||||
Triple<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
|
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
|
||||||
if (command instanceof QuantityType<?> qt) {
|
if (command instanceof QuantityType<?> qt) {
|
||||||
vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(),
|
vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(),
|
||||||
qt.toUnit(Units.PERCENT).floatValue(), planValues.getRight()));
|
qt.toUnit(Units.PERCENT).floatValue(), planValues.getRight()));
|
||||||
|
@ -318,7 +320,7 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case CHANNEL_HEATING_PLAN_TEMPERATURE -> {
|
case CHANNEL_HEATING_PLAN_TEMPERATURE -> {
|
||||||
Triple<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
|
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
|
||||||
if (command instanceof QuantityType<?> qt) {
|
if (command instanceof QuantityType<?> qt) {
|
||||||
vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(),
|
vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(),
|
||||||
qt.toUnit(SIUnits.CELSIUS).floatValue(), planValues.getRight()));
|
qt.toUnit(SIUnits.CELSIUS).floatValue(), planValues.getRight()));
|
||||||
|
@ -337,14 +339,14 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case CHANNEL_VEHICLE_PLAN_TIME, CHANNEL_HEATING_PLAN_TIME -> {
|
case CHANNEL_VEHICLE_PLAN_TIME, CHANNEL_HEATING_PLAN_TIME -> {
|
||||||
Triple<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
|
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
|
||||||
if (command instanceof DateTimeType dtt) {
|
if (command instanceof DateTimeType dtt) {
|
||||||
vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(), planValues.getMiddle(),
|
vehiclePlans.put(vehicleName,
|
||||||
dtt.getZonedDateTime()));
|
new Triple<>(planValues.getLeft(), planValues.getMiddle(), dtt.getInstant()));
|
||||||
if (planValues.getLeft()) {
|
if (planValues.getLeft()) {
|
||||||
try {
|
try {
|
||||||
evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(),
|
evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(),
|
||||||
dtt.getZonedDateTime());
|
dtt.getInstant());
|
||||||
} catch (DateTimeParseException e) {
|
} catch (DateTimeParseException e) {
|
||||||
logger.debug("Failed to set vehicle plan time: ", e);
|
logger.debug("Failed to set vehicle plan time: ", e);
|
||||||
}
|
}
|
||||||
|
@ -967,15 +969,16 @@ public class EvccHandler extends BaseThingHandler {
|
||||||
plan = vehicle.getPlan();
|
plan = vehicle.getPlan();
|
||||||
}
|
}
|
||||||
if (plan == null && vehiclePlans.get(vehicleName) == null) {
|
if (plan == null && vehiclePlans.get(vehicleName) == null) {
|
||||||
vehiclePlans.put(vehicleName, new Triple<>(false, 100f, ZonedDateTime.now().plusHours(12)));
|
vehiclePlans.put(vehicleName, new Triple<>(false, 100f, Instant.now().plus(12, ChronoUnit.HOURS)));
|
||||||
} else if (plan != null) {
|
} else if (plan != null) {
|
||||||
vehiclePlans.put(vehicleName, new Triple<>(true, plan.getSoC(), ZonedDateTime.parse(plan.getTime())));
|
vehiclePlans.put(vehicleName,
|
||||||
|
new Triple<>(true, plan.getSoC(), ZonedDateTime.parse(plan.getTime()).toInstant()));
|
||||||
}
|
}
|
||||||
updateVehiclePlanChannel(uid, vehicleName, channelGroup, isHeating);
|
updateVehiclePlanChannel(uid, vehicleName, channelGroup, isHeating);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateVehiclePlanChannel(ThingUID uid, String vehicleName, String channelGroup, boolean isHeating) {
|
private void updateVehiclePlanChannel(ThingUID uid, String vehicleName, String channelGroup, boolean isHeating) {
|
||||||
Triple<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
|
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
|
||||||
|
|
||||||
if (isHeating) {
|
if (isHeating) {
|
||||||
ChannelUID channel = new ChannelUID(uid, channelGroup, CHANNEL_HEATING_PLAN_ENABLED);
|
ChannelUID channel = new ChannelUID(uid, channelGroup, CHANNEL_HEATING_PLAN_ENABLED);
|
||||||
|
|
|
@ -16,7 +16,7 @@ import static org.openhab.binding.evcc.internal.EvccBindingConstants.EVCC_REST_A
|
||||||
import static org.openhab.binding.evcc.internal.EvccBindingConstants.LONG_CONNECTION_TIMEOUT_MILLISEC;
|
import static org.openhab.binding.evcc.internal.EvccBindingConstants.LONG_CONNECTION_TIMEOUT_MILLISEC;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.ZoneId;
|
import java.time.Instant;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
@ -147,9 +147,8 @@ public class EvccAPI {
|
||||||
return httpRequest(this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/limitsoc/" + limitSoC, "POST");
|
return httpRequest(this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/limitsoc/" + limitSoC, "POST");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String setVehiclePlan(String vehicleName, int planSoC, ZonedDateTime planTime) throws EvccApiException {
|
public String setVehiclePlan(String vehicleName, int planSoC, Instant planTime) throws EvccApiException {
|
||||||
ZoneId zoneId = timeZoneProvider.getTimeZone();
|
ZonedDateTime adjustedTime = planTime.atZone(timeZoneProvider.getTimeZone());
|
||||||
ZonedDateTime adjustedTime = planTime.withZoneSameInstant(zoneId);
|
|
||||||
String formattedTime = adjustedTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
String formattedTime = adjustedTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||||
return httpRequest(
|
return httpRequest(
|
||||||
this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" + formattedTime,
|
this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" + formattedTime,
|
||||||
|
|
Loading…
Reference in New Issue