From fa43fe9290b365d7fd3c2f3cf9b7fad2b76edb70 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 14 Oct 2024 11:16:28 -0500 Subject: [PATCH] feat: add ability to set temperature setpoint on device --- .../com/bigboxer23/eco_net/EcoNetAPI.java | 33 +++++++++++++++++++ .../com/bigboxer23/eco_net/EcoNetApiTest.java | 32 +++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bigboxer23/eco_net/EcoNetAPI.java b/src/main/java/com/bigboxer23/eco_net/EcoNetAPI.java index 13f6d18..2b91a32 100644 --- a/src/main/java/com/bigboxer23/eco_net/EcoNetAPI.java +++ b/src/main/java/com/bigboxer23/eco_net/EcoNetAPI.java @@ -199,4 +199,37 @@ public void subscribeToEvents(IEventSubscriber subscriber) { initMQTTConnection(); // Init if necessary subscribers.add(subscriber); } + + public void setMode(String deviceId, String serialNumber, int mode) { + // TODO: + + } + + /** + * Set the heater's temperature setpoint + * + * @param deviceId device id (name) + * @param serialNumber serial number of device + * @param setpoint setpoint to heat to + */ + public void setTemperatureSetPoint(String deviceId, String serialNumber, int setpoint) { + sendCommand(deviceId, serialNumber, "@SETPOINT", setpoint); + } + + private void sendCommand(String deviceId, String serialNumber, String command, int value) { + initMQTTConnection(); + try { + MqttMessage message = new MqttMessage(); + message.setPayload(new JsonMapBuilder() + .put("transactionId", "ANDROID_" + System.currentTimeMillis()) + .put("device_name", deviceId) + .put("serial_number", serialNumber) + .put(command, value) + .toJson() + .getBytes()); + mqttClient.publish("user/" + accountId + "/device/desired", message); + } catch (MqttException e) { + logger.error("sendCommand " + command, e); + } + } } diff --git a/src/test/java/com/bigboxer23/eco_net/EcoNetApiTest.java b/src/test/java/com/bigboxer23/eco_net/EcoNetApiTest.java index 84ed0c0..bbc2a99 100644 --- a/src/test/java/com/bigboxer23/eco_net/EcoNetApiTest.java +++ b/src/test/java/com/bigboxer23/eco_net/EcoNetApiTest.java @@ -85,9 +85,39 @@ public void fetchEnergyUsage() { api.fetchUserData().ifPresent(userData -> { Location location = userData.getResults().getLocations().get(0); Equipment equipment = location.getEquipments().get(0); - Optional data = api.fetchEnergyUsage(equipment.getDeviceName(), equipment.getSerialNumber(), 8, 10, 2024); + Optional data = + api.fetchEnergyUsage(equipment.getDeviceName(), equipment.getSerialNumber(), 8, 10, 2024); assertTrue(data.isPresent()); assertNotNull(data.get().getResults()); }); } + + @Test + public void setTemperatureSetPoint() { + EcoNetAPI api = EcoNetAPI.getInstance(email, password); + api.fetchUserData().ifPresent(userData -> { + Location location = userData.getResults().getLocations().get(0); + Equipment equipment = location.getEquipments().get(0); + int currentSetpoint = equipment.getSetpoint().getValue(); + assertNotEquals(132, currentSetpoint); + api.setTemperatureSetPoint(equipment.getDeviceName(), equipment.getSerialNumber(), 132); + try { + Thread.sleep(5000); + } catch (InterruptedException theE) { + } + api.fetchUserData().ifPresent(updatedUserData -> { + assertEquals( + 132, + updatedUserData + .getResults() + .getLocations() + .get(0) + .getEquipments() + .get(0) + .getSetpoint() + .getValue()); + }); + api.setTemperatureSetPoint(equipment.getDeviceName(), equipment.getSerialNumber(), currentSetpoint); + }); + } }