2 * Copyright (c) 2010-2021 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.fronius.internal.handler;
15 import org.apache.commons.lang.StringUtils;
16 import org.openhab.binding.fronius.internal.FroniusBaseDeviceConfiguration;
17 import org.openhab.binding.fronius.internal.FroniusBindingConstants;
18 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
19 import org.openhab.binding.fronius.internal.api.InverterRealtimeResponse;
20 import org.openhab.binding.fronius.internal.api.PowerFlowRealtimeResponse;
21 import org.openhab.binding.fronius.internal.api.ValueUnit;
22 import org.openhab.core.thing.Thing;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link FroniusSymoInverterHandler} is responsible for updating the data, which are
28 * sent to one of the channels.
30 * @author Thomas Rokohl - Initial contribution
31 * @author Peter Schraffl - Added device status and error status channels
33 public class FroniusSymoInverterHandler extends FroniusBaseThingHandler {
35 private final Logger logger = LoggerFactory.getLogger(FroniusSymoInverterHandler.class);
36 private InverterRealtimeResponse inverterRealtimeResponse;
37 private PowerFlowRealtimeResponse powerFlowResponse;
38 private FroniusBaseDeviceConfiguration config;
40 public FroniusSymoInverterHandler(Thing thing) {
45 protected String getDescription() {
46 return "Fronius Symo Inverter";
50 public void refresh(FroniusBridgeConfiguration bridgeConfiguration) {
51 updateData(bridgeConfiguration, config);
56 public void initialize() {
57 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
62 * Update the channel from the last data retrieved
64 * @param channelId the id identifying the channel to be updated
65 * @return the last retrieved data
68 protected Object getValue(String channelId) {
69 String[] fields = StringUtils.split(channelId, "#");
71 String fieldName = fields[0];
73 if (inverterRealtimeResponse == null) {
77 case FroniusBindingConstants.InverterDataChannelDayEnergy:
78 ValueUnit day = inverterRealtimeResponse.getBody().getData().getDayEnergy();
83 case FroniusBindingConstants.InverterDataChannelPac:
84 ValueUnit pac = inverterRealtimeResponse.getBody().getData().getPac();
86 pac = new ValueUnit();
90 case FroniusBindingConstants.InverterDataChannelTotal:
91 ValueUnit total = inverterRealtimeResponse.getBody().getData().getTotalEnergy();
96 case FroniusBindingConstants.InverterDataChannelYear:
97 ValueUnit year = inverterRealtimeResponse.getBody().getData().getYearEnergy();
102 case FroniusBindingConstants.InverterDataChannelFac:
103 return inverterRealtimeResponse.getBody().getData().getFac();
104 case FroniusBindingConstants.InverterDataChannelIac:
105 return inverterRealtimeResponse.getBody().getData().getIac();
106 case FroniusBindingConstants.InverterDataChannelIdc:
107 return inverterRealtimeResponse.getBody().getData().getIdc();
108 case FroniusBindingConstants.InverterDataChannelUac:
109 return inverterRealtimeResponse.getBody().getData().getUac();
110 case FroniusBindingConstants.InverterDataChannelUdc:
111 return inverterRealtimeResponse.getBody().getData().getUdc();
112 case FroniusBindingConstants.InverterDataChannelDeviceStatusErrorCode:
113 return inverterRealtimeResponse.getBody().getData().getDeviceStatus().getErrorCode();
114 case FroniusBindingConstants.InverterDataChannelDeviceStatusStatusCode:
115 return inverterRealtimeResponse.getBody().getData().getDeviceStatus().getStatusCode();
117 if (powerFlowResponse == null) {
121 case FroniusBindingConstants.PowerFlowpGrid:
122 return powerFlowResponse.getBody().getData().getSite().getPgrid();
123 case FroniusBindingConstants.PowerFlowpLoad:
124 return powerFlowResponse.getBody().getData().getSite().getPload();
125 case FroniusBindingConstants.PowerFlowpAkku:
126 return powerFlowResponse.getBody().getData().getSite().getPakku();
135 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config) {
136 inverterRealtimeResponse = getRealtimeData(bridgeConfiguration.hostname, config.deviceId);
137 powerFlowResponse = getPowerFlowRealtime(bridgeConfiguration.hostname);
141 * Make the PowerFlowRealtimeDataRequest
143 * @param ip address of the device
144 * @return {PowerFlowRealtimeResponse} the object representation of the json response
146 private PowerFlowRealtimeResponse getPowerFlowRealtime(String ip) {
147 String location = FroniusBindingConstants.POWERFLOW_REALTIME_DATA.replace("%IP%", StringUtils.trimToEmpty(ip));
148 return collectDataFormUrl(PowerFlowRealtimeResponse.class, location);
152 * Make the InverterRealtimeDataRequest
154 * @param ip address of the device
155 * @param deviceId of the device
156 * @return {InverterRealtimeResponse} the object representation of the json response
158 private InverterRealtimeResponse getRealtimeData(String ip, int deviceId) {
159 String location = FroniusBindingConstants.INVERTER_REALTIME_DATA_URL.replace("%IP%",
160 StringUtils.trimToEmpty(ip));
161 location = location.replace("%DEVICEID%", Integer.toString(deviceId));
162 return collectDataFormUrl(InverterRealtimeResponse.class, location);