项目作者: RealZimboGuy

项目描述 :
Java Api for the Sonoff ewelink
高级语言: Java
项目地址: git://github.com/RealZimboGuy/ewelink-api-java.git
创建时间: 2020-12-03T12:49:26Z
项目社区:https://github.com/RealZimboGuy/ewelink-api-java

开源协议:

下载


ewelink-api-java

eWeLink API written in JAVA

3.3.3-RELEASE BREAKING CHANGES prior V2.X.X nolonger working due to ewelik api changes

Import

  1. <dependency>
  2. <groupId>com.github.realzimboguy.ewelink.api</groupId>
  3. <artifactId>ewelink-api-java</artifactId>
  4. <version>3.3.3-RELEASE</version>
  5. </dependency>

Usage

Please refer to the Below example, note these are the same details you would login to from the mobile app.
if you get an error on ie “eu” region, try “us” region.

Params for the EWeLink Constructor

  • region eg us/eu
  • username
  • password
  • country code NEW
  • interval in Minutes for re-login eg, if the api was last called more than 60 min ago, it will perform a login before executing the command.

You must perform a login before calling any other methods, the methods exposed are:

Homepage is the base endpoint that returns all the information about your account, devices, scenes, timers etc.

  1. public void login() throws Exception
  2. public FamilyPage getFamily() throws Exception
  3. public List<Thing> getThings() throws Exception
  4. //use the getFamily() to find the family IDs
  5. public List<Thing> getThings(String familyId) throws Exception
  6. public boolean setDeviceStatus(String deviceId, String status) throws Exception
  7. public boolean setDeviceStatusByName(String name, String status) throws Exception
  8. //ie. used for TX2CH devices
  9. public boolean setMultiDeviceStatus(String deviceId, List<OutletSwitch> outletSwitches) throws Exception

sample

  1. Gson gson = new Gson();
  2. EweLink eweLink = new EweLink("eu", "test@gmail.com", "test", "+263",60);
  3. try {
  4. eweLink.login();
  5. List<Thing> things = eweLink.getThings();
  6. logger.info("PRINT DEVICE_ID, NAME, ONLINE, SWITCH, VOLTAGE");
  7. for (Thing thing : things) {
  8. logger.info("{}, {}, {}, {}, {}",
  9. thing.getItemData().getDeviceid() ,
  10. thing.getItemData().getName() ,
  11. thing.getItemData().getOnline(),
  12. thing.getItemData().getParams().getSwitch(),
  13. thing.getItemData().getParams().getVoltage());
  14. }
  15. logger.info("PRINT JSON OBJECTS");
  16. for (Thing thing : things) {
  17. logger.info("{} ",gson.toJson(thing));
  18. }
  19. eweLink.getWebSocket(new WssResponse() {
  20. @Override
  21. public void onMessage(String s) {
  22. //if you want the raw json data
  23. System.out.println("on message in test raw:" + s);
  24. }
  25. @Override
  26. public void onMessageParsed(WssRspMsg rsp) {
  27. if (rsp.getError() == null) {
  28. //normal scenario
  29. StringBuilder sb = new StringBuilder();
  30. sb.append("Device:").append(rsp.getDeviceid()).append(" - ");
  31. if (rsp.getParams() != null) {
  32. sb.append("Switch:").append(rsp.getParams().getSwitch()).append(" - ");
  33. sb.append("Voltage:").append(rsp.getParams().getVoltage()).append(" - ");
  34. sb.append("Power:").append(rsp.getParams().getPower()).append(" - ");
  35. sb.append("Current:").append(rsp.getParams().getCurrent()).append(" - ");
  36. }
  37. System.out.println(sb.toString());
  38. } else if (rsp.getError() == 0) {
  39. //this is from a login response
  40. System.out.println("login success");
  41. } else if (rsp.getError() > 0) {
  42. System.out.println("login error:" + rsp.toString());
  43. }
  44. }
  45. @Override
  46. public void onError(String error) {
  47. System.out.println("onError in test, this should never be called");
  48. System.out.println(error);
  49. }
  50. });
  51. Thread.sleep(10000);
  52. System.out.println(eweLink.setDeviceStatus("1000f40d35", "on"));
  53. Thread.sleep(5000);
  54. System.out.println(eweLink.setDeviceStatus("1000f40d35", "off"));
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }

tags:
sonoff eweelink api automation java code library