]> git.basschouten.com Git - openhab-addons.git/blob
051b07775a1f382ced56c279a0aa86ba9f65c25e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.TimeUnit;
20
21 import javax.measure.quantity.Power;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.omnikinverter.internal.OmnikInverter;
26 import org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants;
27 import org.openhab.binding.omnikinverter.internal.OmnikInverterConfiguration;
28 import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.unit.SmartHomeUnits;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.BaseThingHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link OmnikInverterHandler} is responsible for handling commands, which are
43  * sent to one of the channels.
44  *
45  * @author Hans van den Bogert - Initial contribution
46  */
47 @NonNullByDefault
48 public class OmnikInverterHandler extends BaseThingHandler {
49     private @Nullable OmnikInverter inverter;
50
51     private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
52
53     public OmnikInverterHandler(Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     public void handleCommand(ChannelUID channelUID, Command command) {
59         if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) {
60             if (command instanceof RefreshType) {
61                 updateData();
62             }
63         }
64     }
65
66     @Override
67     public void initialize() {
68         OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
69
70         try {
71             inverter = new OmnikInverter(config.hostname, config.port, config.serial);
72             scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
73         } catch (IOException e) {
74             logger.debug("Could not instantiate OmnikInverter object: {}", e.getMessage());
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
76                     "Failed to initialize: " + e.getMessage());
77         }
78     }
79
80     private void updateData() {
81         try {
82             if (inverter != null) {
83                 OmnikInverterMessage message = inverter.pullCurrentStats();
84
85                 updateStatus(ThingStatus.ONLINE);
86
87                 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), SmartHomeUnits.WATT);
88                 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
89
90                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
91                         new QuantityType<>(message.getEnergyToday(), SmartHomeUnits.KILOWATT_HOUR));
92
93                 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
94                         new QuantityType<>(message.getTotalEnergy(), SmartHomeUnits.KILOWATT_HOUR));
95             }
96         } catch (UnknownHostException | NoRouteToHostException | ConnectException e) {
97             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
98         } catch (IOException e) {
99             logger.debug("Unknown exception when pulling data from the inverter: {}", e.getMessage());
100             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Unknown error: " + e.getMessage());
101         }
102     }
103 }