]> git.basschouten.com Git - openhab-addons.git/commitdiff
[easee] simplified determination of start/stop status due to recent API changes ...
authorAlexander Friese <alexf2015@users.noreply.github.com>
Sat, 9 Sep 2023 12:09:29 +0000 (14:09 +0200)
committerGitHub <noreply@github.com>
Sat, 9 Sep 2023 12:09:29 +0000 (14:09 +0200)
* simplified determination of start/stop as the opMode now has new values (7+8) to show missing authentication (before this was included in value "2")
* refactored ChargerOpState to enum

---------

Signed-off-by: Alexander Friese <af944580@googlemail.com>
Signed-off-by: Alexander Friese <alexf2015@users.noreply.github.com>
Co-authored-by: lsiepel <leosiepel@gmail.com>
bundles/org.openhab.binding.easee/src/main/java/org/openhab/binding/easee/internal/EaseeBindingConstants.java
bundles/org.openhab.binding.easee/src/main/java/org/openhab/binding/easee/internal/model/ChargerOpState.java [new file with mode: 0644]
bundles/org.openhab.binding.easee/src/main/java/org/openhab/binding/easee/internal/model/CustomResponseTransformer.java

index 239ea2d06f30d6972d60e39a644a03b60ed930ec..be23deb91f3bc2ee0dc9896862268781689fd321 100644 (file)
@@ -156,9 +156,6 @@ public class EaseeBindingConstants {
 
     public static final String GENERIC_YES = "Yes";
     public static final String GENERIC_NO = "No";
-    public static final int CHARGER_OP_STATE_WAITING = 2;
-    public static final int CHARGER_OP_STATE_CHARGING = 3;
-    public static final int CHARGER_OP_STATE_NOT_AUTHENTICATED = 7;
     public static final double CHARGER_DYNAMIC_CURRENT_PAUSE = 0;
     public static final int CHARGER_REASON_FOR_NO_CURRENT_CIRCUIT_LIMIT = 2;
     public static final int CHARGER_REASON_FOR_NO_CURRENT_CHARGER_LIMIT = 52;
diff --git a/bundles/org.openhab.binding.easee/src/main/java/org/openhab/binding/easee/internal/model/ChargerOpState.java b/bundles/org.openhab.binding.easee/src/main/java/org/openhab/binding/easee/internal/model/ChargerOpState.java
new file mode 100644 (file)
index 0000000..5141967
--- /dev/null
@@ -0,0 +1,75 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.easee.internal.model;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * this enum represents the charger operation states as documented by https://developer.easee.cloud/docs/enumerations
+ *
+ * @author Alexander Friese - initial contribution
+ */
+@NonNullByDefault
+public enum ChargerOpState {
+    OFFLINE(0),
+    DISCONNECTED(1),
+    WAITING(2),
+    CHARGING(3),
+    COMPLETED(4),
+    ERROR(5),
+    READY_TO_CHARGE(6),
+    NOT_AUTHENTICATED(7),
+    DEAUTHENTICATING(8),
+    UNKNOWN_STATE(-1);
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ChargerOpState.class);
+    private final int code;
+
+    private ChargerOpState(int code) {
+        this.code = code;
+    }
+
+    public boolean isAuthenticatedState() {
+        switch (this) {
+            case WAITING:
+            case CHARGING:
+            case COMPLETED:
+            case READY_TO_CHARGE:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static ChargerOpState fromCode(String code) {
+        try {
+            return ChargerOpState.fromCode(Integer.parseInt(code));
+        } catch (NumberFormatException ex) {
+            LOGGER.warn("caught exception while parsing ChargerOpState code: '{}' - exception: {}", code,
+                    ex.getMessage());
+            return UNKNOWN_STATE;
+        }
+    }
+
+    public static ChargerOpState fromCode(int code) {
+        for (ChargerOpState state : ChargerOpState.values()) {
+            if (state.code == code) {
+                return state;
+            }
+        }
+        LOGGER.info("unknown ChargerOpState code: '{}'", code);
+        return UNKNOWN_STATE;
+    }
+}
index de6ab958ab7db9a3ac140c6adc48736522799995..172cc90b9c1ff9cfb8920b1bc990614a87d00404 100644 (file)
@@ -53,7 +53,7 @@ class CustomResponseTransformer {
 
         switch (triggerChannel.getUID().getId()) {
             case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_OP_MODE:
-                updateChargerStartStop(result, value, rawData);
+                updateChargerStartStop(result, value);
                 break;
             case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_DYNAMIC_CURRENT:
                 updateChargerPauseResume(result, value);
@@ -78,28 +78,11 @@ class CustomResponseTransformer {
         return result;
     }
 
-    private void updateChargerStartStop(Map<Channel, State> result, String value, JsonObject rawData) {
+    private void updateChargerStartStop(Map<Channel, State> result, String value) {
         Channel channel = channelProvider.getChannel(CHANNEL_GROUP_CHARGER_COMMANDS, CHANNEL_CHARGER_START_STOP);
         if (channel != null) {
-            int val = Integer.parseInt(value);
-            // state >= 3 && state < 7 will mean charging, ready to charge or charging finished
-            boolean charging = val >= CHARGER_OP_STATE_CHARGING && val < CHARGER_OP_STATE_NOT_AUTHENTICATED;
-
-            String rfnc = Utils.getAsString(rawData, CHANNEL_CHARGER_REASON_FOR_NO_CURRENT);
-            int reasonForNoCurrent = Integer.valueOf(rfnc == null ? "-1" : rfnc);
-            boolean paused = false;
-            if (val == CHARGER_OP_STATE_WAITING) {
-                switch (reasonForNoCurrent) {
-                    case CHARGER_REASON_FOR_NO_CURRENT_CHARGER_LIMIT:
-                    case CHARGER_REASON_FOR_NO_CURRENT_CIRCUIT_LIMIT:
-                        paused = true;
-                        break;
-                    default:
-                        paused = false;
-                        break;
-                }
-            }
-            result.put(channel, OnOffType.from(charging || paused));
+            ChargerOpState state = ChargerOpState.fromCode(value);
+            result.put(channel, OnOffType.from(state.isAuthenticatedState()));
         }
     }