[evcc] Fix data type issues in DTO (#13710)

* [evcc] Change DTO from double to float as the higher precision is not required
* [evcc] Change DTO from int/long to float
* [evcc] Update JavaDoc & small improvements
* [evcc] `EvccHandler`: Add exception message to the debug log

Fixes https://github.com/openhab/openhab-addons/issues/13646.
Avoids problems with changed data types in the future by generally using float instead of int.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2022-11-13 12:54:00 +01:00 committed by GitHub
parent 71d1226505
commit aee4d31d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 71 deletions

View File

@ -66,7 +66,7 @@ public class EvccHandler extends BaseThingHandler {
private boolean gridConfigured = false;
private boolean pvConfigured = false;
private int targetSoC = 100;
private float targetSoC = 100;
private boolean targetTimeEnabled = false;
private ZonedDateTime targetTimeZDT = ZonedDateTime.now().plusHours(12);
@ -85,7 +85,7 @@ public class EvccHandler extends BaseThingHandler {
return;
}
String channelIdWithoutGroup = channelUID.getIdWithoutGroup();
int loadpoint = Integer.parseInt(groupId.toString().substring(9));
int loadpoint = Integer.parseInt(groupId.substring(9));
EvccAPI evccAPI = this.evccAPI;
if (evccAPI == null) {
return;
@ -117,7 +117,7 @@ public class EvccHandler extends BaseThingHandler {
try {
evccAPI.setTargetCharge(loadpoint, targetSoC, targetTimeZDT);
} catch (DateTimeParseException e) {
logger.debug("Failed to set target charge", e);
logger.debug("Failed to set target charge: ", e);
}
}
}
@ -186,7 +186,7 @@ public class EvccHandler extends BaseThingHandler {
*/
private void refresh() {
logger.debug("Running refresh job ...");
EvccAPI evccAPI = null;
EvccAPI evccAPI;
evccAPI = this.evccAPI;
if (evccAPI == null) {
return;
@ -194,7 +194,7 @@ public class EvccHandler extends BaseThingHandler {
try {
this.result = evccAPI.getResult();
} catch (EvccApiException e) {
logger.debug("Failed to get state");
logger.debug("Failed to get state: ", e);
}
Result result = this.result;
if (result == null) {
@ -309,28 +309,28 @@ public class EvccHandler extends BaseThingHandler {
ChannelUID channel;
boolean batteryConfigured = this.batteryConfigured;
if (batteryConfigured) {
double batteryPower = result.getBatteryPower();
float batteryPower = result.getBatteryPower();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_BATTERY_POWER);
updateState(channel, new QuantityType<>(batteryPower, Units.WATT));
int batterySoC = result.getBatterySoC();
float batterySoC = result.getBatterySoC();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_BATTERY_SOC);
updateState(channel, new QuantityType<>(batterySoC, Units.PERCENT));
int batteryPrioritySoC = result.getBatterySoC();
float batteryPrioritySoC = result.getBatterySoC();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_BATTERY_PRIORITY_SOC);
updateState(channel, new QuantityType<>(batteryPrioritySoC, Units.PERCENT));
}
boolean gridConfigured = this.gridConfigured;
if (gridConfigured) {
double gridPower = result.getGridPower();
float gridPower = result.getGridPower();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_GRID_POWER);
updateState(channel, new QuantityType<>(gridPower, Units.WATT));
}
double homePower = result.getHomePower();
float homePower = result.getHomePower();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_HOME_POWER);
updateState(channel, new QuantityType<>(homePower, Units.WATT));
boolean pvConfigured = this.pvConfigured;
if (pvConfigured) {
double pvPower = result.getPvPower();
float pvPower = result.getPvPower();
channel = new ChannelUID(getThing().getUID(), "general", CHANNEL_PV_POWER);
updateState(channel, new QuantityType<>(pvPower, Units.WATT));
}
@ -347,22 +347,22 @@ public class EvccHandler extends BaseThingHandler {
int activePhases = loadpoint.getActivePhases();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_ACTIVE_PHASES);
updateState(channel, new DecimalType(activePhases));
double chargeCurrent = loadpoint.getChargeCurrent();
float chargeCurrent = loadpoint.getChargeCurrent();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGE_CURRENT);
updateState(channel, new QuantityType<>(chargeCurrent, Units.AMPERE));
long chargeDuration = loadpoint.getChargeDuration();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGE_DURATION);
updateState(channel, new QuantityType<>(chargeDuration, MetricPrefix.NANO(Units.SECOND)));
double chargePower = loadpoint.getChargePower();
float chargePower = loadpoint.getChargePower();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGE_POWER);
updateState(channel, new QuantityType<>(chargePower, Units.WATT));
long chargeRemainingDuration = loadpoint.getChargeRemainingDuration();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGE_REMAINING_DURATION);
updateState(channel, new QuantityType<>(chargeRemainingDuration, MetricPrefix.NANO(Units.SECOND)));
double chargeRemainingEnergy = loadpoint.getChargeRemainingEnergy();
float chargeRemainingEnergy = loadpoint.getChargeRemainingEnergy();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGE_REMAINING_ENERGY);
updateState(channel, new QuantityType<>(chargeRemainingEnergy, Units.WATT_HOUR));
double chargedEnergy = loadpoint.getChargedEnergy();
float chargedEnergy = loadpoint.getChargedEnergy();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_CHARGED_ENERGY);
updateState(channel, new QuantityType<>(chargedEnergy, Units.WATT_HOUR));
boolean charging = loadpoint.getCharging();
@ -380,13 +380,13 @@ public class EvccHandler extends BaseThingHandler {
boolean hasVehicle = loadpoint.getHasVehicle();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_HAS_VEHICLE);
updateState(channel, OnOffType.from(hasVehicle));
double maxCurrent = loadpoint.getMaxCurrent();
float maxCurrent = loadpoint.getMaxCurrent();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_MAX_CURRENT);
updateState(channel, new QuantityType<>(maxCurrent, Units.AMPERE));
double minCurrent = loadpoint.getMinCurrent();
float minCurrent = loadpoint.getMinCurrent();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_MIN_CURRENT);
updateState(channel, new QuantityType<>(minCurrent, Units.AMPERE));
int minSoC = loadpoint.getMinSoC();
float minSoC = loadpoint.getMinSoC();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_MIN_SOC);
updateState(channel, new QuantityType<>(minSoC, Units.PERCENT));
String mode = loadpoint.getMode();
@ -414,19 +414,19 @@ public class EvccHandler extends BaseThingHandler {
String title = loadpoint.getTitle();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_TITLE);
updateState(channel, new StringType(title));
double vehicleCapacity = loadpoint.getVehicleCapacity();
float vehicleCapacity = loadpoint.getVehicleCapacity();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_VEHICLE_CAPACITY);
updateState(channel, new QuantityType<>(vehicleCapacity, Units.WATT_HOUR));
double vehicleOdometer = loadpoint.getVehicleOdometer();
float vehicleOdometer = loadpoint.getVehicleOdometer();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_VEHICLE_ODOMETER);
updateState(channel, new QuantityType<>(vehicleOdometer, MetricPrefix.KILO(SIUnits.METRE)));
boolean vehiclePresent = loadpoint.getVehiclePresent();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_VEHICLE_PRESENT);
updateState(channel, OnOffType.from(vehiclePresent));
long vehicleRange = loadpoint.getVehicleRange();
float vehicleRange = loadpoint.getVehicleRange();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_VEHICLE_RANGE);
updateState(channel, new QuantityType<>(vehicleRange, MetricPrefix.KILO(SIUnits.METRE)));
int vehicleSoC = loadpoint.getVehicleSoC();
float vehicleSoC = loadpoint.getVehicleSoC();
channel = new ChannelUID(getThing().getUID(), loadpointName, CHANNEL_LOADPOINT_VEHICLE_SOC);
updateState(channel, new QuantityType<>(vehicleSoC, Units.PERCENT));
String vehicleTitle = loadpoint.getVehicleTitle();

View File

@ -38,7 +38,7 @@ import com.google.gson.JsonSyntaxException;
public class EvccAPI {
private final Logger logger = LoggerFactory.getLogger(EvccAPI.class);
private final Gson gson = new Gson();
private String host = "";
private String host;
public EvccAPI(String host) {
this.host = host;
@ -48,9 +48,9 @@ public class EvccAPI {
* Make a HTTP request.
*
* @param url full request URL
* @param method reguest method, e.g. GET, POST
* @param method request method, e.g. GET, POST
* @return the response body
* @throws {@link EvccApiException} if HTTP request failed
* @throws EvccApiException if HTTP request failed
*/
private String httpRequest(String url, String method) throws EvccApiException {
try {
@ -68,9 +68,8 @@ public class EvccAPI {
/**
* Get the status from evcc.
*
* @param host hostname of IP address of the evcc instance
* @return {@link Result} result object from API
* @throws {@link EvccApiException} if status request failed
* @throws EvccApiException if status request failed
*/
public Result getResult() throws EvccApiException {
final String response = httpRequest(this.host + EVCC_REST_API + "state", "GET");
@ -110,7 +109,7 @@ public class EvccAPI {
return httpRequest(this.host + EVCC_REST_API + "loadpoints/" + loadpoint + "/maxcurrent/" + maxCurrent, "POST");
}
public String setTargetCharge(int loadpoint, int targetSoC, ZonedDateTime targetTime) throws EvccApiException {
public String setTargetCharge(int loadpoint, float targetSoC, ZonedDateTime targetTime) throws EvccApiException {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return httpRequest(this.host + EVCC_REST_API + "loadpoints/" + loadpoint + "/targetcharge/" + targetSoC + "/"
+ targetTime.toLocalDateTime().format(formatter), "POST");

View File

@ -16,7 +16,7 @@ import com.google.gson.annotations.SerializedName;
/**
* This class represents a loadpoint object of the status response (/api/state).
* This DTO was written for evcc version 0.91.
* This DTO was written for evcc version 0.106.3
*
* @author Florian Hotze - Initial contribution
*/
@ -28,22 +28,22 @@ public class Loadpoint {
private int activePhases;
@SerializedName("chargeCurrent")
private double chargeCurrent;
private float chargeCurrent;
@SerializedName("chargeDuration")
private long chargeDuration;
@SerializedName("chargePower")
private double chargePower;
private float chargePower;
@SerializedName("chargeRemainingDuration")
private long chargeRemainingDuration;
@SerializedName("chargeRemainingEnergy")
private double chargeRemainingEnergy;
private float chargeRemainingEnergy;
@SerializedName("chargedEnergy")
private double chargedEnergy;
private float chargedEnergy;
@SerializedName("charging")
private boolean charging;
@ -64,13 +64,13 @@ public class Loadpoint {
private int loadpoint;
@SerializedName("maxCurrent")
private double maxCurrent;
private float maxCurrent;
@SerializedName("minCurrent")
private double minCurrent;
private float minCurrent;
@SerializedName("minSoC")
private int minSoC;
private float minSoC;
@SerializedName("mode")
private String mode;
@ -85,7 +85,7 @@ public class Loadpoint {
private long pvRemaining;
@SerializedName("targetSoC")
private int targetSoC;
private float targetSoC;
@SerializedName("targetTime")
private String targetTime;
@ -94,19 +94,19 @@ public class Loadpoint {
private String title;
@SerializedName("vehicleCapacity")
private long vehicleCapacity;
private float vehicleCapacity;
@SerializedName("vehicleOdometer")
private double vehicleOdometer;
private float vehicleOdometer;
@SerializedName("vehiclePresent")
private boolean vehiclePresent;
@SerializedName("vehicleRange")
private long vehicleRange;
private float vehicleRange;
@SerializedName("vehicleSoC")
private int vehicleSoC;
private float vehicleSoC;
@SerializedName("vehicleTitle")
private String vehicleTitle;
@ -121,7 +121,7 @@ public class Loadpoint {
/**
* @return charge current
*/
public double getChargeCurrent() {
public float getChargeCurrent() {
return chargeCurrent;
}
@ -135,7 +135,7 @@ public class Loadpoint {
/**
* @return charge power
*/
public double getChargePower() {
public float getChargePower() {
return chargePower;
}
@ -149,14 +149,14 @@ public class Loadpoint {
/**
* @return charge remaining energy until the target SoC is reached
*/
public double getChargeRemainingEnergy() {
public float getChargeRemainingEnergy() {
return chargeRemainingEnergy;
}
/**
* @return charged energy
*/
public double getChargedEnergy() {
public float getChargedEnergy() {
return chargedEnergy;
}
@ -205,21 +205,21 @@ public class Loadpoint {
/**
* @return maximum current
*/
public double getMaxCurrent() {
public float getMaxCurrent() {
return maxCurrent;
}
/**
* @return minimum current
*/
public double getMinCurrent() {
public float getMinCurrent() {
return minCurrent;
}
/**
* @return minimum state of charge
*/
public int getMinSoC() {
public float getMinSoC() {
return minSoC;
}
@ -254,7 +254,7 @@ public class Loadpoint {
/**
* @return target state of charge (SoC)
*/
public int getTargetSoC() {
public float getTargetSoC() {
return targetSoC;
}
@ -275,14 +275,14 @@ public class Loadpoint {
/**
* @return vehicle's capacity
*/
public double getVehicleCapacity() {
public float getVehicleCapacity() {
return vehicleCapacity;
}
/**
* @return vehicle's odometer
*/
public double getVehicleOdometer() {
public float getVehicleOdometer() {
return vehicleOdometer;
}
@ -296,14 +296,14 @@ public class Loadpoint {
/**
* @return vehicle's range
*/
public long getVehicleRange() {
public float getVehicleRange() {
return vehicleRange;
}
/**
* @return vehicle's state of charge (SoC)
*/
public int getVehicleSoC() {
public float getVehicleSoC() {
return vehicleSoC;
}

View File

@ -16,7 +16,7 @@ import com.google.gson.annotations.SerializedName;
/**
* This class represents the result object of the status response (/api/state).
* This DTO was written for evcc version 0.91.
* This DTO was written for evcc version 0.106.3
*
* @author Florian Hotze - Initial contribution
*/
@ -24,39 +24,37 @@ public class Result {
// Data types from https://github.com/evcc-io/evcc/blob/master/api/api.go
// and from https://docs.evcc.io/docs/reference/configuration/messaging/#msg
// TO DO LATER
// @SerializedName("auth")
// private Auth auth;
// "auth" is left out because it does not provide any useful information
@SerializedName("batteryConfigured")
private boolean batteryConfigured;
@SerializedName("batteryPower")
private double batteryPower;
private float batteryPower;
@SerializedName("batterySoC")
private int batterySoC;
private float batterySoC;
@SerializedName("gridConfigured")
private boolean gridConfigured;
@SerializedName("gridPower")
private double gridPower;
private float gridPower;
@SerializedName("homePower")
private double homePower;
private float homePower;
@SerializedName("loadpoints")
private Loadpoint[] loadpoints;
@SerializedName("prioritySoC")
private double batteryPrioritySoC;
private float batteryPrioritySoC;
@SerializedName("pvConfigured")
private boolean pvConfigured;
@SerializedName("pvPower")
private double pvPower;
private float pvPower;
@SerializedName("siteTitle")
private String siteTitle;
@ -71,21 +69,21 @@ public class Result {
/**
* @return battery's power
*/
public double getBatteryPower() {
public float getBatteryPower() {
return batteryPower;
}
/**
* @return battery's priority state of charge
*/
public double getBatteryPrioritySoC() {
public float getBatteryPrioritySoC() {
return batteryPrioritySoC;
}
/**
* @return battery's state of charge
*/
public int getBatterySoC() {
public float getBatterySoC() {
return batterySoC;
}
@ -99,14 +97,14 @@ public class Result {
/**
* @return grid's power
*/
public double getGridPower() {
public float getGridPower() {
return gridPower;
}
/**
* @return home's power
*/
public double getHomePower() {
public float getHomePower() {
return homePower;
}
@ -127,7 +125,7 @@ public class Result {
/**
* @return pv's power
*/
public double getPvPower() {
public float getPvPower() {
return pvPower;
}

View File

@ -16,7 +16,7 @@ import com.google.gson.annotations.SerializedName;
/**
* This class represents the status response (/api/state).
* This DTO was written for evcc version 0.91.
* This DTO was written for evcc version 0.106.3
*
* @author Florian Hotze - Initial contribution
*/