]> git.basschouten.com Git - openhab-addons.git/blob
172cc90b9c1ff9cfb8920b1bc990614a87d00404
[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.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.easee.internal.Utils;
22 import org.openhab.binding.easee.internal.handler.ChannelProvider;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.types.State;
27
28 import com.google.gson.JsonObject;
29
30 /**
31  * transforms the http response into the openhab datamodel (instances of State).
32  * This class is used to handle special cases which cannot be mapped by the generic transformer.
33  *
34  * @author Alexander Friese - initial contribution
35  */
36 @NonNullByDefault
37 class CustomResponseTransformer {
38     private final ChannelProvider channelProvider;
39
40     CustomResponseTransformer(ChannelProvider channelProvider) {
41         this.channelProvider = channelProvider;
42     }
43
44     /**
45      * allows additional updates of special/composite channels.
46      *
47      * @param triggerChannel the channel which triggers the additional update
48      * @param value updated value of the triggering channel
49      * @param rawData raw json data provided by the API
50      */
51     Map<Channel, State> transform(Channel triggerChannel, String value, JsonObject rawData) {
52         Map<Channel, State> result = new HashMap<>(20);
53
54         switch (triggerChannel.getUID().getId()) {
55             case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_OP_MODE:
56                 updateChargerStartStop(result, value);
57                 break;
58             case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_DYNAMIC_CURRENT:
59                 updateChargerPauseResume(result, value);
60                 break;
61             case CHANNEL_GROUP_CIRCUIT_DYNAMIC_CURRENT + "#" + CHANNEL_CIRCUIT_DYNAMIC_CURRENT_PHASE1:
62                 updateCompositePhaseChannel(result, rawData, CHANNEL_GROUP_CIRCUIT_DYNAMIC_CURRENT,
63                         CHANNEL_CIRCUIT_DYNAMIC_CURRENTS, CHANNEL_CIRCUIT_DYNAMIC_CURRENT_PHASE1,
64                         CHANNEL_CIRCUIT_DYNAMIC_CURRENT_PHASE2, CHANNEL_CIRCUIT_DYNAMIC_CURRENT_PHASE3);
65                 break;
66             case CHANNEL_GROUP_CIRCUIT_SETTINGS + "#" + CHANNEL_CIRCUIT_MAX_CURRENT_PHASE1:
67                 updateCompositePhaseChannel(result, rawData, CHANNEL_GROUP_CIRCUIT_SETTINGS,
68                         CHANNEL_CIRCUIT_MAX_CURRENTS, CHANNEL_CIRCUIT_MAX_CURRENT_PHASE1,
69                         CHANNEL_CIRCUIT_MAX_CURRENT_PHASE2, CHANNEL_CIRCUIT_MAX_CURRENT_PHASE3);
70                 break;
71             case CHANNEL_GROUP_CIRCUIT_SETTINGS + "#" + CHANNEL_CIRCUIT_OFFLINE_MAX_CURRENT_PHASE1:
72                 updateCompositePhaseChannel(result, rawData, CHANNEL_GROUP_CIRCUIT_SETTINGS,
73                         CHANNEL_CIRCUIT_OFFLINE_MAX_CURRENTS, CHANNEL_CIRCUIT_OFFLINE_MAX_CURRENT_PHASE1,
74                         CHANNEL_CIRCUIT_OFFLINE_MAX_CURRENT_PHASE2, CHANNEL_CIRCUIT_OFFLINE_MAX_CURRENT_PHASE3);
75                 break;
76         }
77
78         return result;
79     }
80
81     private void updateChargerStartStop(Map<Channel, State> result, String value) {
82         Channel channel = channelProvider.getChannel(CHANNEL_GROUP_CHARGER_COMMANDS, CHANNEL_CHARGER_START_STOP);
83         if (channel != null) {
84             ChargerOpState state = ChargerOpState.fromCode(value);
85             result.put(channel, OnOffType.from(state.isAuthenticatedState()));
86         }
87     }
88
89     private void updateChargerPauseResume(Map<Channel, State> result, String value) {
90         Channel channel = channelProvider.getChannel(CHANNEL_GROUP_CHARGER_COMMANDS, CHANNEL_CHARGER_PAUSE_RESUME);
91         if (channel != null) {
92             double val = Double.parseDouble(value);
93             // value == 0 will mean paused
94             boolean paused = val == CHARGER_DYNAMIC_CURRENT_PAUSE;
95
96             result.put(channel, OnOffType.from(paused));
97         }
98     }
99
100     private void updateCompositePhaseChannel(Map<Channel, State> result, JsonObject rawData, final String group,
101             final String targetChannel, final String channelPhase1, final String channelPhase2,
102             final String channelPhase3) {
103         Channel channel = channelProvider.getChannel(group, targetChannel);
104         String phase1 = Utils.getAsString(rawData, channelPhase1);
105         String phase2 = Utils.getAsString(rawData, channelPhase2);
106         String phase3 = Utils.getAsString(rawData, channelPhase3);
107         if (channel != null && phase1 != null && phase2 != null && phase3 != null) {
108             phase1 = phase1.replace(".0", "");
109             phase2 = phase2.replace(".0", "");
110             phase3 = phase3.replace(".0", "");
111             result.put(channel, new StringType(phase1 + ";" + phase2 + ";" + phase3));
112         }
113     }
114 }