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;
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);
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;
}
if (webSocketReconnectionPollingJob != null) {
if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) {
webSocketReconnectionPollingJob.cancel(true);
+ authenticationOk = false;
+ heartBeat = false;
}
webSocketReconnectionPollingJob = null;
}
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;
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,
}
}
- public boolean getAndResetHeartbeat() {
- boolean result = heartBeat;
+ public boolean resetHeartbeat() {
heartBeat = false;
- return result;
+ return true;
+ }
+
+ public boolean getHeartbeat() {
+ return heartBeat;
}
public void reconnectWebsocket() {
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();
--- /dev/null
+/**
+ * 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();
+ }
+}