]> git.basschouten.com Git - openhab-addons.git/blob
7d3d461425614df9e7b0938f5d5e5bc2d20ec71c
[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.handler;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicReference;
21
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;
48
49 import com.google.gson.JsonObject;
50
51 /**
52  * The {@link EaseeChargerHandler} is responsible for handling commands, which are
53  * sent to one of the channels.
54  *
55  * @author Alexander Friese - initial contribution
56  */
57 @NonNullByDefault
58 public class EaseeChargerHandler extends BaseThingHandler implements EaseeThingHandler, AtomicReferenceTrait {
59     private final Logger logger = LoggerFactory.getLogger(EaseeChargerHandler.class);
60
61     /**
62      * Schedule for polling live data
63      */
64     private final AtomicReference<@Nullable Future<?>> dataPollingJobReference;
65
66     public EaseeChargerHandler(Thing thing) {
67         super(thing);
68         this.dataPollingJobReference = new AtomicReference<>(null);
69     }
70
71     @Override
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);
76
77         updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, STATUS_WAITING_FOR_BRIDGE);
78         startPolling();
79
80         enqueueCommand(new Charger(this, chargerId, this::updateProperties));
81     }
82
83     private void updateProperties(CommunicationStatus status, JsonObject charger) {
84         Map<String, String> properties = editProperties();
85
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);
92             } else {
93                 properties.put(THING_CONFIG_IS_MASTER, GENERIC_NO);
94             }
95             properties.put(THING_CONFIG_BACK_PLATE_ID, backPlateId);
96             properties.put(THING_CONFIG_MASTER_BACK_PLATE_ID, masterBackPlateId);
97         }
98         String chargerName = Utils.getAsString(charger, JSON_KEY_GENERIC_NAME);
99         if (chargerName != null) {
100             properties.put(JSON_KEY_GENERIC_NAME, chargerName);
101         }
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);
105         }
106
107         updateProperties(properties);
108     }
109
110     /**
111      * Start the polling.
112      */
113     private void startPolling() {
114         updateJobReference(dataPollingJobReference, scheduler.scheduleWithFixedDelay(this::pollingRun,
115                 POLLING_INITIAL_DELAY, getBridgeConfiguration().getDataPollingInterval(), TimeUnit.SECONDS));
116     }
117
118     /**
119      * Poll the Easee Cloud API one time.
120      */
121     void pollingRun() {
122         String chargerId = getConfig().get(EaseeBindingConstants.THING_CONFIG_ID).toString();
123         logger.debug("polling charger data for {}", chargerId);
124
125         ChargerState state = new ChargerState(this, chargerId);
126         state.registerResultProcessor(this::updateStatusInfo);
127         enqueueCommand(state);
128
129         // proceed if charger is online
130         if (getThing().getStatus() == ThingStatus.ONLINE) {
131             enqueueCommand(new GetConfiguration(this, chargerId));
132             enqueueCommand(new LatestChargingSession(this, chargerId));
133         }
134     }
135
136     /**
137      * updates status depending on online information received from the API.
138      *
139      * @param status
140      * @param jsonObject
141      */
142     private void updateStatusInfo(CommunicationStatus status, JsonObject jsonObject) {
143         Boolean isOnline = Utils.getAsBool(jsonObject, JSON_KEY_ONLINE);
144
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);
149         } else {
150             super.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, STATUS_NO_CONNECTION);
151         }
152     }
153
154     /**
155      * Disposes the thing.
156      */
157     @Override
158     public void dispose() {
159         logger.debug("Handler disposed.");
160         cancelJobReference(dataPollingJobReference);
161     }
162
163     /**
164      * will update all channels provided in the map
165      */
166     @Override
167     public void updateChannelStatus(Map<Channel, State> values) {
168         logger.debug("Handling charger channel update.");
169
170         for (Channel channel : values.keySet()) {
171             if (getThing().getChannels().contains(channel)) {
172                 State value = values.get(channel);
173                 if (value != null) {
174                     logger.debug("Channel is to be updated: {}: {}", channel.getUID().getAsString(), value);
175                     updateState(channel.getUID(), value);
176                 } else {
177                     logger.debug("Value is null or not provided by Easee Cloud (channel: {})",
178                             channel.getUID().getAsString());
179                     updateState(channel.getUID(), UnDefType.UNDEF);
180                 }
181             } else {
182                 logger.debug("Could not identify channel: {} for model {}", channel.getUID().getAsString(),
183                         getThing().getThingTypeUID().getAsString());
184             }
185         }
186     }
187
188     @Override
189     public void enqueueCommand(EaseeCommand command) {
190         EaseeBridgeHandler bridgeHandler = getBridgeHandler();
191         if (bridgeHandler != null) {
192             bridgeHandler.enqueueCommand(command);
193         } else {
194             // this should not happen
195             logger.warn("no bridge handler found");
196         }
197     }
198
199     private @Nullable EaseeBridgeHandler getBridgeHandler() {
200         Bridge bridge = getBridge();
201         return bridge == null ? null : ((EaseeBridgeHandler) bridge.getHandler());
202     }
203
204     @Override
205     public EaseeConfiguration getBridgeConfiguration() {
206         EaseeBridgeHandler bridgeHandler = getBridgeHandler();
207         return bridgeHandler == null ? new EaseeConfiguration() : bridgeHandler.getBridgeConfiguration();
208     }
209
210     @Override
211     public EaseeCommand buildEaseeCommand(Command command, Channel channel) {
212         String chargerId = getConfig().get(EaseeBindingConstants.THING_CONFIG_ID).toString();
213
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);
221             default:
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());
227         }
228     }
229
230     @Override
231     public Logger getLogger() {
232         return logger;
233     }
234 }