]> git.basschouten.com Git - openhab-addons.git/blob
02a86f438bb38ea00455106688b2e14888446fad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.easee.internal.model;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
16
17 import java.time.format.DateTimeParseException;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.measure.MetricPrefix;
22
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;
37
38 import com.google.gson.JsonObject;
39
40 /**
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.
43  *
44  * @author Alexander Friese - initial contribution
45  */
46 @NonNullByDefault
47 public class GenericResponseTransformer {
48     private final Logger logger = LoggerFactory.getLogger(GenericResponseTransformer.class);
49     private final ChannelProvider channelProvider;
50     private final CustomResponseTransformer customResponseTransformer;
51
52     public GenericResponseTransformer(ChannelProvider channelProvider) {
53         this.channelProvider = channelProvider;
54         this.customResponseTransformer = new CustomResponseTransformer(channelProvider);
55     }
56
57     public Map<Channel, State> transform(JsonObject jsonData, String group) {
58         Map<Channel, State> result = new HashMap<>(20);
59
60         for (String channelId : jsonData.keySet()) {
61             String value = Utils.getAsString(jsonData, channelId);
62
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);
68             } else {
69                 logger.debug("mapping value '{}' to channel {}", value, channel.getUID().getId());
70                 String channelType = channel.getAcceptedItemType();
71
72                 if (value == null || channelType == null) {
73                     result.put(channel, UnDefType.NULL);
74                 } else {
75                     try {
76                         switch (channelType) {
77                             case CHANNEL_TYPE_SWITCH:
78                                 result.put(channel, OnOffType.from(Boolean.parseBoolean(value)));
79                                 break;
80                             case CHANNEL_TYPE_VOLT:
81                                 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.VOLT));
82                                 break;
83                             case CHANNEL_TYPE_AMPERE:
84                                 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.AMPERE));
85                                 break;
86                             case CHANNEL_TYPE_KWH:
87                                 result.put(channel, new QuantityType<>(Double.parseDouble(value),
88                                         MetricPrefix.KILO(Units.WATT_HOUR)));
89                                 break;
90                             case CHANNEL_TYPE_KW:
91                                 result.put(channel,
92                                         new QuantityType<>(Double.parseDouble(value), MetricPrefix.KILO(Units.WATT)));
93                                 break;
94                             case CHANNEL_TYPE_DATE:
95                                 result.put(channel, new DateTimeType(Utils.parseDate(value)));
96                                 break;
97                             case CHANNEL_TYPE_STRING:
98                                 result.put(channel, new StringType(value));
99                                 break;
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)));
105                                 } else {
106                                     result.put(channel, new DecimalType(Double.parseDouble(value)));
107                                 }
108                                 break;
109                             default:
110                                 logger.warn("no mapping implemented for channel type '{}'", channelType);
111                         }
112
113                         // call the custom handler to handle specific / composite channels which do not map 1:1 to JSON
114                         // fields.
115                         result.putAll(customResponseTransformer.transform(channel, value, jsonData));
116
117                     } catch (NumberFormatException | DateTimeParseException ex) {
118                         logger.warn("caught exception while parsing data for channel {} (value '{}'). Exception: {}",
119                                 channel.getUID().getId(), value, ex.getMessage());
120                     }
121                 }
122             }
123         }
124
125         return result;
126     }
127 }