From 7c14d574836ebb772ae1cdaf99e5145f97065eec Mon Sep 17 00:00:00 2001 From: Thomas Vogl Date: Thu, 4 Jan 2024 12:46:16 +0100 Subject: [PATCH] adapt to 4.1.x --- bom/openhab-addons/pom.xml | 4 - bundles/org.openhab.binding.ecowitt/pom.xml | 2 +- .../main/java/interfaces/ChannelUpdater.java | 4 +- .../main/java/interfaces/StateUpdater.java | 1 + .../handlers/EcowittThingHandler.java | 27 +- .../servlets/AbstractEcowittServlet.java | 32 +- .../servlets/EcowittGW1000Servlet.java | 48 +-- .../resources/OH-INF/thing/thing-types.xml | 289 ++++++++++++++---- bundles/pom.xml | 2 +- 9 files changed, 285 insertions(+), 124 deletions(-) diff --git a/bom/openhab-addons/pom.xml b/bom/openhab-addons/pom.xml index 6a75a19b6..f31356f0c 100644 --- a/bom/openhab-addons/pom.xml +++ b/bom/openhab-addons/pom.xml @@ -431,16 +431,12 @@ org.openhab.addons.bundles -<<<<<<< HEAD org.openhab.binding.ecovacs ${project.version} org.openhab.addons.bundles org.openhab.binding.ecowatt -======= - org.openhab.binding.ecowitt ->>>>>>> 833ca96673 (initial commit for ecowitt binding) ${project.version} diff --git a/bundles/org.openhab.binding.ecowitt/pom.xml b/bundles/org.openhab.binding.ecowitt/pom.xml index c1ddd2c19..ef4e0952a 100644 --- a/bundles/org.openhab.binding.ecowitt/pom.xml +++ b/bundles/org.openhab.binding.ecowitt/pom.xml @@ -7,7 +7,7 @@ org.openhab.addons.bundles org.openhab.addons.reactor.bundles - 3.2.0 + 4.1.1-SNAPSHOT org.openhab.binding.ecowitt diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java index c6f8a11f4..a701922a9 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java @@ -12,15 +12,13 @@ */ package interfaces; - -import org.eclipse.jdt.annotation.NonNull; import org.openhab.core.thing.Channel; -import org.openhab.core.thing.binding.ThingHandler; /** * @author Av3m - Initial contribution */ public interface ChannelUpdater { void updateChannel(Channel ch, String strValue); + void setStateUpdater(StateUpdater stateUpdater); } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java index dd471f46d..cbdb90e57 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java @@ -21,5 +21,6 @@ import org.openhab.core.types.State; */ public interface StateUpdater { Channel getChannel(String channelName); + void updateState(ChannelUID uid, State state); } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java index 6f4cc6f76..1b7106f3a 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java @@ -12,8 +12,11 @@ */ package org.openhab.binding.ecowitt.internal.handlers; -import interfaces.StateUpdater; +import javax.servlet.Servlet; +import javax.servlet.ServletException; + import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -28,16 +31,13 @@ import org.openhab.core.types.Command; import org.openhab.core.types.State; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.eclipse.jetty.server.Server; -import javax.servlet.Servlet; -import javax.servlet.ServletException; +import interfaces.StateUpdater; /** * @author Av3m - Initial contribution */ - public class EcowittThingHandler extends BaseThingHandler implements StateUpdater { private final Logger logger = LoggerFactory.getLogger(EcowittThingHandler.class); @@ -52,18 +52,16 @@ public class EcowittThingHandler extends BaseT @Override public void handleCommand(ChannelUID channelUID, Command command) { - //no command supported - do nothing! + // no command supported - do nothing! } - - @Override public void initialize() { config = getConfigAs(ecowittConfiguration.class); updateStatus(ThingStatus.UNKNOWN); - if (config == null ) { + if (config == null) { logger.error("no config object!"); updateStatus(ThingStatus.OFFLINE); return; @@ -84,16 +82,13 @@ public class EcowittThingHandler extends BaseT String thingTypeName = thing.getThingTypeUID().getId(); logger.info("url base: {}", thingTypeName); - ServletHolder holder = servletHandler.addServletWithMapping( - this.classServlet, "/" + thingTypeName); - - + ServletHolder holder = servletHandler.addServletWithMapping(this.classServlet, "/" + thingTypeName); try { server.start(); try { Servlet servlet = holder.getServlet(); - if ( servlet != null) { + if (servlet != null) { ((AbstractEcowittServlet) servlet).setStateUpdater(this); } else { logger.error("could not get servlet"); @@ -109,12 +104,11 @@ public class EcowittThingHandler extends BaseT logger.error("{}", e.getMessage()); } }); - } @Override public void dispose() { - if ( server != null && !server.isStopped() ) { + if (server != null && !server.isStopped()) { try { server.stop(); } catch (Exception e) { @@ -132,6 +126,5 @@ public class EcowittThingHandler extends BaseT @Override public void updateState(ChannelUID uid, State state) { super.updateState(uid, state); - } } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java index 6b2034084..75158587e 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java @@ -12,7 +12,12 @@ */ package org.openhab.binding.ecowitt.internal.servlets; -import interfaces.StateUpdater; +import java.util.Map; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.http.HttpStatus; @@ -20,12 +25,7 @@ import org.openhab.core.thing.Channel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Map; +import interfaces.StateUpdater; /** * @author Av3m - Initial contribution @@ -41,11 +41,9 @@ public abstract class AbstractEcowittServlet extends HttpServlet { protected StateUpdater stateUpdater = null; private final Logger logger = LoggerFactory.getLogger(AbstractEcowittServlet.class); - - protected void doPost(HttpServletRequest request, HttpServletResponse response) - { + protected void doPost(HttpServletRequest request, HttpServletResponse response) { for (Map.Entry stringEntry : request.getParameterMap().entrySet()) { - if ( stringEntry.getValue().length > 0 && !stringEntry.getValue()[0].equals("")) { + if (stringEntry.getValue().length > 0 && !stringEntry.getValue()[0].equals("")) { updateValue(stringEntry.getKey(), stringEntry.getValue()[0]); } @@ -54,19 +52,23 @@ public abstract class AbstractEcowittServlet extends HttpServlet { } private void updateValue(String field, String value) { - if ( stateUpdater == null ) { return; } + if (stateUpdater == null) { + return; + } - @Nullable Channel ch = stateUpdater.getChannel(field); + @Nullable + Channel ch = stateUpdater.getChannel(field); - if (ch == null ) { + if (ch == null) { logger.debug("channel for field {} (current value: {}) not implemented", field, value); return; } updateChannel(ch, value); - } + protected abstract void updateChannel(Channel ch, String strValue); + public abstract String getName(); public void setStateUpdater(StateUpdater stateUpdater) { diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java index 6a51b8663..b7557ba15 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java @@ -12,6 +12,10 @@ */ package org.openhab.binding.ecowitt.internal.servlets; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.library.types.DateTimeType; @@ -24,12 +28,6 @@ import org.openhab.core.library.unit.Units; import org.openhab.core.thing.Channel; import org.openhab.core.thing.type.ChannelTypeUID; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; - /** * @author Av3m - Initial contribution */ @@ -45,19 +43,23 @@ public class EcowittGW1000Servlet extends AbstractEcowittServlet { return EcowittGW1000Servlet.class.getName(); } - @Override protected void updateChannel(Channel ch, String strValue) { - if (stateUpdater == null) {return; } + if (stateUpdater == null) { + return; + } String id = ch.getUID().getId(); - @Nullable ChannelTypeUID channelUID = ch.getChannelTypeUID(); - if ( channelUID == null ) {return; } + @Nullable + ChannelTypeUID channelUID = ch.getChannelTypeUID(); + if (channelUID == null) { + return; + } String type = channelUID.getId(); float value; - switch(type) { + switch (type) { case "humidity": case "atmospheric-humidity": @@ -118,11 +120,11 @@ public class EcowittGW1000Servlet extends AbstractEcowittServlet { case "battery-level": value = Float.parseFloat(strValue); int batLevel = 0; - switch(id) { + switch (id) { case "pm25batt1": case "pm25batt2": case "wh57batt": - batLevel = (int) (100/5 * value); + batLevel = (int) (100 / 5 * value); break; case "wh40batt": case "soilbatt1": @@ -131,16 +133,23 @@ public class EcowittGW1000Servlet extends AbstractEcowittServlet { case "soilbatt4": float minLevel = 1.1f; float maxLevel = 1.5f; - if (value >= maxLevel) {batLevel = 100;} - else if (value < minLevel) {batLevel = 0;} - else { batLevel = (int) (100 * (value - minLevel) / (maxLevel - minLevel)); } + if (value >= maxLevel) { + batLevel = 100; + } else if (value < minLevel) { + batLevel = 0; + } else { + batLevel = (int) (100 * (value - minLevel) / (maxLevel - minLevel)); + } break; case "wh65batt": case "battout": case "wh26batt": case "wh25batt": - if ( value == 0) { batLevel = 100;} - else { batLevel = 0;} + if (value == 0) { + batLevel = 100; + } else { + batLevel = 0; + } break; } stateUpdater.updateState(ch.getUID(), new QuantityType<>(batLevel, Units.PERCENT)); @@ -148,7 +157,4 @@ public class EcowittGW1000Servlet extends AbstractEcowittServlet { } } - - - } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.ecowitt/src/main/resources/OH-INF/thing/thing-types.xml index 4fe61e8f9..0ef92ddfa 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.ecowitt/src/main/resources/OH-INF/thing/thing-types.xml @@ -9,73 +9,238 @@ Thing for GW1000 Weather Station - outdoor temperature - indoor temperature - temperature CH1 - temperature CH2 - temperature CH3 - temperature CH4 - temperature CH5 - temperature CH6 - temperature CH7 - temperature CH8 + + + outdoor temperature + + + + indoor temperature + + + + temperature CH1 + + + + temperature CH2 + + + + temperature CH3 + + + + temperature CH4 + + + + temperature CH5 + + + + temperature CH6 + + + + temperature CH7 + + + + temperature CH8 + - outdoor humidity - indoor humidity - humidity CH1 - humidity CH2 - humidity CH3 - humidity CH4 - humidity CH5 - humidity CH6 - humidity CH7 - humidity CH8 + + + outdoor humidity + + + + indoor humidity + + + + humidity CH1 + + + + humidity CH2 + + + + humidity CH3 + + + + humidity CH4 + + + + humidity CH5 + + + + humidity CH6 + + + + humidity CH7 + + + + humidity CH8 + - wind direction - wind speed - wind gust - daily maximum value gust + + + wind direction + + + + wind speed + + + + wind gust + + + + daily maximum value gust + - total amount of rain - amount of rain (event-based) - yearly amount of rain - monthly amount of rain - weekly amount of rain - daily amount of rain - hourly amount of rain - rain amount per hour + + + total amount of rain + + + + amount of rain (event-based) + + + + yearly amount of rain + + + + monthly amount of rain + + + + weekly amount of rain + + + + daily amount of rain + + + + hourly amount of rain + + + + rain amount per hour + - solar radiation (intensity) - uv index + + + solar radiation (intensity) + + + + uv index + - absolute barometric pressure - relative barometric pressure + + + absolute barometric pressure + + + + relative barometric pressure + - soil moisture CH1 - soil moisture CH2 - soil moisture CH3 - soil moisture CH4 + + + soil moisture CH1 + + + + soil moisture CH2 + + + + soil moisture CH3 + + + + soil moisture CH4 + - distance of last lightning - timestamp of last lightning - number of lightnings + + + distance of last lightning + + + + timestamp of last lightning + + + + number of lightnings + - PM2.5 particle sensor CH1 - daily average PM2.5 particle sensor CH1 - PM2.5 particle sensor CH2 - daily average PM2.5 particle sensor CH2 + + + PM2.5 particle sensor CH1 + + + + daily average PM2.5 particle sensor CH1 + + + + PM2.5 particle sensor CH2 + + + + daily average PM2.5 particle sensor CH2 + - battery level WH57 sensor - battery level WH65 sensor - battery level PM2.5 CH1 sensor - battery level PM2.5 CH2 sensor - battery level soil moisture sensor CH1 - battery level soil moisture sensor CH2 - battery level soil moisture sensor CH3 - battery level soil moisture sensor CH4 + + + battery level WH57 sensor + + + + battery level WH65 sensor + + + + battery level PM2.5 CH1 sensor + + + + battery level PM2.5 CH2 sensor + + + + battery level soil moisture sensor CH1 + + + + battery level soil moisture sensor CH2 + + + + battery level soil moisture sensor CH3 + + + + battery level soil moisture sensor CH4 + @@ -84,7 +249,7 @@ 0.0.0.0 - + 40111 @@ -96,28 +261,28 @@ Number:Dimensionless Humidity - + Number:Length Rain - + Number:Speed Rain - + Number:Intensity Sun - + @@ -130,7 +295,7 @@ Number:Dimensionless Moisture - + @@ -151,7 +316,7 @@ Number - + diff --git a/bundles/pom.xml b/bundles/pom.xml index 6174aacff..a7248e7a7 100644 --- a/bundles/pom.xml +++ b/bundles/pom.xml @@ -120,7 +120,7 @@ org.openhab.binding.ecotouch org.openhab.binding.ecovacs org.openhab.binding.ecowatt - org.openhab.binding.ecowitt + org.openhab.binding.ecowitt org.openhab.binding.ekey org.openhab.binding.electroluxair org.openhab.binding.elerotransmitterstick