diff --git a/io.openems.edge.io.shelly/src/io/openems/edge/io/shelly/shelly25/IoShelly25.java b/io.openems.edge.io.shelly/src/io/openems/edge/io/shelly/shelly25/IoShelly25.java index b276675dc5e..01149dde732 100644 --- a/io.openems.edge.io.shelly/src/io/openems/edge/io/shelly/shelly25/IoShelly25.java +++ b/io.openems.edge.io.shelly/src/io/openems/edge/io/shelly/shelly25/IoShelly25.java @@ -5,10 +5,8 @@ import io.openems.common.channel.AccessMode; import io.openems.common.channel.Level; import io.openems.common.channel.PersistencePriority; -import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; import io.openems.common.types.OpenemsType; import io.openems.edge.common.channel.BooleanDoc; -import io.openems.edge.common.channel.BooleanWriteChannel; import io.openems.edge.common.channel.Doc; import io.openems.edge.common.channel.StateChannel; import io.openems.edge.common.channel.value.Value; @@ -41,6 +39,29 @@ public static enum ChannelId implements io.openems.edge.common.channel.ChannelId .accessMode(AccessMode.READ_WRITE) // .persistencePriority(PersistencePriority.HIGH) // .onChannelSetNextWriteMirrorToDebugChannel(ChannelId.DEBUG_RELAY_1)), + + /** + * Indicates whether the associated Relay is in Overtemp-State. + * + *
- * See https://shelly-api-docs.shelly.cloud - */ -public class ShellyApi { - - private final String baseUrl; - - public ShellyApi(String ip) { - this.baseUrl = "http://" + ip; - } - - /** - * Gets the status of the device. - * - *
- * See https://shelly-api-docs.shelly.cloud/#shelly2-5-status - * - * @return the status as JsonObject according to Shelly docs - * @throws OpenemsNamedException on error - */ - public JsonObject getStatus() throws OpenemsNamedException { - return JsonUtils.getAsJsonObject(this.sendGetRequest("/status")); - } - - /** - * Gets the "ison" state of the relay with the given index. - * - *
- * See https://shelly-api-docs.shelly.cloud/#shelly2-5-relay-index - * - * @param index the index of the relay - * @return the boolean value - * @throws OpenemsNamedException on error - */ - public boolean getRelayIson(int index) throws OpenemsNamedException { - var json = this.sendGetRequest("/relay/" + index); - return JsonUtils.getAsBoolean(json, "ison"); - } - - /** - * Turns the relay with the given index on or off. - * - * @param index the index of the relay - * @param value true to turn on; false to turn off - * @throws OpenemsNamedException on error - */ - public void setRelayTurn(int index, boolean value) throws OpenemsNamedException { - this.sendGetRequest("/relay/" + index + "?turn=" + (value ? "on" : "off")); - } - - /** - * Sends a get request to the Shelly API. - * - * @param endpoint the REST Api endpoint - * @return a JsonObject or JsonArray - * @throws OpenemsNamedException on error - */ - private JsonElement sendGetRequest(String endpoint) throws OpenemsNamedException { - try { - var url = new URL(this.baseUrl + endpoint); - var con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.setConnectTimeout(5000); - con.setReadTimeout(5000); - var status = con.getResponseCode(); - String body; - try (var in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { - // Read HTTP response - var content = new StringBuilder(); - String line; - while ((line = in.readLine()) != null) { - content.append(line); - content.append(System.lineSeparator()); - } - body = content.toString(); - } - if (status < 300) { - // Parse response to JSON - return JsonUtils.parseToJsonObject(body); - } - throw new OpenemsException("Error while reading from Shelly API. Response code: " + status + ". " + body); - } catch (OpenemsNamedException | IOException e) { - throw new OpenemsException( - "Unable to read from Shelly API. " + e.getClass().getSimpleName() + ": " + e.getMessage()); - } - } - -} diff --git a/io.openems.edge.io.shelly/test/io/openems/edge/io/shelly/shelly25/IoShelly25ImplTest.java b/io.openems.edge.io.shelly/test/io/openems/edge/io/shelly/shelly25/IoShelly25ImplTest.java index 6e778d2393c..2e6e2a98902 100644 --- a/io.openems.edge.io.shelly/test/io/openems/edge/io/shelly/shelly25/IoShelly25ImplTest.java +++ b/io.openems.edge.io.shelly/test/io/openems/edge/io/shelly/shelly25/IoShelly25ImplTest.java @@ -2,6 +2,7 @@ import org.junit.Test; +import io.openems.edge.bridge.http.dummy.DummyBridgeHttpFactory; import io.openems.edge.common.test.ComponentTest; public class IoShelly25ImplTest { @@ -11,6 +12,7 @@ public class IoShelly25ImplTest { @Test public void test() throws Exception { new ComponentTest(new IoShelly25Impl()) // + .addReference("httpBridgeFactory", new DummyBridgeHttpFactory()) // .activate(MyConfig.create() // .setId(COMPONENT_ID) // .setIp("127.0.0.1") // @@ -18,4 +20,4 @@ public void test() throws Exception { ; } -} +} \ No newline at end of file