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.handler;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicReference;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.easee.internal.AtomicReferenceTrait;
25 import org.openhab.binding.easee.internal.EaseeBindingConstants;
26 import org.openhab.binding.easee.internal.Utils;
27 import org.openhab.binding.easee.internal.command.EaseeCommand;
28 import org.openhab.binding.easee.internal.command.charger.ChangeConfiguration;
29 import org.openhab.binding.easee.internal.command.charger.Charger;
30 import org.openhab.binding.easee.internal.command.charger.ChargerState;
31 import org.openhab.binding.easee.internal.command.charger.GetConfiguration;
32 import org.openhab.binding.easee.internal.command.charger.LatestChargingSession;
33 import org.openhab.binding.easee.internal.command.charger.SendCommand;
34 import org.openhab.binding.easee.internal.command.charger.SendCommandStartStop;
35 import org.openhab.binding.easee.internal.config.EaseeConfiguration;
36 import org.openhab.binding.easee.internal.connector.CommunicationStatus;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.Channel;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.binding.BaseThingHandler;
43 import org.openhab.core.types.Command;
44 import org.openhab.core.types.State;
45 import org.openhab.core.types.UnDefType;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import com.google.gson.JsonObject;
52 * The {@link EaseeChargerHandler} is responsible for handling commands, which are
53 * sent to one of the channels.
55 * @author Alexander Friese - initial contribution
58 public class EaseeChargerHandler extends BaseThingHandler implements EaseeThingHandler, AtomicReferenceTrait {
59 private final Logger logger = LoggerFactory.getLogger(EaseeChargerHandler.class);
62 * Schedule for polling live data
64 private final AtomicReference<@Nullable Future<?>> dataPollingJobReference;
66 public EaseeChargerHandler(Thing thing) {
68 this.dataPollingJobReference = new AtomicReference<>(null);
72 public void initialize() {
73 logger.debug("About to initialize Charger");
74 String chargerId = getConfig().get(EaseeBindingConstants.THING_CONFIG_ID).toString();
75 logger.debug("Easee Charger initialized with id: {}", chargerId);
77 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, STATUS_WAITING_FOR_BRIDGE);
80 enqueueCommand(new Charger(this, chargerId, this::updateProperties));
83 private void updateProperties(CommunicationStatus status, JsonObject charger) {
84 Map<String, String> properties = editProperties();
86 String backPlateId = Utils.getAsString(charger.getAsJsonObject(JSON_KEY_BACK_PLATE), JSON_KEY_GENERIC_ID);
87 String masterBackPlateId = Utils.getAsString(charger.getAsJsonObject(JSON_KEY_BACK_PLATE),
88 JSON_KEY_MASTER_BACK_PLATE_ID);
89 if (backPlateId != null && masterBackPlateId != null) {
90 if (backPlateId.equals(masterBackPlateId)) {
91 properties.put(THING_CONFIG_IS_MASTER, GENERIC_YES);
93 properties.put(THING_CONFIG_IS_MASTER, GENERIC_NO);
95 properties.put(THING_CONFIG_BACK_PLATE_ID, backPlateId);
96 properties.put(THING_CONFIG_MASTER_BACK_PLATE_ID, masterBackPlateId);
98 String chargerName = Utils.getAsString(charger, JSON_KEY_GENERIC_NAME);
99 if (chargerName != null) {
100 properties.put(JSON_KEY_GENERIC_NAME, chargerName);
102 String circuitId = Utils.getAsString(charger.getAsJsonObject(JSON_KEY_BACK_PLATE), JSON_KEY_CIRCUIT_ID);
103 if (circuitId != null) {
104 properties.put(JSON_KEY_CIRCUIT_ID, circuitId);
107 updateProperties(properties);
113 private void startPolling() {
114 updateJobReference(dataPollingJobReference, scheduler.scheduleWithFixedDelay(this::pollingRun,
115 POLLING_INITIAL_DELAY, getBridgeConfiguration().getDataPollingInterval(), TimeUnit.SECONDS));
119 * Poll the Easee Cloud API one time.
122 String chargerId = getConfig().get(EaseeBindingConstants.THING_CONFIG_ID).toString();
123 logger.debug("polling charger data for {}", chargerId);
125 ChargerState state = new ChargerState(this, chargerId);
126 state.registerResultProcessor(this::updateStatusInfo);
127 enqueueCommand(state);
129 // proceed if charger is online
130 if (getThing().getStatus() == ThingStatus.ONLINE) {
131 enqueueCommand(new GetConfiguration(this, chargerId));
132 enqueueCommand(new LatestChargingSession(this, chargerId));
137 * updates status depending on online information received from the API.
142 private void updateStatusInfo(CommunicationStatus status, JsonObject jsonObject) {
143 Boolean isOnline = Utils.getAsBool(jsonObject, JSON_KEY_ONLINE);
145 if (isOnline == null) {
146 super.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, STATUS_NO_VALID_DATA);
147 } else if (isOnline) {
148 super.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
150 super.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, STATUS_NO_CONNECTION);
155 * Disposes the thing.
158 public void dispose() {
159 logger.debug("Handler disposed.");
160 cancelJobReference(dataPollingJobReference);
164 * will update all channels provided in the map
167 public void updateChannelStatus(Map<Channel, State> values) {
168 logger.debug("Handling charger channel update.");
170 for (Channel channel : values.keySet()) {
171 if (getThing().getChannels().contains(channel)) {
172 State value = values.get(channel);
174 logger.debug("Channel is to be updated: {}: {}", channel.getUID().getAsString(), value);
175 updateState(channel.getUID(), value);
177 logger.debug("Value is null or not provided by Easee Cloud (channel: {})",
178 channel.getUID().getAsString());
179 updateState(channel.getUID(), UnDefType.UNDEF);
182 logger.debug("Could not identify channel: {} for model {}", channel.getUID().getAsString(),
183 getThing().getThingTypeUID().getAsString());
189 public void enqueueCommand(EaseeCommand command) {
190 EaseeBridgeHandler bridgeHandler = getBridgeHandler();
191 if (bridgeHandler != null) {
192 bridgeHandler.enqueueCommand(command);
194 // this should not happen
195 logger.warn("no bridge handler found");
199 private @Nullable EaseeBridgeHandler getBridgeHandler() {
200 Bridge bridge = getBridge();
201 return bridge == null ? null : ((EaseeBridgeHandler) bridge.getHandler());
205 public EaseeConfiguration getBridgeConfiguration() {
206 EaseeBridgeHandler bridgeHandler = getBridgeHandler();
207 return bridgeHandler == null ? new EaseeConfiguration() : bridgeHandler.getBridgeConfiguration();
211 public EaseeCommand buildEaseeCommand(Command command, Channel channel) {
212 String chargerId = getConfig().get(EaseeBindingConstants.THING_CONFIG_ID).toString();
214 switch (Utils.getWriteCommand(channel)) {
215 case COMMAND_CHANGE_CONFIGURATION:
216 return new ChangeConfiguration(this, chargerId, channel, command);
217 case COMMAND_SEND_COMMAND:
218 return new SendCommand(this, chargerId, channel, command);
219 case COMMAND_SEND_COMMAND_START_STOP:
220 return new SendCommandStartStop(this, chargerId, channel, command);
222 // this should not happen
223 logger.error("write command '{}' not found for channel '{}'", command.toString(),
224 channel.getUID().getIdWithoutGroup());
225 throw new UnsupportedOperationException(
226 "write command not found for channel: " + channel.getUID().getIdWithoutGroup());
231 public Logger getLogger() {