2 * Copyright (c) 2010-2024 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.DecimalType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.State;
30 * The {@link FroniusMeterHandler} is responsible for updating the data, which are
31 * sent to one of the channels.
33 * @author Jimmy Tanagra - Initial contribution
34 * @author Thomas Kordelle - Actually constants should be all upper case.
35 * @author Hannes Spenger - Added getValue for power sum
37 public class FroniusMeterHandler extends FroniusBaseThingHandler {
39 private MeterRealtimeBodyDataDTO meterRealtimeBodyData;
40 private FroniusBaseDeviceConfiguration config;
42 public FroniusMeterHandler(Thing thing) {
47 protected String getDescription() {
48 return "Fronius Smart Meter";
52 protected void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
53 updateData(bridgeConfiguration, config);
59 public void initialize() {
60 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
65 * Update the channel from the last data retrieved
67 * @param channelId the id identifying the channel to be updated
68 * @return the last retrieved data
71 protected State getValue(String channelId) {
72 if (meterRealtimeBodyData == null) {
76 final String[] fields = channelId.split("#");
77 if (fields.length < 1) {
80 final String fieldName = fields[0];
83 case FroniusBindingConstants.METER_ENABLE:
84 return new DecimalType(meterRealtimeBodyData.getEnable());
85 case FroniusBindingConstants.METER_LOCATION:
86 return new DecimalType(meterRealtimeBodyData.getMeterLocationCurrent());
87 case FroniusBindingConstants.METER_CURRENT_AC_PHASE_1:
88 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase1(), Units.AMPERE);
89 case FroniusBindingConstants.METER_CURRENT_AC_PHASE_2:
90 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase2(), Units.AMPERE);
91 case FroniusBindingConstants.METER_CURRENT_AC_PHASE_3:
92 return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase3(), Units.AMPERE);
93 case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_1:
94 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase1(), Units.VOLT);
95 case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_2:
96 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase2(), Units.VOLT);
97 case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_3:
98 return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase3(), Units.VOLT);
99 case FroniusBindingConstants.METER_POWER_PHASE_1:
100 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase1(), Units.WATT);
101 case FroniusBindingConstants.METER_POWER_PHASE_2:
102 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase2(), Units.WATT);
103 case FroniusBindingConstants.METER_POWER_PHASE_3:
104 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase3(), Units.WATT);
105 case FroniusBindingConstants.METER_POWER_SUM:
106 return new QuantityType<>(meterRealtimeBodyData.getPowerRealPSum(), Units.WATT);
107 case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_1:
108 return new DecimalType(meterRealtimeBodyData.getPowerFactorPhase1());
109 case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_2:
110 return new DecimalType(meterRealtimeBodyData.getPowerFactorPhase2());
111 case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_3:
112 return new DecimalType(meterRealtimeBodyData.getPowerFactorPhase3());
113 case FroniusBindingConstants.METER_ENERGY_REAL_SUM_CONSUMED:
114 return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumConsumed(), Units.WATT_HOUR);
115 case FroniusBindingConstants.METER_ENERGY_REAL_SUM_PRODUCED:
116 return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumProduced(), Units.WATT_HOUR);
124 private void updateProperties() {
125 if (meterRealtimeBodyData == null) {
129 Map<String, String> properties = editProperties();
131 properties.put(Thing.PROPERTY_MODEL_ID, meterRealtimeBodyData.getDetails().getModel());
132 properties.put(Thing.PROPERTY_SERIAL_NUMBER, meterRealtimeBodyData.getDetails().getSerial());
134 updateProperties(properties);
140 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
141 throws FroniusCommunicationException {
142 MeterRealtimeResponseDTO meterRealtimeResponse = getMeterRealtimeData(bridgeConfiguration.hostname,
144 meterRealtimeBodyData = meterRealtimeResponse.getBody().getData();
148 * Make the MeterRealtimeData request
150 * @param ip address of the device
151 * @param deviceId of the device
152 * @return {MeterRealtimeResponse} the object representation of the json response
154 private MeterRealtimeResponseDTO getMeterRealtimeData(String ip, int deviceId)
155 throws FroniusCommunicationException {
156 String location = FroniusBindingConstants.getMeterDataUrl(ip, deviceId);
157 return collectDataFromUrl(MeterRealtimeResponseDTO.class, location);