]> git.basschouten.com Git - openhab-addons.git/blob
969205384fa6188ee2f15fd5453b70770db4e6f5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
28
29 /**
30  * The {@link FroniusMeterHandler} is responsible for updating the data, which are
31  * sent to one of the channels.
32  *
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
36  */
37 public class FroniusMeterHandler extends FroniusBaseThingHandler {
38
39     private MeterRealtimeBodyDataDTO meterRealtimeBodyData;
40     private FroniusBaseDeviceConfiguration config;
41
42     public FroniusMeterHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     protected String getDescription() {
48         return "Fronius Smart Meter";
49     }
50
51     @Override
52     protected void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
53         updateData(bridgeConfiguration, config);
54         updateChannels();
55         updateProperties();
56     }
57
58     @Override
59     public void initialize() {
60         config = getConfigAs(FroniusBaseDeviceConfiguration.class);
61         super.initialize();
62     }
63
64     /**
65      * Update the channel from the last data retrieved
66      *
67      * @param channelId the id identifying the channel to be updated
68      * @return the last retrieved data
69      */
70     @Override
71     protected State getValue(String channelId) {
72         if (meterRealtimeBodyData == null) {
73             return null;
74         }
75
76         final String[] fields = channelId.split("#");
77         if (fields.length < 1) {
78             return null;
79         }
80         final String fieldName = fields[0];
81
82         switch (fieldName) {
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);
117             default:
118                 break;
119         }
120
121         return null;
122     }
123
124     private void updateProperties() {
125         if (meterRealtimeBodyData == null) {
126             return;
127         }
128
129         Map<String, String> properties = editProperties();
130
131         properties.put(Thing.PROPERTY_MODEL_ID, meterRealtimeBodyData.getDetails().getModel());
132         properties.put(Thing.PROPERTY_SERIAL_NUMBER, meterRealtimeBodyData.getDetails().getSerial());
133
134         updateProperties(properties);
135     }
136
137     /**
138      * Get new data
139      */
140     private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
141             throws FroniusCommunicationException {
142         MeterRealtimeResponseDTO meterRealtimeResponse = getMeterRealtimeData(bridgeConfiguration.hostname,
143                 config.deviceId);
144         meterRealtimeBodyData = meterRealtimeResponse.getBody().getData();
145     }
146
147     /**
148      * Make the MeterRealtimeData request
149      *
150      * @param ip address of the device
151      * @param deviceId of the device
152      * @return {MeterRealtimeResponse} the object representation of the json response
153      */
154     private MeterRealtimeResponseDTO getMeterRealtimeData(String ip, int deviceId)
155             throws FroniusCommunicationException {
156         String location = FroniusBindingConstants.getMeterDataUrl(ip, deviceId);
157         return collectDataFromUrl(MeterRealtimeResponseDTO.class, location);
158     }
159 }