RealZimboGuy / ewelink-api-java

Java Api for the Sonoff ewelink
http://ewelink-api-java.co.zw/
15 stars 5 forks source link

User is not able to set the Device status, after a successfully login #7

Closed oncioiu closed 9 months ago

oncioiu commented 11 months ago

Description After a successfully login, I was not able to perform device status change.

Step to reproduce:

  1. Provide Valid Credential.
  2. Make sure that lib is successfully login
  3. Call method setDeviceStatus

Tried to debug, seems that code is break bellow, but not sure why return eweLinkWebSocketClient.sendAndWait(gson.toJson(statusChange),statusChange.getSequence());

Logs "org.java_websocket.exceptions.WebsocketNotConnectedException at app//org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:628) at app//org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:605) at app//org.java_websocket.client.WebSocketClient.send(WebSocketClient.java:410) at app//com.github.realzimboguy.ewelink.api.EweLinkWebSocketClient.sendAndWait(EweLinkWebSocketClient.java:83) at app//com.github.realzimboguy.ewelink.api.EweLink.setDeviceStatus(EweLink.java:328)"

Attached some pics with debug process image image

erugeri commented 10 months ago

same here, any idea ?

RealZimboGuy commented 10 months ago

HI, seemed to miss this issue being logged, i can reproduce it by

        System.out.println(eweLink.setDeviceStatus("1000111111", "off"));

however its fixed if i do this.

        Thread.sleep(5000);
        System.out.println(eweLink.setDeviceStatus("1000111111", "off"));

i feel you are trying to set the status of a device too soon after the setup of the websocket. the other thing, make sure you are using a shared instance of the EweLink in your code, dont spin one up each time you want to do something

erugeri commented 10 months ago

Thanks a lot for your reply ! And thanks a lot for your work!! In the end I wrote my own code not using web sockets which I find simpler for my specific use case :

URL url = new URL("https://eu-api.coolkit.cc:8080/api/user/device/status");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);

body should be :

{
  "deviceid": <deviceId>,
  "params": {
    "switch": "<on/off>"
  },
  "appid": <same APP_ID as login>,
  "type": 1,
  "nonce": <same nonce code as login - getNonce()>,
  "ts": <(System.currentTimeMillis() / 1000)>,
  "version": 8
}
RealZimboGuy commented 10 months ago

verry interesting so i had to rewrite a large portion of the code because "coolkit" deprecated V1 of their api

https://github.com/RealZimboGuy/ewelink-api-java/issues/5

image

because of that i had to reverse out V2 and that didnt have post support under baseUrl = "https://"+region+"-apia.coolkit.cc/v2/";

i see they must have had too much backlash and turned it back on

RealZimboGuy commented 10 months ago

if it works for now, then in theory V2.1.3-RELEASE should still work which uses the post logic

erugeri commented 10 months ago

Great! In case it is removed again, I'll add the sleep to the websocket version :) thanks!