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.fronius.internal.handler;
15 import java.io.IOException;
17 import org.apache.commons.lang.StringUtils;
18 import org.openhab.binding.fronius.internal.FroniusBaseDeviceConfiguration;
19 import org.openhab.binding.fronius.internal.FroniusBindingConstants;
20 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
21 import org.openhab.binding.fronius.internal.api.BaseFroniusResponse;
22 import org.openhab.binding.fronius.internal.api.InverterRealtimeResponse;
23 import org.openhab.binding.fronius.internal.api.PowerFlowRealtimeResponse;
24 import org.openhab.binding.fronius.internal.api.ValueUnit;
25 import org.openhab.core.io.net.http.HttpUtil;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
33 import com.google.gson.JsonSyntaxException;
36 * The {@link FroniusSymoInverterHandler} is responsible for updating the data, which are
37 * sent to one of the channels.
39 * @author Thomas Rokohl - Initial contribution
40 * @author Peter Schraffl - Added device status and error status channels
42 public class FroniusSymoInverterHandler extends FroniusBaseThingHandler {
44 private static final int API_TIMEOUT = 5000;
45 private final Logger logger = LoggerFactory.getLogger(FroniusSymoInverterHandler.class);
46 private InverterRealtimeResponse inverterRealtimeResponse;
47 private PowerFlowRealtimeResponse powerFlowResponse;
48 private FroniusBaseDeviceConfiguration config;
49 private final Gson gson;
51 public FroniusSymoInverterHandler(Thing thing) {
57 protected String getDescription() {
58 return "Fronius Symo Inverter";
62 public void refresh(FroniusBridgeConfiguration bridgeConfiguration) {
63 updateData(bridgeConfiguration, config);
68 public void initialize() {
69 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
74 * Update the channel from the last data retrieved
76 * @param channelId the id identifying the channel to be updated
77 * @return the last retrieved data
80 protected Object getValue(String channelId) {
81 String[] fields = StringUtils.split(channelId, "#");
83 String fieldName = fields[0];
85 if (inverterRealtimeResponse == null) {
89 case FroniusBindingConstants.InverterDataChannelDayEnergy:
90 ValueUnit day = inverterRealtimeResponse.getBody().getData().getDayEnergy();
95 case FroniusBindingConstants.InverterDataChannelPac:
96 return inverterRealtimeResponse.getBody().getData().getPac();
97 case FroniusBindingConstants.InverterDataChannelTotal:
98 ValueUnit total = inverterRealtimeResponse.getBody().getData().getTotalEnergy();
100 total.setUnit("MWh");
103 case FroniusBindingConstants.InverterDataChannelYear:
104 ValueUnit year = inverterRealtimeResponse.getBody().getData().getYearEnergy();
109 case FroniusBindingConstants.InverterDataChannelFac:
110 return inverterRealtimeResponse.getBody().getData().getFac();
111 case FroniusBindingConstants.InverterDataChannelIac:
112 return inverterRealtimeResponse.getBody().getData().getIac();
113 case FroniusBindingConstants.InverterDataChannelIdc:
114 return inverterRealtimeResponse.getBody().getData().getIdc();
115 case FroniusBindingConstants.InverterDataChannelUac:
116 return inverterRealtimeResponse.getBody().getData().getUac();
117 case FroniusBindingConstants.InverterDataChannelUdc:
118 return inverterRealtimeResponse.getBody().getData().getUdc();
119 case FroniusBindingConstants.InverterDataChannelDeviceStatusErrorCode:
120 return inverterRealtimeResponse.getBody().getData().getDeviceStatus().getErrorCode();
121 case FroniusBindingConstants.InverterDataChannelDeviceStatusStatusCode:
122 return inverterRealtimeResponse.getBody().getData().getDeviceStatus().getStatusCode();
124 if (powerFlowResponse == null) {
128 case FroniusBindingConstants.PowerFlowpGrid:
129 return powerFlowResponse.getBody().getData().getSite().getPgrid();
130 case FroniusBindingConstants.PowerFlowpLoad:
131 return powerFlowResponse.getBody().getData().getSite().getPload();
132 case FroniusBindingConstants.PowerFlowpAkku:
133 return powerFlowResponse.getBody().getData().getSite().getPakku();
142 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config) {
143 inverterRealtimeResponse = getRealtimeData(bridgeConfiguration.hostname, config.deviceId);
144 powerFlowResponse = getPowerFlowRealtime(bridgeConfiguration.hostname);
149 * @param type response class type
150 * @param url to request
151 * @return the object representation of the json response
153 private <T extends BaseFroniusResponse> T collectDataFormUrl(Class<T> type, String url) {
155 boolean resultOk = false;
156 String errorMsg = null;
159 logger.debug("URL = {}", url);
160 String response = HttpUtil.executeUrl("GET", url, API_TIMEOUT);
162 if (response != null) {
163 logger.debug("aqiResponse = {}", response);
164 result = gson.fromJson(response, type);
167 if (result == null) {
168 errorMsg = "no data returned";
170 if (result.getHead().getStatus().getCode() == 0) {
173 errorMsg = result.getHead().getStatus().getReason();
177 logger.debug("Error in fronius response: {}", errorMsg);
179 } catch (JsonSyntaxException e) {
180 errorMsg = "Configuration is incorrect";
181 logger.debug("Error running fronius request: {}", errorMsg);
182 } catch (IOException | IllegalStateException e) {
183 logger.debug("Error running fronius request: {}", e.getMessage());
186 // Update the thing status
188 updateStatus(ThingStatus.ONLINE);
190 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, errorMsg);
192 return resultOk ? result : null;
196 * Make the PowerFlowRealtimeDataRequest
198 * @param ip address of the device
199 * @return {PowerFlowRealtimeResponse} the object representation of the json response
201 private PowerFlowRealtimeResponse getPowerFlowRealtime(String ip) {
202 String location = FroniusBindingConstants.POWERFLOW_REALTIME_DATA.replace("%IP%", StringUtils.trimToEmpty(ip));
203 return collectDataFormUrl(PowerFlowRealtimeResponse.class, location);
207 * Make the InverterRealtimeDataRequest
209 * @param ip address of the device
210 * @param deviceId of the device
211 * @return {InverterRealtimeResponse} the object representation of the json response
213 private InverterRealtimeResponse getRealtimeData(String ip, int deviceId) {
214 String location = FroniusBindingConstants.INVERTER_REALTIME_DATA_URL.replace("%IP%",
215 StringUtils.trimToEmpty(ip));
216 location = location.replace("%DEVICEID%", Integer.toString(deviceId));
217 return collectDataFormUrl(InverterRealtimeResponse.class, location);