]> git.basschouten.com Git - openhab-addons.git/blob
100e38b9ef8ca78b0f75b88110d28d988d79baa9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.energenie.internal.handler;
14
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;
21
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;
36
37 /**
38  * The {@link EnergeniePWMHandler} is responsible for reading states and update PWM channels.
39  *
40  * @author Hans-Jörg Merk - Initial contribution
41  */
42
43 @NonNullByDefault
44 public class EnergeniePWMHandler extends BaseThingHandler {
45
46     private final Logger logger = LoggerFactory.getLogger(EnergeniePWMHandler.class);
47
48     private static final int HTTP_TIMEOUT_MILLISECONDS = 6000;
49
50     private String host = "";
51     private String password = "";
52     private int refreshInterval;
53
54     private @Nullable ScheduledFuture<?> refreshJob;
55
56     public EnergeniePWMHandler(Thing thing) {
57         super(thing);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         if (command instanceof RefreshType) {
63             scheduler.execute(this::getState);
64         }
65     }
66
67     @Override
68     public void initialize() {
69         EnergenieConfiguration config = getConfigAs(EnergenieConfiguration.class);
70
71         if (!config.host.isEmpty() && !config.password.isEmpty()) {
72             host = config.host;
73             password = config.password;
74             refreshInterval = EnergenieConfiguration.DEFAULT_REFRESH_INTERVAL;
75             logger.debug("Initializing EnergeniePWMHandler for Host '{}'", host);
76             updateStatus(ThingStatus.ONLINE);
77             onUpdate();
78         } else {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80                     "Can not access device , IP-Address or password not set");
81         }
82     }
83
84     @Override
85     public void dispose() {
86         logger.debug("EnergeniePWMHandler disposed.");
87         final ScheduledFuture<?> refreshJob = this.refreshJob;
88
89         if (refreshJob != null) {
90             refreshJob.cancel(true);
91             this.refreshJob = null;
92         }
93     }
94
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;
100
101         try {
102             logger.trace("sending 'POST' request to URL : {}", url);
103             loginResponseString = HttpUtil.executeUrl("POST", url, urlContent, "TEXT/PLAIN", HTTP_TIMEOUT_MILLISECONDS);
104
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));
110                 try {
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());
116                 }
117             }
118
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());
122         }
123     }
124
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);
129         }
130     }
131 }