]> git.basschouten.com Git - openhab-addons.git/blob
f224ace4df1b79e1acc2e517e9294ed6a1afbb04
[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, rawData);
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, JsonObject rawData) {
82         Channel channel = channelProvider.getChannel(CHANNEL_GROUP_CHARGER_COMMANDS, CHANNEL_CHARGER_START_STOP);
83         if (channel != null) {
84             int val = Integer.parseInt(value);
85             // state >= 3 will mean charging, ready to charge or charging finished
86             boolean charging = val >= CHARGER_OP_STATE_CHARGING;
87
88             String rfnc = Utils.getAsString(rawData, CHANNEL_CHARGER_REASON_FOR_NO_CURRENT);
89             int reasonForNoCurrent = Integer.valueOf(rfnc == null ? "-1" : rfnc);
90             boolean paused = false;
91             if (val == CHARGER_OP_STATE_WAITING) {
92                 switch (reasonForNoCurrent) {
93                     case CHARGER_REASON_FOR_NO_CURRENT_PAUSED:
94                     case CHARGER_REASON_FOR_NO_CURRENT_DYNAMIC_0KW:
95                         paused = true;
96                         break;
97                     default:
98                         paused = false;
99                         break;
100                 }
101             }
102             result.put(channel, OnOffType.from(charging || paused));
103         }
104     }
105
106     private void updateChargerPauseResume(Map<Channel, State> result, String value) {
107         Channel channel = channelProvider.getChannel(CHANNEL_GROUP_CHARGER_COMMANDS, CHANNEL_CHARGER_PAUSE_RESUME);
108         if (channel != null) {
109             double val = Double.parseDouble(value);
110             // value == 0 will mean paused
111             boolean paused = val == CHARGER_DYNAMIC_CURRENT_PAUSE;
112
113             result.put(channel, OnOffType.from(paused));
114         }
115     }
116
117     private void updateCompositePhaseChannel(Map<Channel, State> result, JsonObject rawData, final String group,
118             final String targetChannel, final String channelPhase1, final String channelPhase2,
119             final String channelPhase3) {
120         Channel channel = channelProvider.getChannel(group, targetChannel);
121         String phase1 = Utils.getAsString(rawData, channelPhase1);
122         String phase2 = Utils.getAsString(rawData, channelPhase2);
123         String phase3 = Utils.getAsString(rawData, channelPhase3);
124         if (channel != null && phase1 != null && phase2 != null && phase3 != null) {
125             phase1 = phase1.replaceAll("\\.0", "");
126             phase2 = phase2.replaceAll("\\.0", "");
127             phase3 = phase3.replaceAll("\\.0", "");
128             result.put(channel, new StringType(phase1 + ";" + phase2 + ";" + phase3));
129         }
130     }
131 }