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.kostalinverter.internal.firstgeneration;
15 import java.io.IOException;
16 import java.math.BigDecimal;
17 import java.util.ArrayList;
18 import java.util.Base64;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.concurrent.TimeUnit;
23 import javax.measure.Unit;
25 import org.jsoup.Jsoup;
26 import org.jsoup.nodes.Document;
27 import org.jsoup.nodes.Element;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.State;
39 import org.openhab.core.types.UnDefType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * @author Christian Schneider - Initial contribution
45 * @author Christoph Weitkamp - Incorporated new QuantityType (Units of Measurement)
47 public class WebscrapeHandler extends BaseThingHandler {
48 private Logger logger = LoggerFactory.getLogger(WebscrapeHandler.class);
49 private SourceConfig config;
51 private final List<ChannelConfig> channelConfigs = new ArrayList<>();
53 public WebscrapeHandler(Thing thing) {
55 channelConfigs.add(new ChannelConfig("acPower", "td", 4, Units.WATT));
56 channelConfigs.add(new ChannelConfig("totalEnergy", "td", 7, Units.KILOWATT_HOUR));
57 channelConfigs.add(new ChannelConfig("dayEnergy", "td", 10, Units.KILOWATT_HOUR));
58 channelConfigs.add(new ChannelConfig("status", "td", 13, null));
59 channelConfigs.add(new ChannelConfig("str1Voltage", "td", 19, Units.VOLT));
60 channelConfigs.add(new ChannelConfig("str1Current", "td", 25, Units.AMPERE));
61 channelConfigs.add(new ChannelConfig("str2Voltage", "td", 33, Units.VOLT));
62 channelConfigs.add(new ChannelConfig("str2Current", "td", 39, Units.AMPERE));
63 channelConfigs.add(new ChannelConfig("l1Voltage", "td", 22, Units.VOLT));
64 channelConfigs.add(new ChannelConfig("l1Power", "td", 28, Units.WATT));
65 channelConfigs.add(new ChannelConfig("l2Voltage", "td", 36, Units.VOLT));
66 channelConfigs.add(new ChannelConfig("l2Power", "td", 42, Units.WATT));
67 channelConfigs.add(new ChannelConfig("l3Voltage", "td", 46, Units.VOLT));
68 channelConfigs.add(new ChannelConfig("l3Power", "td", 49, Units.WATT));
72 public void initialize() {
73 config = getConfigAs(SourceConfig.class);
74 scheduler.scheduleWithFixedDelay(() -> {
77 updateStatus(ThingStatus.ONLINE);
78 } catch (Exception e) {
79 logger.debug("Error refreshing source '{}'", getThing().getUID(), e);
80 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
81 e.getClass().getName() + ":" + e.getMessage());
83 }, 0, config.refreshInterval, TimeUnit.SECONDS);
87 public void handleCommand(ChannelUID channelUID, Command command) {
91 private void refresh() throws Exception {
92 Document doc = getDoc();
93 for (ChannelConfig cConfig : channelConfigs) {
94 Channel channel = getThing().getChannel(cConfig.id);
95 if (channel != null) {
96 String value = getTag(doc, cConfig.tag).get(cConfig.num);
97 updateState(channel.getUID(), getState(value, cConfig.unit));
102 private static List<String> getTag(Document doc, String tag) {
103 List<String> result = new ArrayList<>();
104 Iterator<Element> elIt = doc.getElementsByTag(tag).iterator();
105 while (elIt.hasNext()) {
106 String content = elIt.next().text();
107 content = content.replace("\u00A0", "").trim();
108 if (!content.isEmpty()) {
115 private Document getDoc() throws IOException {
116 String login = config.userName + ":" + config.password;
117 String base64login = new String(Base64.getEncoder().encode(login.getBytes()));
118 return Jsoup.connect(config.url).header("Authorization", "Basic " + base64login).get();
121 private State getState(String value, Unit<?> unit) {
123 return new StringType(value);
126 return new QuantityType<>(new BigDecimal(value), unit);
127 } catch (NumberFormatException e) {
128 logger.debug("Error parsing value '{}'", value, e);
129 return UnDefType.UNDEF;