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.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;
28 * The {@link FroniusOhmpilotHandler} is responsible for updating the data, which are
29 * sent to one of the channels.
31 * @author Hannes Spenger - Initial contribution
34 public class FroniusOhmpilotHandler extends FroniusBaseThingHandler {
36 private OhmpilotRealtimeBodyDataDTO ohmpilotRealtimeBodyData;
37 private FroniusBaseDeviceConfiguration config;
39 public FroniusOhmpilotHandler(Thing thing) {
44 protected String getDescription() {
45 return "Fronius Ohmpilot";
49 public void refresh(FroniusBridgeConfiguration bridgeConfiguration) {
50 updateData(bridgeConfiguration, config);
56 public void initialize() {
57 config = getConfigAs(FroniusBaseDeviceConfiguration.class);
62 * Update the channel from the last data retrieved
64 * @param channelId the id identifying the channel to be updated
65 * @return the last retrieved data
68 protected Object getValue(String channelId) {
69 if (ohmpilotRealtimeBodyData == null) {
73 final String[] fields = channelId.split("#");
74 if (fields.length < 1) {
77 final String fieldName = fields[0];
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());
98 private void updateProperties() {
99 if (ohmpilotRealtimeBodyData == null) {
103 Map<String, String> properties = editProperties();
105 properties.put(Thing.PROPERTY_MODEL_ID, ohmpilotRealtimeBodyData.getDetails().getModel());
106 properties.put(Thing.PROPERTY_SERIAL_NUMBER, ohmpilotRealtimeBodyData.getDetails().getSerial());
108 updateProperties(properties);
114 private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusBaseDeviceConfiguration config) {
115 OhmpilotRealtimeResponseDTO ohmpilotRealtimeResponse = getOhmpilotRealtimeData(bridgeConfiguration.hostname,
117 if (ohmpilotRealtimeResponse == null) {
118 ohmpilotRealtimeBodyData = null;
120 ohmpilotRealtimeBodyData = ohmpilotRealtimeResponse.getBody().getData();
125 * Make the OhmpilotRealtimeData request
127 * @param ip address of the device
128 * @param deviceId of the device
129 * @return {OhmpilotRealtimeResponse} the object representation of the json response
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);