]> git.basschouten.com Git - openhab-addons.git/blob
d0809fa845dfac52d70e31c2a5a589287f4a3551
[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.solaredge.internal.model;
14
15 import static org.openhab.binding.solaredge.internal.SolarEdgeBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.solaredge.internal.handler.ChannelProvider;
22 import org.openhab.binding.solaredge.internal.model.AggregateDataResponsePrivateApi.UtilizationMeasures;
23 import org.openhab.binding.solaredge.internal.model.AggregateDataResponsePrivateApi.Value;
24 import org.openhab.binding.solaredge.internal.model.AggregateDataResponsePrivateApi.ValueAndPercent;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.types.State;
27
28 /**
29  * transforms the http response into the openhab datamodel (instances of State)
30  *
31  * @author Alexander Friese - initial contribution
32  */
33 @NonNullByDefault
34 public class AggregateDataResponseTransformerPrivateApi extends AbstractDataResponseTransformer {
35     private final ChannelProvider channelProvider;
36
37     public AggregateDataResponseTransformerPrivateApi(ChannelProvider channelProvider) {
38         this.channelProvider = channelProvider;
39     }
40
41     public Map<Channel, State> transform(AggregateDataResponsePrivateApi response, AggregatePeriod period) {
42         Map<Channel, State> result = new HashMap<>(20);
43         UtilizationMeasures utilizationMeasures = response.getUtilizationMeasures();
44
45         String group = convertPeriodToGroup(period);
46
47         if (utilizationMeasures != null) {
48             Value production = utilizationMeasures.production;
49             if (production != null) {
50                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_PRODUCTION), production);
51             }
52
53             Value consumption = utilizationMeasures.consumption;
54             if (consumption != null) {
55                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_CONSUMPTION), consumption);
56             }
57
58             ValueAndPercent selfConsumptionForConsumption = utilizationMeasures.selfConsumptionForConsumption;
59             if (selfConsumptionForConsumption != null) {
60                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_SELF_CONSUMPTION_FOR_CONSUMPTION),
61                         selfConsumptionForConsumption);
62                 putPercentType(result, channelProvider.getChannel(group, CHANNEL_ID_SELF_CONSUMPTION_COVERAGE),
63                         selfConsumptionForConsumption);
64             }
65
66             Value batterySelfConsumption = utilizationMeasures.batterySelfConsumption;
67             if (batterySelfConsumption != null) {
68                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_BATTERY_SELF_CONSUMPTION),
69                         batterySelfConsumption);
70             }
71
72             Value imported = utilizationMeasures.imported;
73             if (imported != null) {
74                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_IMPORT), imported);
75             }
76
77             Value export = utilizationMeasures.export;
78             if (export != null) {
79                 putEnergyType(result, channelProvider.getChannel(group, CHANNEL_ID_EXPORT), export);
80             }
81         }
82         return result;
83     }
84 }