]> git.basschouten.com Git - openhab-addons.git/blob
fdf95cf952e391c87c9f193a4f36e679a434e2d5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.omnikinverter.internal.handler;
14
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;
21
22 import javax.measure.quantity.ElectricCurrent;
23 import javax.measure.quantity.ElectricPotential;
24 import javax.measure.quantity.Frequency;
25 import javax.measure.quantity.Power;
26
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;
45
46 /**
47  * The {@link OmnikInverterHandler} is responsible for handling commands, which are
48  * sent to one of the channels.
49  *
50  * @author Hans van den Bogert - Initial contribution
51  */
52 @NonNullByDefault
53 public class OmnikInverterHandler extends BaseThingHandler {
54     private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
55
56     private @Nullable OmnikInverter inverter;
57     private @Nullable ScheduledFuture<?> pollJob;
58
59     public OmnikInverterHandler(Thing thing) {
60         super(thing);
61     }
62
63     @Override
64     public void handleCommand(ChannelUID channelUID, Command command) {
65         // All channels depend on data gotten from `updateData()`
66         if (command instanceof RefreshType) {
67             updateData();
68         }
69     }
70
71     @Override
72     public void initialize() {
73         OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
74
75         inverter = new OmnikInverter(config.hostname, config.port, config.serial);
76         updateStatus(ThingStatus.UNKNOWN);
77         pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
78     }
79
80     @Override
81     public void dispose() {
82         ScheduledFuture<?> pollJob = this.pollJob;
83         if (pollJob != null) {
84             pollJob.cancel(true);
85             this.pollJob = null;
86         }
87         super.dispose();
88     }
89
90     private void updateData() {
91         try {
92             if (inverter != null) {
93                 OmnikInverterMessage message = inverter.pullCurrentStats();
94
95                 updateStatus(ThingStatus.ONLINE);
96
97                 /**
98                  * AC
99                  **/
100                 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
101                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
102
103                 QuantityType<Power> powerQuantity1 = new QuantityType<>(message.getPowerAC1(), Units.WATT);
104                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC1, powerQuantity1);
105
106                 QuantityType<Power> powerQuantity2 = new QuantityType<>(message.getPowerAC2(), Units.WATT);
107                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC2, powerQuantity2);
108
109                 QuantityType<Power> powerQuantity3 = new QuantityType<>(message.getPowerAC3(), Units.WATT);
110                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC3, powerQuantity3);
111
112                 QuantityType<ElectricPotential> voltageQuantity1 = new QuantityType<>(message.getVoltageAC1(),
113                         Units.VOLT);
114                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC1, voltageQuantity1);
115
116                 QuantityType<ElectricPotential> voltageQuantity2 = new QuantityType<>(message.getVoltageAC2(),
117                         Units.VOLT);
118                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC2, voltageQuantity2);
119
120                 QuantityType<ElectricPotential> voltageQuantity3 = new QuantityType<>(message.getVoltageAC3(),
121                         Units.VOLT);
122                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_AC3, voltageQuantity3);
123
124                 QuantityType<ElectricCurrent> currentQuantity1 = new QuantityType<>(message.getCurrentAC1(),
125                         Units.AMPERE);
126                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC1, currentQuantity1);
127
128                 QuantityType<ElectricCurrent> currentQuantity2 = new QuantityType<>(message.getCurrentAC2(),
129                         Units.AMPERE);
130                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC2, currentQuantity2);
131
132                 QuantityType<ElectricCurrent> currentQuantity3 = new QuantityType<>(message.getCurrentAC3(),
133                         Units.AMPERE);
134                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_AC3, currentQuantity3);
135
136                 QuantityType<Frequency> frequencyQuantity1 = new QuantityType<>(message.getFrequencyAC1(), Units.HERTZ);
137                 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC1, frequencyQuantity1);
138
139                 QuantityType<Frequency> frequencyQuantity2 = new QuantityType<>(message.getFrequencyAC2(), Units.HERTZ);
140                 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC2, frequencyQuantity2);
141
142                 QuantityType<Frequency> frequencyQuantity3 = new QuantityType<>(message.getFrequencyAC3(), Units.HERTZ);
143                 updateState(OmnikInverterBindingConstants.CHANNEL_FREQUENCY_AC3, frequencyQuantity3);
144
145                 /**
146                  * PV
147                  **/
148
149                 QuantityType<ElectricCurrent> pvAmp1 = new QuantityType<>(message.getCurrentPV1(), Units.AMPERE);
150                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV1, pvAmp1);
151
152                 QuantityType<ElectricCurrent> pvAmp2 = new QuantityType<>(message.getCurrentPV2(), Units.AMPERE);
153                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV2, pvAmp2);
154
155                 QuantityType<ElectricCurrent> pvAmp3 = new QuantityType<>(message.getCurrentPV3(), Units.AMPERE);
156                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV3, pvAmp3);
157
158                 QuantityType<ElectricPotential> pvVoltage1 = new QuantityType<>(message.getVoltagePV1(), Units.VOLT);
159                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV1, pvVoltage1);
160
161                 QuantityType<ElectricPotential> pvVoltage2 = new QuantityType<>(message.getVoltagePV2(), Units.VOLT);
162                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV2, pvVoltage2);
163
164                 QuantityType<ElectricPotential> pvVoltage3 = new QuantityType<>(message.getVoltagePV3(), Units.VOLT);
165                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV3, pvVoltage3);
166
167                 /**
168                  * MISC
169                  **/
170                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
171                         new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
172
173                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
174                         new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
175
176                 updateState(OmnikInverterBindingConstants.CHANNEL_TEMPERATURE,
177                         new QuantityType<>(message.getTemperature(), SIUnits.CELSIUS));
178
179                 updateState(OmnikInverterBindingConstants.CHANNEL_HOURS_TOTAL,
180                         new QuantityType<>(message.getHoursTotal(), Units.HOUR));
181             }
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());
187         }
188     }
189 }