]> git.basschouten.com Git - openhab-addons.git/blob
5d3910275ed6898409c7afc04e3be5e537286f00
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 java.util.Map;
16
17 import org.openhab.binding.fronius.internal.FroniusBaseDeviceConfiguration;
18 import org.openhab.binding.fronius.internal.FroniusBindingConstants;
19 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
20 import org.openhab.binding.fronius.internal.FroniusCommunicationException;
21 import org.openhab.binding.fronius.internal.api.MeterRealtimeBodyDataDTO;
22 import org.openhab.binding.fronius.internal.api.MeterRealtimeResponseDTO;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.thing.Thing;
26
27 /**
28  * The {@link FroniusMeterHandler} is responsible for updating the data, which are
29  * sent to one of the channels.
30  *
31  * @author Jimmy Tanagra - Initial contribution
32  * @author Thomas Kordelle - Actually constants should be all upper case.
33  * @author Hannes Spenger - Added getValue for power sum
34  */
35 public class FroniusMeterHandler extends FroniusBaseThingHandler {
36
37     private MeterRealtimeBodyDataDTO meterRealtimeBodyData;
38     private FroniusBaseDeviceConfiguration config;
39
40     public FroniusMeterHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     protected String getDescription() {
46         return "Fronius Smart Meter";
47     }
48
49     @Override
50     protected void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
51         updateData(bridgeConfiguration, config);
52         updateChannels();
53         updateProperties();
54     }
55
56     @Override
57     public void initialize() {
58         config = getConfigAs(FroniusBaseDeviceConfiguration.class);
59         super.initialize();
60     }
61
62     /**
63      * Update the channel from the last data retrieved
64      *
65      * @param channelId the id identifying the channel to be updated
66      * @return the last retrieved data
67      */
68     @Override
69     protected Object getValue(String channelId) {
70         if (meterRealtimeBodyData == null) {
71             return null;
72         }
73
74         final String[] fields = channelId.split("#");
75         if (fields.length < 1) {
76             return null;
77         }
78         final String fieldName = fields[0];
79
80         switch (fieldName) {
81             case FroniusBindingConstants.METER_ENABLE:
82                 return meterRealtimeBodyData.getEnable();
83             case FroniusBindingConstants.METER_LOCATION:
84                 return meterRealtimeBodyData.getMeterLocationCurrent();
85             case FroniusBindingConstants.METER_CURRENT_AC_PHASE_1:
86                 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase1(), Units.AMPERE);
87             case FroniusBindingConstants.METER_CURRENT_AC_PHASE_2:
88                 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase2(), Units.AMPERE);
89             case FroniusBindingConstants.METER_CURRENT_AC_PHASE_3:
90                 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase3(), Units.AMPERE);
91             case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_1:
92                 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase1(), Units.VOLT);
93             case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_2:
94                 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase2(), Units.VOLT);
95             case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_3:
96                 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase3(), Units.VOLT);
97             case FroniusBindingConstants.METER_POWER_PHASE_1:
98                 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase1(), Units.WATT);
99             case FroniusBindingConstants.METER_POWER_PHASE_2:
100                 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase2(), Units.WATT);
101             case FroniusBindingConstants.METER_POWER_PHASE_3:
102                 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase3(), Units.WATT);
103             case FroniusBindingConstants.METER_POWER_SUM:
104                 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPSum(), Units.WATT);
105             case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_1:
106                 return meterRealtimeBodyData.getPowerFactorPhase1();
107             case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_2:
108                 return meterRealtimeBodyData.getPowerFactorPhase2();
109             case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_3:
110                 return meterRealtimeBodyData.getPowerFactorPhase3();
111             case FroniusBindingConstants.METER_ENERGY_REAL_SUM_CONSUMED:
112                 return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumConsumed(), Units.WATT_HOUR);
113             case FroniusBindingConstants.METER_ENERGY_REAL_SUM_PRODUCED:
114                 return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumProduced(), Units.WATT_HOUR);
115             default:
116                 break;
117         }
118
119         return null;
120     }
121
122     private void updateProperties() {
123         if (meterRealtimeBodyData == null) {
124             return;
125         }
126
127         Map<String, String> properties = editProperties();
128
129         properties.put(Thing.PROPERTY_MODEL_ID, meterRealtimeBodyData.getDetails().getModel());
130         properties.put(Thing.PROPERTY_SERIAL_NUMBER, meterRealtimeBodyData.getDetails().getSerial());
131
132         updateProperties(properties);
133     }
134
135     /**
136      * Get new data
137      */
138     private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
139             throws FroniusCommunicationException {
140         MeterRealtimeResponseDTO meterRealtimeResponse = getMeterRealtimeData(bridgeConfiguration.hostname,
141                 config.deviceId);
142         meterRealtimeBodyData = meterRealtimeResponse.getBody().getData();
143     }
144
145     /**
146      * Make the MeterRealtimeData request
147      *
148      * @param ip address of the device
149      * @param deviceId of the device
150      * @return {MeterRealtimeResponse} the object representation of the json response
151      */
152     private MeterRealtimeResponseDTO getMeterRealtimeData(String ip, int deviceId)
153             throws FroniusCommunicationException {
154         String location = FroniusBindingConstants.METER_REALTIME_DATA_URL.replace("%IP%",
155                 (ip != null ? ip.trim() : ""));
156         location = location.replace("%DEVICEID%", Integer.toString(deviceId));
157         return collectDataFromUrl(MeterRealtimeResponseDTO.class, location);
158     }
159 }