]> git.basschouten.com Git - openhab-addons.git/commitdiff
[yioremote] Make the Binding compatible to new firmware version of YIO Dock (#9094)
authormiloit <MichaelLoercher@web.de>
Thu, 26 Nov 2020 22:47:07 +0000 (23:47 +0100)
committerGitHub <noreply@github.com>
Thu, 26 Nov 2020 22:47:07 +0000 (14:47 -0800)
* Make the Binding compatible to new firmware version of YIO Dock

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteBindingConstants.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/AuthenticationMessage.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/IRCodeSendMessage.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/IRReceiverMessage.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/PingMessage.java [new file with mode: 0644]

index 66ef1cc7e0a033e4873d4fece42faf7ef6424885..1335a761e3a4b1e3f911f74a0957323fc2bd9cb4 100644 (file)
@@ -32,6 +32,8 @@ public class YIOremoteBindingConstants {
         AUTHENTICATION_PROCESS,
         AUTHENTICATION_FAILED,
         AUTHENTICATION_COMPLETE,
+        SEND_PING,
+        CHECK_PONG,
         CONNECTION_FAILED,
         CONNECTION_ESTABLISHED,
         COMMUNICATION_ERROR,
index c90a2cbaae251fd439d4ff4e84ed2fd319faeb90..7d26f1d7d9e0183d0e9577652dedc7617cc4a938 100644 (file)
@@ -31,6 +31,7 @@ import org.openhab.binding.yioremote.internal.dto.AuthenticationMessage;
 import org.openhab.binding.yioremote.internal.dto.IRCode;
 import org.openhab.binding.yioremote.internal.dto.IRCodeSendMessage;
 import org.openhab.binding.yioremote.internal.dto.IRReceiverMessage;
+import org.openhab.binding.yioremote.internal.dto.PingMessage;
 import org.openhab.binding.yioremote.internal.utils.Websocket;
 import org.openhab.binding.yioremote.internal.utils.WebsocketInterface;
 import org.openhab.core.library.types.OnOffType;
@@ -80,6 +81,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
     private IRCodeSendMessage irCodeSendMessageHandler = new IRCodeSendMessage(irCodeSendHandler);
     private AuthenticationMessage authenticationMessageHandler = new AuthenticationMessage();
     private IRReceiverMessage irReceiverMessageHandler = new IRReceiverMessage();
+    private PingMessage pingMessageHandler = new PingMessage();
 
     public YIOremoteDockHandler(Thing thing) {
         super(thing);
@@ -165,27 +167,23 @@ public class YIOremoteDockHandler extends BaseThingHandler {
 
         if (message.has("type")) {
             if (message.get("type").toString().equalsIgnoreCase("\"auth_required\"")) {
-                heartBeat = true;
                 success = true;
                 receivedStatus = "Authentication required";
             } else if (message.get("type").toString().equalsIgnoreCase("\"auth_ok\"")) {
                 authenticationOk = true;
-                heartBeat = true;
                 success = true;
                 receivedStatus = "Authentication ok";
             } else if (message.get("type").toString().equalsIgnoreCase("\"dock\"") && message.has("message")) {
-                if (message.get("message").toString().equalsIgnoreCase("\"ir_send\"")) {
+                if (message.get("message").toString().equalsIgnoreCase("\"pong\"")) {
+                    heartBeat = true;
+                    success = true;
+                    receivedStatus = "Heart beat received";
+                } else if (message.get("message").toString().equalsIgnoreCase("\"ir_send\"")) {
                     if (message.get("success").toString().equalsIgnoreCase("true")) {
                         receivedStatus = "Send IR Code successfully";
-                        heartBeat = true;
                         success = true;
                     } else {
-                        if (irCodeSendHandler.getCode().equalsIgnoreCase("0;0x0;0;0")) {
-                            logger.debug("Send heartBeat Code success");
-                            receivedStatus = "Send heartBeat Code success";
-                        } else {
-                            receivedStatus = "Send IR Code failure";
-                        }
+                        receivedStatus = "Send IR Code failure";
                         heartBeat = true;
                         success = true;
                     }
@@ -253,6 +251,8 @@ public class YIOremoteDockHandler extends BaseThingHandler {
         if (webSocketReconnectionPollingJob != null) {
             if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) {
                 webSocketReconnectionPollingJob.cancel(true);
+                authenticationOk = false;
+                heartBeat = false;
             }
             webSocketReconnectionPollingJob = null;
         }
@@ -309,7 +309,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
                 if (authenticationOk) {
                     yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_COMPLETE;
                     updateStatus(ThingStatus.ONLINE);
-                    webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 150,
+                    webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 60,
                             TimeUnit.SECONDS);
                 } else {
                     yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_FAILED;
@@ -337,11 +337,20 @@ public class YIOremoteDockHandler extends BaseThingHandler {
     private void pollingWebsocketJob() {
         switch (yioRemoteDockActualStatus) {
             case AUTHENTICATION_COMPLETE:
-                if (getAndResetHeartbeat()) {
-                    updateChannelString(GROUP_OUTPUT, STATUS_STRING_CHANNEL,
-                            irCodeReceivedHandler.getCode() + irCodeReceivedHandler.getFormat());
+                resetHeartbeat();
+                sendMessage(YioRemoteMessages.HEARTBEAT_MESSAGE, "");
+                yioRemoteDockActualStatus = YioRemoteDockHandleStatus.CHECK_PONG;
+                break;
+            case SEND_PING:
+                resetHeartbeat();
+                sendMessage(YioRemoteMessages.HEARTBEAT_MESSAGE, "");
+                yioRemoteDockActualStatus = YioRemoteDockHandleStatus.CHECK_PONG;
+                break;
+            case CHECK_PONG:
+                if (getHeartbeat()) {
+                    updateChannelString(GROUP_OUTPUT, STATUS_STRING_CHANNEL, receivedStatus);
+                    yioRemoteDockActualStatus = YioRemoteDockHandleStatus.SEND_PING;
                     logger.debug("heartBeat ok");
-                    sendMessage(YioRemoteMessages.HEARTBEAT_MESSAGE, "");
                 } else {
                     yioRemoteDockActualStatus = YioRemoteDockHandleStatus.COMMUNICATION_ERROR;
                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
@@ -360,10 +369,13 @@ public class YIOremoteDockHandler extends BaseThingHandler {
         }
     }
 
-    public boolean getAndResetHeartbeat() {
-        boolean result = heartBeat;
+    public boolean resetHeartbeat() {
         heartBeat = false;
-        return result;
+        return true;
+    }
+
+    public boolean getHeartbeat() {
+        return heartBeat;
     }
 
     public void reconnectWebsocket() {
@@ -423,9 +435,8 @@ public class YIOremoteDockHandler extends BaseThingHandler {
                         authenticationMessageHandler.getAuthenticationMessageString());
                 break;
             case HEARTBEAT_MESSAGE:
-                irCodeSendHandler.setCode("0;0x0;0;0");
-                yioremoteDockwebSocketClient.sendMessage(irCodeSendMessageHandler.getIRcodeSendMessageString());
-                logger.debug("sending heartBeat message: {}", irCodeSendMessageHandler.getIRcodeSendMessageString());
+                yioremoteDockwebSocketClient.sendMessage(pingMessageHandler.getPingMessageString());
+                logger.debug("sending ping {}", pingMessageHandler.getPingMessageString());
                 break;
             case IR_RECEIVER_ON:
                 irReceiverMessageHandler.setOn();
index a056fafe43a6b4eb50474f230a3a41bc01a7a577..5313bb9487277481c4bf5db91d80cd0687eea64a 100644 (file)
@@ -47,9 +47,6 @@ public class AuthenticationMessage {
     }
 
     public String getAuthenticationMessageString() {
-        JsonObject authenticationMessage = new JsonObject();
-        authenticationMessage.addProperty("type", type);
-        authenticationMessage.addProperty("token", token);
-        return authenticationMessage.toString();
+        return getAuthenticationMessageJsonObject().toString();
     }
 }
index 12eaab4b68e61a5c1e73f482b37e4dc10503c935..984a0ffb2e5fc6d44f2a2e8c9aebcbf44aefba3c 100644 (file)
@@ -51,11 +51,6 @@ public class IRCodeSendMessage {
     }
 
     public String getIRcodeSendMessageString() {
-        JsonObject irCodeSendMessage = new JsonObject();
-        irCodeSendMessage.addProperty("type", type);
-        irCodeSendMessage.addProperty("command", command);
-        irCodeSendMessage.addProperty("code", ircode.getCode());
-        irCodeSendMessage.addProperty("format", ircode.getFormat());
-        return irCodeSendMessage.toString();
+        return getIRcodeSendMessageJsonObject().toString();
     }
 }
index 434d354789d9f3acdf6ac5f5c05a53e419a40b8b..ae9b1e2e5fdc1b27e4fc0a6e000897ec01b17006 100644 (file)
@@ -43,9 +43,6 @@ public class IRReceiverMessage {
     }
 
     public String getIRreceiverMessageString() {
-        JsonObject irReceiverMessage = new JsonObject();
-        irReceiverMessage.addProperty("type", type);
-        irReceiverMessage.addProperty("command", command);
-        return irReceiverMessage.toString();
+        return getIRreceiverMessageJsonObject().toString();
     }
 }
diff --git a/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/PingMessage.java b/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/dto/PingMessage.java
new file mode 100644 (file)
index 0000000..8b8c61e
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2010-2020 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.yioremote.internal.dto;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+import com.google.gson.JsonObject;
+
+/**
+ * The {@link PingMessage} the AuthenticationMessage DTO
+ *
+ *
+ * @author Michael Loercher - Initial contribution
+ */
+@NonNullByDefault
+public class PingMessage {
+    private String type = "dock";
+    private String command = "ping";
+
+    public String getType() {
+        return type;
+    }
+
+    public String getcommand() {
+        return command;
+    }
+
+    public void setToken(String command) {
+        this.command = command;
+    }
+
+    public JsonObject getPingMessageJsonObject() {
+        JsonObject pingMessage = new JsonObject();
+        pingMessage.addProperty("type", type);
+        pingMessage.addProperty("command", command);
+        return pingMessage;
+    }
+
+    public String getPingMessageString() {
+        return getPingMessageJsonObject().toString();
+    }
+}