]> git.basschouten.com Git - openhab-addons.git/blob
20702752ece4b5d3f9e0a01c50c159184b49430f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Power;
25
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;
43
44 /**
45  * The {@link OmnikInverterHandler} is responsible for handling commands, which are
46  * sent to one of the channels.
47  *
48  * @author Hans van den Bogert - Initial contribution
49  */
50 @NonNullByDefault
51 public class OmnikInverterHandler extends BaseThingHandler {
52     private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
53
54     private @Nullable OmnikInverter inverter;
55     private @Nullable ScheduledFuture<?> pollJob;
56
57     public OmnikInverterHandler(Thing thing) {
58         super(thing);
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         // All channels depend on data gotten from `updateData()`
64         if (command instanceof RefreshType) {
65             updateData();
66         }
67     }
68
69     @Override
70     public void initialize() {
71         OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
72
73         inverter = new OmnikInverter(config.hostname, config.port, config.serial);
74         updateStatus(ThingStatus.UNKNOWN);
75         pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
76     }
77
78     @Override
79     public void dispose() {
80         ScheduledFuture<?> pollJob = this.pollJob;
81         if (pollJob != null) {
82             pollJob.cancel(true);
83             this.pollJob = null;
84         }
85         super.dispose();
86     }
87
88     private void updateData() {
89         try {
90             if (inverter != null) {
91                 OmnikInverterMessage message = inverter.pullCurrentStats();
92
93                 updateStatus(ThingStatus.ONLINE);
94
95                 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
96                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
97
98                 QuantityType<Power> powerQuantity1 = new QuantityType<>(message.getPowerAC1(), Units.WATT);
99                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC1, powerQuantity1);
100
101                 QuantityType<Power> powerQuantity2 = new QuantityType<>(message.getPowerAC2(), Units.WATT);
102                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC2, powerQuantity2);
103
104                 QuantityType<Power> powerQuantity3 = new QuantityType<>(message.getPowerAC3(), Units.WATT);
105                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC3, powerQuantity3);
106
107                 QuantityType<ElectricCurrent> pvAmp1 = new QuantityType<>(message.getCurrentPV1(), Units.AMPERE);
108                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV1, pvAmp1);
109
110                 QuantityType<ElectricCurrent> pvAmp2 = new QuantityType<>(message.getCurrentPV2(), Units.AMPERE);
111                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV2, pvAmp2);
112
113                 QuantityType<ElectricCurrent> pvAmp3 = new QuantityType<>(message.getCurrentPV3(), Units.AMPERE);
114                 updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV3, pvAmp3);
115
116                 QuantityType<ElectricPotential> pvVoltage1 = new QuantityType<>(message.getVoltagePV1(), Units.VOLT);
117                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV1, pvVoltage1);
118
119                 QuantityType<ElectricPotential> pvVoltage2 = new QuantityType<>(message.getVoltagePV2(), Units.VOLT);
120                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV2, pvVoltage2);
121
122                 QuantityType<ElectricPotential> pvVoltage3 = new QuantityType<>(message.getVoltagePV3(), Units.VOLT);
123                 updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV3, pvVoltage3);
124
125                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
126                         new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
127
128                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
129                         new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
130             }
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());
136         }
137     }
138 }