]> git.basschouten.com Git - openhab-addons.git/blob
ff8592f8d20c2baa8fd0b020cc3fd83584fbc933
[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.fronius.internal.handler;
14
15 import org.apache.commons.lang3.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;
25
26 /**
27  * The {@link FroniusSymoInverterHandler} is responsible for updating the data, which are
28  * sent to one of the channels.
29  *
30  * @author Thomas Rokohl - Initial contribution
31  * @author Peter Schraffl - Added device status and error status channels
32  */
33 public class FroniusSymoInverterHandler extends FroniusBaseThingHandler {
34
35     private final Logger logger = LoggerFactory.getLogger(FroniusSymoInverterHandler.class);
36     private InverterRealtimeResponse inverterRealtimeResponse;
37     private PowerFlowRealtimeResponse powerFlowResponse;
38     private FroniusBaseDeviceConfiguration config;
39
40     public FroniusSymoInverterHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     protected String getDescription() {
46         return "Fronius Symo Inverter";
47     }
48
49     @Override
50     public void refresh(FroniusBridgeConfiguration bridgeConfiguration) {
51         updateData(bridgeConfiguration, config);
52         updateChannels();
53     }
54
55     @Override
56     public void initialize() {
57         config = getConfigAs(FroniusBaseDeviceConfiguration.class);
58         super.initialize();
59     }
60
61     /**
62      * Update the channel from the last data retrieved
63      *
64      * @param channelId the id identifying the channel to be updated
65      * @return the last retrieved data
66      */
67     @Override
68     protected Object getValue(String channelId) {
69         String[] fields = StringUtils.split(channelId, "#");
70
71         String fieldName = fields[0];
72
73         if (inverterRealtimeResponse == null) {
74             return null;
75         }
76         switch (fieldName) {
77             case FroniusBindingConstants.InverterDataChannelDayEnergy:
78                 ValueUnit day = inverterRealtimeResponse.getBody().getData().getDayEnergy();
79                 if (day != null) {
80                     day.setUnit("kWh");
81                 }
82                 return day;
83             case FroniusBindingConstants.InverterDataChannelPac:
84                 ValueUnit pac = inverterRealtimeResponse.getBody().getData().getPac();
85                 if (pac == null) {
86                     pac = new ValueUnit();
87                     pac.setValue(0);
88                 }
89                 return pac;
90             case FroniusBindingConstants.InverterDataChannelTotal:
91                 ValueUnit total = inverterRealtimeResponse.getBody().getData().getTotalEnergy();
92                 if (total != null) {
93                     total.setUnit("MWh");
94                 }
95                 return total;
96             case FroniusBindingConstants.InverterDataChannelYear:
97                 ValueUnit year = inverterRealtimeResponse.getBody().getData().getYearEnergy();
98                 if (year != null) {
99                     year.setUnit("MWh");
100                 }
101                 return year;
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();
116         }
117         if (powerFlowResponse == null) {
118             return null;
119         }
120         switch (fieldName) {
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();
127         }
128
129         return null;
130     }
131
132     /**
133      * Get new data
134      */
135     private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config) {
136         inverterRealtimeResponse = getRealtimeData(bridgeConfiguration.hostname, config.deviceId);
137         powerFlowResponse = getPowerFlowRealtime(bridgeConfiguration.hostname);
138     }
139
140     /**
141      * Make the PowerFlowRealtimeDataRequest
142      *
143      * @param ip address of the device
144      * @return {PowerFlowRealtimeResponse} the object representation of the json response
145      */
146     private PowerFlowRealtimeResponse getPowerFlowRealtime(String ip) {
147         String location = FroniusBindingConstants.POWERFLOW_REALTIME_DATA.replace("%IP%", StringUtils.trimToEmpty(ip));
148         return collectDataFormUrl(PowerFlowRealtimeResponse.class, location);
149     }
150
151     /**
152      * Make the InverterRealtimeDataRequest
153      *
154      * @param ip address of the device
155      * @param deviceId of the device
156      * @return {InverterRealtimeResponse} the object representation of the json response
157      */
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);
163     }
164 }