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