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