2 * Copyright (c) 2010-2023 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;
27 import org.openhab.core.types.State;
30 * The {@link FroniusOhmpilotHandler} is responsible for updating the data, which are
31 * sent to one of the channels.
33 * @author Hannes Spenger - Initial contribution
36 public class FroniusOhmpilotHandler extends FroniusBaseThingHandler {
38 private OhmpilotRealtimeBodyDataDTO ohmpilotRealtimeBodyData;
39 private FroniusBaseDeviceConfiguration config;
41 public FroniusOhmpilotHandler(Thing thing) {
46 protected String getDescription() {
47 return "Fronius Ohmpilot";
51 public void handleRefresh(FroniusBridgeConfiguration bridgeConfiguration) throws FroniusCommunicationException {
52 updateData(bridgeConfiguration, config);
58 public void initialize() {
59 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
64 * Update the channel from the last data retrieved
66 * @param channelId the id identifying the channel to be updated
67 * @return the last retrieved data
70 protected State getValue(String channelId) {
71 if (ohmpilotRealtimeBodyData == null) {
75 final String[] fields = channelId.split("#");
76 if (fields.length < 1) {
79 final String fieldName = fields[0];
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());
100 private void updateProperties() {
101 if (ohmpilotRealtimeBodyData == null) {
105 Map<String, String> properties = editProperties();
107 properties.put(Thing.PROPERTY_MODEL_ID, ohmpilotRealtimeBodyData.getDetails().getModel());
108 properties.put(Thing.PROPERTY_SERIAL_NUMBER, ohmpilotRealtimeBodyData.getDetails().getSerial());
110 updateProperties(properties);
116 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config)
117 throws FroniusCommunicationException {
118 OhmpilotRealtimeResponseDTO ohmpilotRealtimeResponse = getOhmpilotRealtimeData(bridgeConfiguration.hostname,
120 ohmpilotRealtimeBodyData = ohmpilotRealtimeResponse.getBody().getData();
124 * Make the OhmpilotRealtimeData request
126 * @param ip address of the device
127 * @param deviceId of the device
128 * @return {OhmpilotRealtimeResponse} the object representation of the json response
130 private OhmpilotRealtimeResponseDTO getOhmpilotRealtimeData(String ip, int deviceId)
131 throws FroniusCommunicationException {
132 String location = FroniusBindingConstants.getOhmPilotDataUrl(ip, deviceId);
133 return collectDataFromUrl(OhmpilotRealtimeResponseDTO.class, location);