]> git.basschouten.com Git - openhab-addons.git/blob
d8d2a3110ba838565a3dacd3c666aafeaa02602f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.Power;
23
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;
41
42 /**
43  * The {@link OmnikInverterHandler} is responsible for handling commands, which are
44  * sent to one of the channels.
45  *
46  * @author Hans van den Bogert - Initial contribution
47  */
48 @NonNullByDefault
49 public class OmnikInverterHandler extends BaseThingHandler {
50     private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
51
52     private @Nullable OmnikInverter inverter;
53     private @Nullable ScheduledFuture<?> pollJob;
54
55     public OmnikInverterHandler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) {
62             if (command instanceof RefreshType) {
63                 updateData();
64             }
65         }
66     }
67
68     @Override
69     public void initialize() {
70         OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
71
72         inverter = new OmnikInverter(config.hostname, config.port, config.serial);
73         updateStatus(ThingStatus.UNKNOWN);
74         pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
75     }
76
77     @Override
78     public void dispose() {
79         ScheduledFuture<?> pollJob = this.pollJob;
80         if (pollJob != null) {
81             pollJob.cancel(true);
82             this.pollJob = null;
83         }
84         super.dispose();
85     }
86
87     private void updateData() {
88         try {
89             if (inverter != null) {
90                 OmnikInverterMessage message = inverter.pullCurrentStats();
91
92                 updateStatus(ThingStatus.ONLINE);
93
94                 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), Units.WATT);
95                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
96
97                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
98                         new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR));
99
100                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
101                         new QuantityType<>(message.getTotalEnergy(), Units.KILOWATT_HOUR));
102             }
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());
108         }
109     }
110 }