From 13b4080e4ab0b3d87423f9d43898d0dbbbdd4cc7 Mon Sep 17 00:00:00 2001 From: Av3m <7688354+Av3m@users.noreply.github.com> Date: Sat, 8 Jan 2022 21:26:34 +0100 Subject: [PATCH] initial commit for ecowitt binding --- bundles/org.openhab.binding.ecowitt/pom.xml | 2 +- .../main/java/interfaces/ChannelUpdater.java | 26 +++ .../main/java/interfaces/StateUpdater.java | 25 +++ .../{ => config}/ecowittConfiguration.java | 13 +- .../internal/ecowittBindingConstants.java | 8 +- .../ecowitt/internal/ecowittHandler.java | 100 ----------- .../ecowittHandlerFactory.java | 12 +- .../handlers/EcowittThingHandler.java | 137 ++++++++++++++ .../servlets/AbstractEcowittServlet.java | 75 ++++++++ .../servlets/EcowittGW1000Servlet.java | 154 ++++++++++++++++ .../resources/OH-INF/thing/thing-types.xml | 167 +++++++++++++++--- 11 files changed, 571 insertions(+), 148 deletions(-) create mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java create mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java rename bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/{ => config}/ecowittConfiguration.java (64%) delete mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandler.java rename bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/{ => factories}/ecowittHandlerFactory.java (78%) create mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java create mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java create mode 100644 bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java diff --git a/bundles/org.openhab.binding.ecowitt/pom.xml b/bundles/org.openhab.binding.ecowitt/pom.xml index be902241c..c1ddd2c19 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-SNAPSHOT + 3.2.0 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 new file mode 100644 index 000000000..c6f8a11f4 --- /dev/null +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/ChannelUpdater.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +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 new file mode 100644 index 000000000..dd471f46d --- /dev/null +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/interfaces/StateUpdater.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package interfaces; + +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.types.State; + +/** + * @author Av3m - Initial contribution + */ +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/ecowittConfiguration.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/config/ecowittConfiguration.java similarity index 64% rename from bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittConfiguration.java rename to bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/config/ecowittConfiguration.java index 4bc9ad48c..cfd7c6acb 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittConfiguration.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/config/ecowittConfiguration.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2022 Contributors to the openHAB project + * Copyright (c) 2010-2021 Contributors to the openHAB project * * See the NOTICE file(s) distributed with this work for additional * information. @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.binding.ecowitt.internal; +package org.openhab.binding.ecowitt.internal.config; /** * The {@link ecowittConfiguration} class contains fields mapping thing configuration parameters. @@ -18,11 +18,6 @@ package org.openhab.binding.ecowitt.internal; * @author Av3m - Initial contribution */ public class ecowittConfiguration { - - /** - * Sample configuration parameters. Replace with your own. - */ - public String hostname; - public String password; - public int refreshInterval; + public String bindHostname; + public int bindPort; } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittBindingConstants.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittBindingConstants.java index 5355b38a6..ad1f5a89d 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittBindingConstants.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittBindingConstants.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2022 Contributors to the openHAB project + * Copyright (c) 2010-2021 Contributors to the openHAB project * * See the NOTICE file(s) distributed with this work for additional * information. @@ -21,14 +21,12 @@ import org.openhab.core.thing.ThingTypeUID; * * @author Av3m - Initial contribution */ + @NonNullByDefault public class ecowittBindingConstants { private static final String BINDING_ID = "ecowitt"; // List of all Thing Type UIDs - public static final ThingTypeUID THING_TYPE_SAMPLE = new ThingTypeUID(BINDING_ID, "sample"); - - // List of all Channel ids - public static final String CHANNEL_1 = "channel1"; + public static final ThingTypeUID THING_TYPE_GW1000 = new ThingTypeUID(BINDING_ID, "gw1000"); } diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandler.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandler.java deleted file mode 100644 index f62c82b1c..000000000 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandler.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Copyright (c) 2010-2022 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.ecowitt.internal; - -import static org.openhab.binding.ecowitt.internal.ecowittBindingConstants.*; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.thing.ChannelUID; -import org.openhab.core.thing.Thing; -import org.openhab.core.thing.ThingStatus; -import org.openhab.core.thing.binding.BaseThingHandler; -import org.openhab.core.types.Command; -import org.openhab.core.types.RefreshType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The {@link ecowittHandler} is responsible for handling commands, which are - * sent to one of the channels. - * - * @author Av3m - Initial contribution - */ -@NonNullByDefault -public class ecowittHandler extends BaseThingHandler { - - private final Logger logger = LoggerFactory.getLogger(ecowittHandler.class); - - private @Nullable ecowittConfiguration config; - - public ecowittHandler(Thing thing) { - super(thing); - } - - @Override - public void handleCommand(ChannelUID channelUID, Command command) { - if (CHANNEL_1.equals(channelUID.getId())) { - if (command instanceof RefreshType) { - // TODO: handle data refresh - } - - // TODO: handle command - - // Note: if communication with thing fails for some reason, - // indicate that by setting the status with detail information: - // updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - // "Could not control device at IP address x.x.x.x"); - } - } - - @Override - public void initialize() { - config = getConfigAs(ecowittConfiguration.class); - - // TODO: Initialize the handler. - // The framework requires you to return from this method quickly. Also, before leaving this method a thing - // status from one of ONLINE, OFFLINE or UNKNOWN must be set. This might already be the real thing status in - // case you can decide it directly. - // In case you can not decide the thing status directly (e.g. for long running connection handshake using WAN - // access or similar) you should set status UNKNOWN here and then decide the real status asynchronously in the - // background. - - // set the thing status to UNKNOWN temporarily and let the background task decide for the real status. - // the framework is then able to reuse the resources from the thing handler initialization. - // we set this upfront to reliably check status updates in unit tests. - updateStatus(ThingStatus.UNKNOWN); - - // Example for background initialization: - scheduler.execute(() -> { - boolean thingReachable = true; // - // when done do: - if (thingReachable) { - updateStatus(ThingStatus.ONLINE); - } else { - updateStatus(ThingStatus.OFFLINE); - } - }); - - // These logging types should be primarily used by bindings - // logger.trace("Example trace message"); - // logger.debug("Example debug message"); - // logger.warn("Example warn message"); - - // Note: When initialization can NOT be done set the status with more details for further - // analysis. See also class ThingStatusDetail for all available status details. - // Add a description to give user information to understand why thing does not work as expected. E.g. - // updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - // "Can not access device as username and/or password are invalid"); - } -} diff --git a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandlerFactory.java b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/factories/ecowittHandlerFactory.java similarity index 78% rename from bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandlerFactory.java rename to bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/factories/ecowittHandlerFactory.java index fab3a4e0b..1f22f0c43 100644 --- a/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/ecowittHandlerFactory.java +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/factories/ecowittHandlerFactory.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2022 Contributors to the openHAB project + * Copyright (c) 2010-2021 Contributors to the openHAB project * * See the NOTICE file(s) distributed with this work for additional * information. @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.binding.ecowitt.internal; +package org.openhab.binding.ecowitt.internal.factories; import static org.openhab.binding.ecowitt.internal.ecowittBindingConstants.*; @@ -18,6 +18,8 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.ecowitt.internal.handlers.EcowittThingHandler; +import org.openhab.binding.ecowitt.internal.servlets.EcowittGW1000Servlet; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.binding.BaseThingHandlerFactory; @@ -35,7 +37,7 @@ import org.osgi.service.component.annotations.Component; @Component(configurationPid = "binding.ecowitt", service = ThingHandlerFactory.class) public class ecowittHandlerFactory extends BaseThingHandlerFactory { - private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SAMPLE); + private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_GW1000); @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { @@ -46,8 +48,8 @@ public class ecowittHandlerFactory extends BaseThingHandlerFactory { protected @Nullable ThingHandler createHandler(Thing thing) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); - if (THING_TYPE_SAMPLE.equals(thingTypeUID)) { - return new ecowittHandler(thing); + if (THING_TYPE_GW1000.equals(thingTypeUID)) { + return new EcowittThingHandler(thing, EcowittGW1000Servlet.class); } return null; 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 new file mode 100644 index 000000000..6f4cc6f76 --- /dev/null +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/handlers/EcowittThingHandler.java @@ -0,0 +1,137 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.ecowitt.internal.handlers; + +import interfaces.StateUpdater; +import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.ServletHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.openhab.binding.ecowitt.internal.config.ecowittConfiguration; +import org.openhab.binding.ecowitt.internal.servlets.AbstractEcowittServlet; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.Thing; +import org.openhab.core.thing.ThingStatus; +import org.openhab.core.thing.binding.BaseThingHandler; +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; + +/** + * @author Av3m - Initial contribution + */ + + +public class EcowittThingHandler extends BaseThingHandler implements StateUpdater { + private final Logger logger = LoggerFactory.getLogger(EcowittThingHandler.class); + + private @Nullable ecowittConfiguration config; + private @Nullable Server server; + private Class classServlet; + + public EcowittThingHandler(Thing thing, Class classServlet) { + super(thing); + this.classServlet = classServlet; + } + + @Override + public void handleCommand(ChannelUID channelUID, Command command) { + //no command supported - do nothing! + } + + + + @Override + public void initialize() { + config = getConfigAs(ecowittConfiguration.class); + + updateStatus(ThingStatus.UNKNOWN); + + if (config == null ) { + logger.error("no config object!"); + updateStatus(ThingStatus.OFFLINE); + return; + } + + // Example for background initialization: + scheduler.execute(() -> { + server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setHost(config.bindHostname); + connector.setPort(config.bindPort); + connector.setReuseAddress(true); + server.addConnector(connector); + + ServletHandler servletHandler = new ServletHandler(); + server.setHandler(servletHandler); + + String thingTypeName = thing.getThingTypeUID().getId(); + logger.info("url base: {}", thingTypeName); + + ServletHolder holder = servletHandler.addServletWithMapping( + this.classServlet, "/" + thingTypeName); + + + + try { + server.start(); + try { + Servlet servlet = holder.getServlet(); + if ( servlet != null) { + ((AbstractEcowittServlet) servlet).setStateUpdater(this); + } else { + logger.error("could not get servlet"); + } + + } catch (ServletException e) { + logger.error("could not get servlet"); + } + updateStatus(ThingStatus.ONLINE); + + } catch (Exception e) { + updateStatus(ThingStatus.OFFLINE); + logger.error("{}", e.getMessage()); + } + }); + + } + + @Override + public void dispose() { + if ( server != null && !server.isStopped() ) { + try { + server.stop(); + } catch (Exception e) { + logger.error("could not stop server"); + logger.error("{}", e.getMessage()); + } + } + } + + @Override + public Channel getChannel(String channelName) { + return thing.getChannel(channelName); + } + + @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 new file mode 100644 index 000000000..6b2034084 --- /dev/null +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/AbstractEcowittServlet.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.ecowitt.internal.servlets; + +import interfaces.StateUpdater; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.jetty.http.HttpStatus; +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; + +/** + * @author Av3m - Initial contribution + */ +@NonNullByDefault +public abstract class AbstractEcowittServlet extends HttpServlet { + + public AbstractEcowittServlet() { + super(); + } + + @Nullable + protected StateUpdater stateUpdater = null; + private final Logger logger = LoggerFactory.getLogger(AbstractEcowittServlet.class); + + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + { + for (Map.Entry stringEntry : request.getParameterMap().entrySet()) { + if ( stringEntry.getValue().length > 0 && !stringEntry.getValue()[0].equals("")) { + updateValue(stringEntry.getKey(), stringEntry.getValue()[0]); + } + + } + response.setStatus(HttpStatus.OK_200); + } + + private void updateValue(String field, String value) { + if ( stateUpdater == null ) { return; } + + @Nullable Channel ch = stateUpdater.getChannel(field); + + 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) { + this.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 new file mode 100644 index 000000000..6a51b8663 --- /dev/null +++ b/bundles/org.openhab.binding.ecowitt/src/main/java/org/openhab/binding/ecowitt/internal/servlets/EcowittGW1000Servlet.java @@ -0,0 +1,154 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.ecowitt.internal.servlets; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.library.types.DateTimeType; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.unit.ImperialUnits; +import org.openhab.core.library.unit.MetricPrefix; +import org.openhab.core.library.unit.SIUnits; +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 + */ +@NonNullByDefault +public class EcowittGW1000Servlet extends AbstractEcowittServlet { + + public EcowittGW1000Servlet() { + super(); + } + + @Override + public String getName() { + return EcowittGW1000Servlet.class.getName(); + } + + + @Override + protected void updateChannel(Channel ch, String strValue) { + if (stateUpdater == null) {return; } + + String id = ch.getUID().getId(); + @Nullable ChannelTypeUID channelUID = ch.getChannelTypeUID(); + if ( channelUID == null ) {return; } + + String type = channelUID.getId(); + float value; + + switch(type) { + + case "humidity": + case "atmospheric-humidity": + case "moisture": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, Units.PERCENT)); + break; + case "temperature": + case "outdoor-temperature": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, ImperialUnits.FAHRENHEIT)); + break; + case "uv-index": + case "lightning-num": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new DecimalType(value)); + break; + case "rain-amount": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, ImperialUnits.INCH)); + break; + case "rain-rate": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, Units.INCHES_PER_HOUR)); + break; + case "wind-speed": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, ImperialUnits.MILES_PER_HOUR)); + break; + case "wind-direction": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, Units.DEGREE_ANGLE)); + break; + case "barometric-pressure": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, ImperialUnits.INCH_OF_MERCURY)); + break; + case "solar-radiation": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, Units.IRRADIANCE)); + break; + + case "lightning-distance": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, MetricPrefix.KILO(SIUnits.METRE))); + break; + + case "lightning-timestamp": + Instant i = Instant.ofEpochSecond(Long.parseLong(strValue)); + stateUpdater.updateState(ch.getUID(), new DateTimeType(ZonedDateTime.ofInstant(i, ZoneOffset.UTC))); + break; + + case "pm25-particles": + value = Float.parseFloat(strValue); + stateUpdater.updateState(ch.getUID(), new QuantityType<>(value, Units.MICROGRAM_PER_CUBICMETRE)); + break; + + case "battery-level": + value = Float.parseFloat(strValue); + int batLevel = 0; + switch(id) { + case "pm25batt1": + case "pm25batt2": + case "wh57batt": + batLevel = (int) (100/5 * value); + break; + case "wh40batt": + case "soilbatt1": + case "soilbatt2": + case "soilbatt3": + 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)); } + break; + case "wh65batt": + case "battout": + case "wh26batt": + case "wh25batt": + if ( value == 0) { batLevel = 100;} + else { batLevel = 0;} + break; + } + stateUpdater.updateState(ch.getUID(), new QuantityType<>(batLevel, Units.PERCENT)); + break; + + } + } + + + +} 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 04c8e3c1f..4fe61e8f9 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 @@ -5,42 +5,153 @@ xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> - - - - - Sample thing for ecowitt Binding - + + + 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 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 + + 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 + + + absolute barometric pressure + relative barometric pressure + + soil moisture CH1 + soil moisture CH2 + soil moisture CH3 + soil moisture CH4 + + 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 + + + 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 + - + network-address - - Hostname or IP address of the device + + 0.0.0.0 + - - password - - Passwort to access the device - - - - Interval the device is polled in sec. + + + 40111 + - - - Number:Temperature - - Sample channel for ecowitt Binding + + Number:Dimensionless + + Humidity + + + + Number:Length + + Rain + + + + + Number:Speed + + Rain + + + + + Number:Intensity + + Sun + + + + + Number + + Sun + + + + Number:Dimensionless + + Moisture + + + + + Number:Length + + Energy + + + Number + + Energy + + + DateTime + + Energy + + + Number + + + +