]> git.basschouten.com Git - openhab-addons.git/blob
fbf26aa3f778e8765b2c028ae6cb31e22b33998a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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                         String channelTypeId = Utils.getChannelTypeId(channel);
77                         switch (channelType) {
78                             case CHANNEL_TYPE_SWITCH:
79                                 result.put(channel, OnOffType.from(Boolean.parseBoolean(value)));
80                                 break;
81                             case CHANNEL_TYPE_VOLT:
82                                 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.VOLT));
83                                 break;
84                             case CHANNEL_TYPE_AMPERE:
85                                 result.put(channel, new QuantityType<>(Double.parseDouble(value), Units.AMPERE));
86                                 break;
87                             case CHANNEL_TYPE_KWH:
88                                 result.put(channel, new QuantityType<>(Double.parseDouble(value),
89                                         MetricPrefix.KILO(Units.WATT_HOUR)));
90                                 break;
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.
95                                     result.put(channel,
96                                             new QuantityType<>(Long.parseLong(value), Units.DECIBEL_MILLIWATTS));
97                                 } else {
98                                     result.put(channel, new QuantityType<>(Double.parseDouble(value),
99                                             MetricPrefix.KILO(Units.WATT)));
100                                 }
101                                 break;
102                             case CHANNEL_TYPE_DATE:
103                                 result.put(channel, new DateTimeType(Utils.parseDate(value)));
104                                 break;
105                             case CHANNEL_TYPE_STRING:
106                                 result.put(channel, new StringType(value));
107                                 break;
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)));
113                                 } else {
114                                     result.put(channel, new DecimalType(Double.parseDouble(value)));
115                                 }
116                                 break;
117                             default:
118                                 logger.warn("no mapping implemented for channel type '{}'", channelType);
119                         }
120
121                         // call the custom handler to handle specific / composite channels which do not map 1:1 to JSON
122                         // fields.
123                         result.putAll(customResponseTransformer.transform(channel, value, jsonData));
124
125                     } catch (NumberFormatException | DateTimeParseException ex) {
126                         logger.warn("caught exception while parsing data for channel {} (value '{}'). Exception: {}",
127                                 channel.getUID().getId(), value, ex.getMessage());
128                     }
129                 }
130             }
131         }
132
133         return result;
134     }
135 }