[yioremote] Make the Binding compatible to new firmware version of YIO Dock (#9094)
* Make the Binding compatible to new firmware version of YIO Dock Signed-off-by: Michael Loercher <MichaelLoercher@web.de>pull/9139/head^2
parent
0a9ea9fd45
commit
3fb0b86989
|
@ -32,6 +32,8 @@ public class YIOremoteBindingConstants {
|
|||
AUTHENTICATION_PROCESS,
|
||||
AUTHENTICATION_FAILED,
|
||||
AUTHENTICATION_COMPLETE,
|
||||
SEND_PING,
|
||||
CHECK_PONG,
|
||||
CONNECTION_FAILED,
|
||||
CONNECTION_ESTABLISHED,
|
||||
COMMUNICATION_ERROR,
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue