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.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 switch (channelType) {
77 case CHANNEL_TYPE_SWITCH:
78 result.put(channel, OnOffType.from(Boolean.parseBoolean(value)));
80 case CHANNEL_TYPE_VOLT:
81 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.VOLT));
83 case CHANNEL_TYPE_AMPERE:
84 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.AMPERE));
86 case CHANNEL_TYPE_KWH:
87 result.put(channel, new QuantityType<>(Double.parseDouble(value),
88 MetricPrefix.KILO(Units.WATT_HOUR)));
92 new QuantityType<>(Double.parseDouble(value), MetricPrefix.KILO(Units.WATT)));
94 case CHANNEL_TYPE_DATE:
95 result.put(channel, new DateTimeType(Utils.parseDate(value)));
97 case CHANNEL_TYPE_STRING:
98 result.put(channel, new StringType(value));
100 case CHANNEL_TYPE_NUMBER:
101 if (Utils.getChannelTypeId(channel).contains(CHANNEL_TYPENAME_INTEGER)) {
102 // explicit type long is needed in case of integer/long values otherwise automatic
103 // transformation to a decimal type is applied.
104 result.put(channel, new DecimalType(Long.parseLong(value)));
106 result.put(channel, new DecimalType(Double.parseDouble(value)));
110 logger.warn("no mapping implemented for channel type '{}'", channelType);
113 // call the custom handler to handle specific / composite channels which do not map 1:1 to JSON
115 result.putAll(customResponseTransformer.transform(channel, value, jsonData));
117 } catch (NumberFormatException | DateTimeParseException ex) {
118 logger.warn("caught exception while parsing data for channel {} (value '{}'). Exception: {}",
119 channel.getUID().getId(), value, ex.getMessage());