]> git.basschouten.com Git - openhab-addons.git/blob
f8715ea5cc9e2effe3cbb301fa68232a56a4dabf
[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.OhmpilotRealtimeBodyDataDTO;
22 import org.openhab.binding.fronius.internal.api.OhmpilotRealtimeResponseDTO;
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 FroniusOhmpilotHandler} is responsible for updating the data, which are
31  * sent to one of the channels.
32  *
33  * @author Hannes Spenger - Initial contribution
34  *
35  */
36 public class FroniusOhmpilotHandler extends FroniusBaseThingHandler {
37
38     private OhmpilotRealtimeBodyDataDTO ohmpilotRealtimeBodyData;
39     private FroniusBaseDeviceConfiguration config;
40
41     public FroniusOhmpilotHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     protected String getDescription() {
47         return "Fronius Ohmpilot";
48     }
49
50     @Override
51     public void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
52         updateData(bridgeConfiguration, config);
53         updateChannels();
54         updateProperties();
55     }
56
57     @Override
58     public void initialize() {
59         config = getConfigAs(FroniusBaseDeviceConfiguration.class);
60         super.initialize();
61     }
62
63     /**
64      * Update the channel from the last data retrieved
65      *
66      * @param channelId the id identifying the channel to be updated
67      * @return the last retrieved data
68      */
69     @Override
70     protected State getValue(String channelId) {
71         if (ohmpilotRealtimeBodyData == null) {
72             return null;
73         }
74
75         final String[] fields = channelId.split("#");
76         if (fields.length < 1) {
77             return null;
78         }
79         final String fieldName = fields[0];
80
81         switch (fieldName) {
82             case FroniusBindingConstants.OHMPILOT_POWER_REAL_SUM:
83                 return new QuantityType<>(ohmpilotRealtimeBodyData.getPowerPACSum(), Units.WATT);
84             case FroniusBindingConstants.OHMPILOT_ENERGY_REAL_SUM_CONSUMED:
85                 return new QuantityType<>(ohmpilotRealtimeBodyData.getEnergyRealWACSumConsumed(), Units.WATT_HOUR);
86             case FroniusBindingConstants.OHMPILOT_ENERGY_SENSOR_TEMPERATURE_CHANNEL_1:
87                 return new QuantityType<>(ohmpilotRealtimeBodyData.getTemperatureChannel1(), Units.KELVIN);
88             case FroniusBindingConstants.OHMPILOT_STATE_CODE:
89                 return new DecimalType(ohmpilotRealtimeBodyData.getStateCode());
90             case FroniusBindingConstants.OHMPILOT_ERROR_CODE:
91                 return new DecimalType(ohmpilotRealtimeBodyData.getErrorCode());
92
93             default:
94                 break;
95         }
96
97         return null;
98     }
99
100     private void updateProperties() {
101         if (ohmpilotRealtimeBodyData == null) {
102             return;
103         }
104
105         Map<String, String> properties = editProperties();
106
107         properties.put(Thing.PROPERTY_MODEL_ID, ohmpilotRealtimeBodyData.getDetails().getModel());
108         properties.put(Thing.PROPERTY_SERIAL_NUMBER, ohmpilotRealtimeBodyData.getDetails().getSerial());
109
110         updateProperties(properties);
111     }
112
113     /**
114      * Get new data
115      */
116     private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
117             throws FroniusCommunicationException {
118         OhmpilotRealtimeResponseDTO ohmpilotRealtimeResponse = getOhmpilotRealtimeData(bridgeConfiguration.hostname,
119                 config.deviceId);
120         ohmpilotRealtimeBodyData = ohmpilotRealtimeResponse.getBody().getData();
121     }
122
123     /**
124      * Make the OhmpilotRealtimeData request
125      *
126      * @param ip address of the device
127      * @param deviceId of the device
128      * @return {OhmpilotRealtimeResponse} the object representation of the json response
129      */
130     private OhmpilotRealtimeResponseDTO getOhmpilotRealtimeData(String ip, int deviceId)
131             throws FroniusCommunicationException {
132         String location = FroniusBindingConstants.getOhmPilotDataUrl(ip, deviceId);
133         return collectDataFromUrl(OhmpilotRealtimeResponseDTO.class, location);
134     }
135 }