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.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.ElectricCurrent;
23 import javax.measure.quantity.ElectricPotential;
24 import javax.measure.quantity.Power;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.omnikinverter.internal.OmnikInverter;
29 import org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants;
30 import org.openhab.binding.omnikinverter.internal.OmnikInverterConfiguration;
31 import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.library.unit.Units;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingStatusDetail;
38 import org.openhab.core.thing.binding.BaseThingHandler;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.RefreshType;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link OmnikInverterHandler} is responsible for handling commands, which are
46 * sent to one of the channels.
48 * @author Hans van den Bogert - Initial contribution
51 public class OmnikInverterHandler extends BaseThingHandler {
52 private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
54 private @Nullable OmnikInverter inverter;
55 private @Nullable ScheduledFuture<?> pollJob;
57 public OmnikInverterHandler(Thing thing) {
62 public void handleCommand(ChannelUID channelUID, Command command) {
63 // All channels depend on data gotten from `updateData()`
64 if (command instanceof RefreshType) {
70 public void initialize() {
71 OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
73 inverter = new OmnikInverter(config.hostname, config.port, config.serial);
74 updateStatus(ThingStatus.UNKNOWN);
75 pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
79 public void dispose() {
80 ScheduledFuture<?> pollJob = this.pollJob;
81 if (pollJob != null) {
88 private void updateData() {
90 if (inverter != null) {
91 OmnikInverterMessage message = inverter.pullCurrentStats();
93 updateStatus(ThingStatus.ONLINE);
95 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
96 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
98 QuantityType<Power> powerQuantity1 = new QuantityType<>(message.getPowerAC1(), Units.WATT);
99 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC1, powerQuantity1);
101 QuantityType<Power> powerQuantity2 = new QuantityType<>(message.getPowerAC2(), Units.WATT);
102 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC2, powerQuantity2);
104 QuantityType<Power> powerQuantity3 = new QuantityType<>(message.getPowerAC3(), Units.WATT);
105 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC3, powerQuantity3);
107 QuantityType<ElectricCurrent> pvAmp1 = new QuantityType<>(message.getCurrentPV1(), Units.AMPERE);
108 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV1, pvAmp1);
110 QuantityType<ElectricCurrent> pvAmp2 = new QuantityType<>(message.getCurrentPV2(), Units.AMPERE);
111 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV2, pvAmp2);
113 QuantityType<ElectricCurrent> pvAmp3 = new QuantityType<>(message.getCurrentPV3(), Units.AMPERE);
114 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV3, pvAmp3);
116 QuantityType<ElectricPotential> pvVoltage1 = new QuantityType<>(message.getVoltagePV1(), Units.VOLT);
117 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV1, pvVoltage1);
119 QuantityType<ElectricPotential> pvVoltage2 = new QuantityType<>(message.getVoltagePV2(), Units.VOLT);
120 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV2, pvVoltage2);
122 QuantityType<ElectricPotential> pvVoltage3 = new QuantityType<>(message.getVoltagePV3(), Units.VOLT);
123 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV3, pvVoltage3);
125 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
126 new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
128 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
129 new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
131 } catch (UnknownHostException | NoRouteToHostException | ConnectException e) {
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
133 } catch (IOException e) {
134 logger.debug("Unknown exception when pulling data from the inverter: {}", e.getMessage());
135 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Unknown error: " + e.getMessage());