-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
609 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/github/realzimboguy/ewelink/api/EweLinkWebSocketClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.realzimboguy.ewelink.api; | ||
|
||
import com.github.realzimboguy.ewelink.api.wss.WssLogin; | ||
import com.github.realzimboguy.ewelink.api.wss.WssResponse; | ||
import com.github.realzimboguy.ewelink.api.wss.wssrsp.WssRspMsg; | ||
import com.google.gson.Gson; | ||
import org.java_websocket.client.WebSocketClient; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URI; | ||
|
||
public class EweLinkWebSocketClient extends WebSocketClient { | ||
|
||
Logger logger = LoggerFactory.getLogger(EweLinkWebSocketClient.class); | ||
|
||
private WssResponse wssResponse; | ||
private String wssLogin; | ||
Gson gson = new Gson(); | ||
|
||
public void setWssResponse(WssResponse wssResponse) { | ||
this.wssResponse = wssResponse; | ||
} | ||
|
||
public void setWssLogin(String wssLogin) { | ||
this.wssLogin = wssLogin; | ||
} | ||
|
||
public EweLinkWebSocketClient(URI serverUri) { | ||
super(serverUri); | ||
} | ||
|
||
@Override | ||
public void onOpen(ServerHandshake serverHandshake) { | ||
send(wssLogin); | ||
} | ||
|
||
@Override | ||
public void onMessage(String s) { | ||
if (s!= null && s.equalsIgnoreCase("pong")){ | ||
//swallow this as its just a ping/pong | ||
logger.debug(s); | ||
}else { | ||
wssResponse.onMessage(s); | ||
wssResponse.onMessageParsed(gson.fromJson(s, WssRspMsg.class)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClose(int i, String s, boolean b) { | ||
logger.warn("WS onCloseCalled, system will self-recover {} {} {}",i,s,b); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
wssResponse.onError(e.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.