2 * Copyright (c) 2010-2021 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.omnikinverter.internal.handler;
15 import java.io.IOException;
16 import java.net.ConnectException;
17 import java.net.NoRouteToHostException;
18 import java.net.UnknownHostException;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import javax.measure.quantity.Power;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.omnikinverter.internal.OmnikInverter;
27 import org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants;
28 import org.openhab.binding.omnikinverter.internal.OmnikInverterConfiguration;
29 import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
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.RefreshType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link OmnikInverterHandler} is responsible for handling commands, which are
44 * sent to one of the channels.
46 * @author Hans van den Bogert - Initial contribution
49 public class OmnikInverterHandler extends BaseThingHandler {
50 private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
52 private @Nullable OmnikInverter inverter;
53 private @Nullable ScheduledFuture<?> pollJob;
55 public OmnikInverterHandler(Thing thing) {
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) {
62 if (command instanceof RefreshType) {
69 public void initialize() {
70 OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
72 inverter = new OmnikInverter(config.hostname, config.port, config.serial);
73 updateStatus(ThingStatus.UNKNOWN);
74 pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
78 public void dispose() {
79 ScheduledFuture<?> pollJob = this.pollJob;
80 if (pollJob != null) {
87 private void updateData() {
89 if (inverter != null) {
90 OmnikInverterMessage message = inverter.pullCurrentStats();
92 updateStatus(ThingStatus.ONLINE);
94 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
95 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
97 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
98 new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
100 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
101 new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
103 } catch (UnknownHostException | NoRouteToHostException | ConnectException e) {
104 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
105 } catch (IOException e) {
106 logger.debug("Unknown exception when pulling data from the inverter: {}", e.getMessage());
107 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Unknown error: " + e.getMessage());