]> git.basschouten.com Git - openhab-addons.git/commitdiff
[evcc] Fix data type issues in DTO (#13710)
authorFlorian Hotze <florianh_dev@icloud.com>
Sun, 13 Nov 2022 11:54:00 +0000 (12:54 +0100)
committerGitHub <noreply@github.com>
Sun, 13 Nov 2022 11:54:00 +0000 (12:54 +0100)
* [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>
bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandler.java
bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/EvccAPI.java
bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/dto/Loadpoint.java
bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/dto/Result.java
bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/dto/Status.java

index f1c394886173a2ab4a5fb5d0664f5fe466e00e2a..438e8181a89d1c60a7a41425cc4457cf3673202a 100644 (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();
index 9cc4733408a79c424c13ce2e992199c6b559b064..17c97d42a06cf652c8ae39b9ab14c3abdb22a773 100644 (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 {
@@ -67,10 +67,9 @@ public class EvccAPI {
     // API calls to evcc
     /**
      * 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");
index 732af3a1a37b632366e6d05da9d3747bf11f0e84..cc98dee4119b6ff07ccd7a69b0b670026a885481 100644 (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;
     }
 
index 6a613e8b235baaef5a0696bc34e88af594a909fa..e1ab2aa6aab4d91099659483e94a08319292e261 100644 (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;
     }
 
index b70e1f9e83fdc29ff5e2b4fad5bd9995dd1d9f3d..ae37608b316a5896e25702e7f39eaab0cd07d694 100644 (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
  */