2 * Copyright (c) 2010-2024 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.easee.internal.model;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
17 import java.time.format.DateTimeParseException;
18 import java.util.HashMap;
21 import javax.measure.MetricPrefix;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.easee.internal.Utils;
25 import org.openhab.binding.easee.internal.handler.ChannelProvider;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Channel;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonObject;
41 * transforms the http response into the openhab datamodel (instances of State)
42 * this is a generic trnasformer which tries to map json fields 1:1 to channels.
44 * @author Alexander Friese - initial contribution
47 public class GenericResponseTransformer {
48 private final Logger logger = LoggerFactory.getLogger(GenericResponseTransformer.class);
49 private final ChannelProvider channelProvider;
50 private final CustomResponseTransformer customResponseTransformer;
52 public GenericResponseTransformer(ChannelProvider channelProvider) {
53 this.channelProvider = channelProvider;
54 this.customResponseTransformer = new CustomResponseTransformer(channelProvider);
57 public Map<Channel, State> transform(JsonObject jsonData, String group) {
58 Map<Channel, State> result = new HashMap<>(20);
60 for (String channelId : jsonData.keySet()) {
61 String value = Utils.getAsString(jsonData, channelId);
63 Channel channel = channelProvider.getChannel(group, channelId);
64 if (channel == null) {
65 // As we have a generic response mapper it ould happen that a subset of key/values in the response
66 // cannot be mapped to openhab channels.
67 logger.debug("Channel not found: {}#{}", group, channelId);
69 logger.debug("mapping value '{}' to channel {}", value, channel.getUID().getId());
70 String channelType = channel.getAcceptedItemType();
72 if (value == null || channelType == null) {
73 result.put(channel, UnDefType.NULL);
76 String channelTypeId = Utils.getChannelTypeId(channel);
77 switch (channelType) {
78 case CHANNEL_TYPE_SWITCH:
79 result.put(channel, OnOffType.from(Boolean.parseBoolean(value)));
81 case CHANNEL_TYPE_VOLT:
82 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.VOLT));
84 case CHANNEL_TYPE_AMPERE:
85 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.AMPERE));
87 case CHANNEL_TYPE_KWH:
88 result.put(channel, new QuantityType<>(Double.parseDouble(value),
89 MetricPrefix.KILO(Units.WATT_HOUR)));
91 case CHANNEL_TYPE_POWER:
92 if (channelTypeId.equals(CHANNEL_TYPENAME_RSSI)) {
93 // explicit type long is needed in case of integer/long values otherwise automatic
94 // transformation to a decimal type is applied.
96 new QuantityType<>(Long.parseLong(value), Units.DECIBEL_MILLIWATTS));
98 result.put(channel, new QuantityType<>(Double.parseDouble(value),
99 MetricPrefix.KILO(Units.WATT)));
102 case CHANNEL_TYPE_DATE:
103 result.put(channel, new DateTimeType(Utils.parseDate(value)));
105 case CHANNEL_TYPE_STRING:
106 result.put(channel, new StringType(value));
108 case CHANNEL_TYPE_NUMBER:
109 if (channelTypeId.contains(CHANNEL_TYPENAME_INTEGER)) {
110 // explicit type long is needed in case of integer/long values otherwise automatic
111 // transformation to a decimal type is applied.
112 result.put(channel, new DecimalType(Long.parseLong(value)));
114 result.put(channel, new DecimalType(Double.parseDouble(value)));
118 logger.warn("no mapping implemented for channel type '{}'", channelType);
121 // call the custom handler to handle specific / composite channels which do not map 1:1 to JSON
123 result.putAll(customResponseTransformer.transform(channel, value, jsonData));
125 } catch (NumberFormatException | DateTimeParseException ex) {
126 logger.warn("caught exception while parsing data for channel {} (value '{}'). Exception: {}",
127 channel.getUID().getId(), value, ex.getMessage());