2 * Copyright (c) 2010-2020 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.TimeUnit;
21 import javax.measure.quantity.Power;
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;
42 * The {@link OmnikInverterHandler} is responsible for handling commands, which are
43 * sent to one of the channels.
45 * @author Hans van den Bogert - Initial contribution
48 public class OmnikInverterHandler extends BaseThingHandler {
49 private @Nullable OmnikInverter inverter;
51 private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
53 public OmnikInverterHandler(Thing thing) {
58 public void handleCommand(ChannelUID channelUID, Command command) {
59 if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) {
60 if (command instanceof RefreshType) {
67 public void initialize() {
68 OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
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());
80 private void updateData() {
82 if (inverter != null) {
83 OmnikInverterMessage message = inverter.pullCurrentStats();
85 updateStatus(ThingStatus.ONLINE);
87 QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), SmartHomeUnits.WATT);
88 updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
90 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
91 new QuantityType<>(message.getEnergyToday(), SmartHomeUnits.KILOWATT_HOUR));
93 updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
94 new QuantityType<>(message.getTotalEnergy(), SmartHomeUnits.KILOWATT_HOUR));
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());