Skip to content

Commit

Permalink
[lgwebos] Avoid thing updates when the thing handler is already dispo…
Browse files Browse the repository at this point in the history
…sed (openhab#7299)

* [lgwebos] Avoid thing updates when the thing handler is already disposed
* Change of state trigger for stopping the reconection job and starting the keep alive job

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and J-N-K committed Jul 14, 2020
1 parent d7aeaf3 commit 57824c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void dispose() {
LGWebOSTVSocket s = socket;
if (s != null) {
s.setListener(null);
scheduler.execute(() -> s.disconnect()); // dispose should be none-blocking
s.disconnect();
}
socket = null;
config = null; // ensure config gets actually refreshed during re-initialization
Expand Down Expand Up @@ -275,14 +275,15 @@ public void onStateChanged(LGWebOSTVSocket.State state) {
stopKeepAliveJob();
startReconnectJob();
break;

case REGISTERING:
case CONNECTING:
stopReconnectJob();
startKeepAliveJob();
break;
case REGISTERING:
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE,
"Registering - You may need to confirm pairing on TV.");
break;
case REGISTERED:
startKeepAliveJob();
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Connected");

channelHandlers.forEach((k, v) -> {
Expand All @@ -307,6 +308,7 @@ public void onError(String error) {
switch (getSocket().getState()) {
case DISCONNECTED:
break;
case CONNECTING:
case REGISTERING:
case REGISTERED:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Connection Failed: " + error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class LGWebOSTVSocket {

public enum State {
DISCONNECTED,
CONNECTING,
REGISTERING,
REGISTERED
}
Expand Down Expand Up @@ -209,6 +210,8 @@ public void onClose(int statusCode, String reason) {
* WebOS WebSocket API specific Communication
*/
void sendHello() {
setState(State.CONNECTING);

JsonObject packet = new JsonObject();
packet.addProperty("id", nextRequestId());
packet.addProperty("type", "hello");
Expand Down Expand Up @@ -293,9 +296,11 @@ public void sendCommand(ServiceCommand<?> command) {
this.sendMessage(packet);

break;
case CONNECTING:
case REGISTERING:
case DISCONNECTED:
logger.warn("Skipping command {} for {} in state {}", command, command.getTarget(), state);
logger.warn("Skipping {} command {} for {} in state {}", command.getType(), command,
command.getTarget(), state);
break;
}

Expand Down Expand Up @@ -393,6 +398,10 @@ public void onMessage(String message) {
}
break;
case "hello":
if (state != State.CONNECTING) {
logger.debug("Skipping response {}, not in CONNECTING state, state was {}", message, state);
break;
}
if (response.getPayload() == null) {
logger.warn("No payload in error message: {}", message);
break;
Expand All @@ -408,6 +417,10 @@ public void onMessage(String message) {
sendRegister();
break;
case "registered":
if (state != State.REGISTERING) {
logger.debug("Skipping response {}, not in REGISTERING state, state was {}", message, state);
break;
}
if (response.getPayload() == null) {
logger.warn("No payload in registered message: {}", message);
break;
Expand Down

0 comments on commit 57824c6

Please sign in to comment.