2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.easee.internal.model;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
17 import java.util.HashMap;
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;
28 import com.google.gson.JsonObject;
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.
34 * @author Alexander Friese - initial contribution
37 class CustomResponseTransformer {
38 private final ChannelProvider channelProvider;
40 CustomResponseTransformer(ChannelProvider channelProvider) {
41 this.channelProvider = channelProvider;
45 * allows additional updates of special/composite channels.
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
51 Map<Channel, State> transform(Channel triggerChannel, String value, JsonObject rawData) {
52 Map<Channel, State> result = new HashMap<>(20);
54 switch (triggerChannel.getUID().getId()) {
55 case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_OP_MODE:
56 updateChargerStartStop(result, value, rawData);
58 case CHANNEL_GROUP_CHARGER_STATE + "#" + CHANNEL_CHARGER_DYNAMIC_CURRENT:
59 updateChargerPauseResume(result, value);
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);
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);
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);
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 && state < 7 will mean charging, ready to charge or charging finished
86 boolean charging = val >= CHARGER_OP_STATE_CHARGING && val < CHARGER_OP_STATE_NOT_AUTHENTICATED;
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_CHARGER_LIMIT:
94 case CHARGER_REASON_FOR_NO_CURRENT_CIRCUIT_LIMIT:
102 result.put(channel, OnOffType.from(charging || paused));
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;
113 result.put(channel, OnOffType.from(paused));
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.replace(".0", "");
126 phase2 = phase2.replace(".0", "");
127 phase3 = phase3.replace(".0", "");
128 result.put(channel, new StringType(phase1 + ";" + phase2 + ";" + phase3));