2 * Copyright (c) 2010-2022 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.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;
29 * The {@link FroniusOhmpilotHandler} is responsible for updating the data, which are
30 * sent to one of the channels.
32 * @author Hannes Spenger - Initial contribution
35 public class FroniusOhmpilotHandler extends FroniusBaseThingHandler {
37 private OhmpilotRealtimeBodyDataDTO ohmpilotRealtimeBodyData;
38 private FroniusBaseDeviceConfiguration config;
40 public FroniusOhmpilotHandler(Thing thing) {
45 protected String getDescription() {
46 return "Fronius Ohmpilot";
50 public void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
51 updateData(bridgeConfiguration, config);
57 public void initialize() {
58 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
63 * Update the channel from the last data retrieved
65 * @param channelId the id identifying the channel to be updated
66 * @return the last retrieved data
69 protected Object getValue(String channelId) {
70 if (ohmpilotRealtimeBodyData == null) {
74 final String[] fields = channelId.split("#");
75 if (fields.length < 1) {
78 final String fieldName = fields[0];
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());
99 private void updateProperties() {
100 if (ohmpilotRealtimeBodyData == null) {
104 Map<String, String> properties = editProperties();
106 properties.put(Thing.PROPERTY_MODEL_ID, ohmpilotRealtimeBodyData.getDetails().getModel());
107 properties.put(Thing.PROPERTY_SERIAL_NUMBER, ohmpilotRealtimeBodyData.getDetails().getSerial());
109 updateProperties(properties);
115 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
116 throws FroniusCommunicationException {
117 OhmpilotRealtimeResponseDTO ohmpilotRealtimeResponse = getOhmpilotRealtimeData(bridgeConfiguration.hostname,
119 ohmpilotRealtimeBodyData = ohmpilotRealtimeResponse.getBody().getData();
123 * Make the OhmpilotRealtimeData request
125 * @param ip address of the device
126 * @param deviceId of the device
127 * @return {OhmpilotRealtimeResponse} the object representation of the json response
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);