2 * Copyright (c) 2010-2024 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.Frequency;
25 import javax.measure.quantity.Power;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.omnikinverter.internal.OmnikInverter;
30 import org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants;
31 import org.openhab.binding.omnikinverter.internal.OmnikInverterConfiguration;
32 import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.unit.SIUnits;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.binding.BaseThingHandler;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link OmnikInverterHandler} is responsible for handling commands, which are
48 * sent to one of the channels.
50 * @author Hans van den Bogert - Initial contribution
53 public class OmnikInverterHandler extends BaseThingHandler {
54 private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
56 private @Nullable OmnikInverter inverter;
57 private @Nullable ScheduledFuture<?> pollJob;
59 public OmnikInverterHandler(Thing thing) {
64 public void handleCommand(ChannelUID channelUID, Command command) {
65 // All channels depend on data gotten from `updateData()`
66 if (command instanceof RefreshType) {
72 public void initialize() {
73 OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
75 inverter = new OmnikInverter(config.hostname, config.port, config.serial);
76 updateStatus(ThingStatus.UNKNOWN);
77 pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
81 public void dispose() {
82 ScheduledFuture<?> pollJob = this.pollJob;
83 if (pollJob != null) {
90 private void updateData() {
92 if (inverter != null) {
93 OmnikInverterMessage message = inverter.pullCurrentStats();
95 updateStatus(ThingStatus.ONLINE);
100 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
101 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
103 QuantityType<Power> powerQuantity1 = new QuantityType<>(message.getPowerAC1(), Units.WATT);
104 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC1, powerQuantity1);
106 QuantityType<Power> powerQuantity2 = new QuantityType<>(message.getPowerAC2(), Units.WATT);
107 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC2, powerQuantity2);
109 QuantityType<Power> powerQuantity3 = new QuantityType<>(message.getPowerAC3(), Units.WATT);
110 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC3, powerQuantity3);
112 QuantityType<ElectricPotential> voltageQuantity1 = new QuantityType<>(message.getVoltageAC1(),
114 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC1, voltageQuantity1);
116 QuantityType<ElectricPotential> voltageQuantity2 = new QuantityType<>(message.getVoltageAC2(),
118 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC2, voltageQuantity2);
120 QuantityType<ElectricPotential> voltageQuantity3 = new QuantityType<>(message.getVoltageAC3(),
122 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC3, voltageQuantity3);
124 QuantityType<ElectricCurrent> currentQuantity1 = new QuantityType<>(message.getCurrentAC1(),
126 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC1, currentQuantity1);
128 QuantityType<ElectricCurrent> currentQuantity2 = new QuantityType<>(message.getCurrentAC2(),
130 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC2, currentQuantity2);
132 QuantityType<ElectricCurrent> currentQuantity3 = new QuantityType<>(message.getCurrentAC3(),
134 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC3, currentQuantity3);
136 QuantityType<Frequency> frequencyQuantity1 = new QuantityType<>(message.getFrequencyAC1(), Units.HERTZ);
137 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC1, frequencyQuantity1);
139 QuantityType<Frequency> frequencyQuantity2 = new QuantityType<>(message.getFrequencyAC2(), Units.HERTZ);
140 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC2, frequencyQuantity2);
142 QuantityType<Frequency> frequencyQuantity3 = new QuantityType<>(message.getFrequencyAC3(), Units.HERTZ);
143 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC3, frequencyQuantity3);
149 QuantityType<ElectricCurrent> pvAmp1 = new QuantityType<>(message.getCurrentPV1(), Units.AMPERE);
150 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV1, pvAmp1);
152 QuantityType<ElectricCurrent> pvAmp2 = new QuantityType<>(message.getCurrentPV2(), Units.AMPERE);
153 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV2, pvAmp2);
155 QuantityType<ElectricCurrent> pvAmp3 = new QuantityType<>(message.getCurrentPV3(), Units.AMPERE);
156 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV3, pvAmp3);
158 QuantityType<ElectricPotential> pvVoltage1 = new QuantityType<>(message.getVoltagePV1(), Units.VOLT);
159 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV1, pvVoltage1);
161 QuantityType<ElectricPotential> pvVoltage2 = new QuantityType<>(message.getVoltagePV2(), Units.VOLT);
162 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV2, pvVoltage2);
164 QuantityType<ElectricPotential> pvVoltage3 = new QuantityType<>(message.getVoltagePV3(), Units.VOLT);
165 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV3, pvVoltage3);
170 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
171 new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
173 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
174 new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
176 updateState(OmnikInverterBindingConstants.CHANNEL_TEMPERATURE,
177 new QuantityType<>(message.getTemperature(), SIUnits.CELSIUS));
179 updateState(OmnikInverterBindingConstants.CHANNEL_HOURS_TOTAL,
180 new QuantityType<>(message.getHoursTotal(), Units.HOUR));
182 } catch (UnknownHostException | NoRouteToHostException | ConnectException e) {
183 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
184 } catch (IOException e) {
185 logger.debug("Unknown exception when pulling data from the inverter: {}", e.getMessage());
186 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Unknown error: " + e.getMessage());