2 * Copyright (c) 2010-2022 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;
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;
28 * The {@link FroniusMeterHandler} is responsible for updating the data, which are
29 * sent to one of the channels.
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
35 public class FroniusMeterHandler extends FroniusBaseThingHandler {
37 private MeterRealtimeBodyDataDTO meterRealtimeBodyData;
38 private FroniusBaseDeviceConfiguration config;
40 public FroniusMeterHandler(Thing thing) {
45 protected String getDescription() {
46 return "Fronius Smart Meter";
50 protected void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
51 updateData(bridgeConfiguration, config);
57 public void initialize() {
58 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
63 * Update the channel from the last data retrieved
65 * @param channelId the id identifying the channel to be updated
66 * @return the last retrieved data
69 protected Object getValue(String channelId) {
70 if (meterRealtimeBodyData == null) {
74 final String[] fields = channelId.split("#");
75 if (fields.length < 1) {
78 final String fieldName = fields[0];
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);
122 private void updateProperties() {
123 if (meterRealtimeBodyData == null) {
127 Map<String, String> properties = editProperties();
129 properties.put(Thing.PROPERTY_MODEL_ID, meterRealtimeBodyData.getDetails().getModel());
130 properties.put(Thing.PROPERTY_SERIAL_NUMBER, meterRealtimeBodyData.getDetails().getSerial());
132 updateProperties(properties);
138 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
139 throws FroniusCommunicationException {
140 MeterRealtimeResponseDTO meterRealtimeResponse = getMeterRealtimeData(bridgeConfiguration.hostname,
142 meterRealtimeBodyData = meterRealtimeResponse.getBody().getData();
146 * Make the MeterRealtimeData request
148 * @param ip address of the device
149 * @param deviceId of the device
150 * @return {MeterRealtimeResponse} the object representation of the json response
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);