2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.energenie.internal.handler;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.charset.StandardCharsets;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.energenie.internal.EnergeniePWMStateEnum;
25 import org.openhab.binding.energenie.internal.config.EnergenieConfiguration;
26 import org.openhab.core.io.net.http.HttpUtil;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link EnergeniePWMHandler} is responsible for reading states and update PWM channels.
40 * @author Hans-Jörg Merk - Initial contribution
44 public class EnergeniePWMHandler extends BaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(EnergeniePWMHandler.class);
48 private static final int HTTP_TIMEOUT_MILLISECONDS = 6000;
50 private String host = "";
51 private String password = "";
52 private int refreshInterval;
54 private @Nullable ScheduledFuture<?> refreshJob;
56 public EnergeniePWMHandler(Thing thing) {
61 public void handleCommand(ChannelUID channelUID, Command command) {
62 if (command instanceof RefreshType) {
63 scheduler.execute(this::getState);
68 public void initialize() {
69 EnergenieConfiguration config = getConfigAs(EnergenieConfiguration.class);
71 if (!config.host.isEmpty() && !config.password.isEmpty()) {
73 password = config.password;
74 refreshInterval = EnergenieConfiguration.DEFAULT_REFRESH_INTERVAL;
75 logger.debug("Initializing EnergeniePWMHandler for Host '{}'", host);
76 updateStatus(ThingStatus.ONLINE);
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80 "Can not access device , IP-Address or password not set");
85 public void dispose() {
86 logger.debug("EnergeniePWMHandler disposed.");
87 final ScheduledFuture<?> refreshJob = this.refreshJob;
89 if (refreshJob != null) {
90 refreshJob.cancel(true);
91 this.refreshJob = null;
95 public synchronized void getState() {
96 String url = "http://" + host + "/login.html";
97 String urlParameters = "pw=" + password;
98 InputStream urlContent = new ByteArrayInputStream(urlParameters.getBytes(StandardCharsets.UTF_8));
99 String loginResponseString = null;
102 logger.trace("sending 'POST' request to URL : {}", url);
103 loginResponseString = HttpUtil.executeUrl("POST", url, urlContent, "TEXT/PLAIN", HTTP_TIMEOUT_MILLISECONDS);
105 if (loginResponseString != null) {
106 updateState("voltage", EnergeniePWMStateEnum.VOLTAGE.readState(loginResponseString));
107 updateState("current", EnergeniePWMStateEnum.CURRENT.readState(loginResponseString));
108 updateState("power", EnergeniePWMStateEnum.POWER.readState(loginResponseString));
109 updateState("energy", EnergeniePWMStateEnum.ENERGY.readState(loginResponseString));
111 HttpUtil.executeUrl("POST", url, HTTP_TIMEOUT_MILLISECONDS);
112 logger.trace("logout from ip {}", host);
113 } catch (IOException e) {
114 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
115 "failed to logout: " + e.getMessage());
119 } catch (IOException e) {
120 logger.debug("energenie: failed to login to {} with ip {}", thing.getUID(), host, e);
121 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
125 private synchronized void onUpdate() {
126 ScheduledFuture<?> refreshJob = this.refreshJob;
127 if (refreshJob == null || refreshJob.isCancelled()) {
128 this.refreshJob = scheduler.scheduleWithFixedDelay(this::getState, 5, refreshInterval, TimeUnit.SECONDS);