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.*;
17 import java.util.Collection;
18 import java.util.Collections;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.easee.internal.Utils;
25 import org.openhab.binding.easee.internal.command.EaseeCommand;
26 import org.openhab.binding.easee.internal.command.site.GetSite;
27 import org.openhab.binding.easee.internal.config.EaseeConfiguration;
28 import org.openhab.binding.easee.internal.connector.CommunicationStatus;
29 import org.openhab.binding.easee.internal.connector.WebInterface;
30 import org.openhab.binding.easee.internal.discovery.EaseeSiteDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.BaseBridgeHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.gson.JsonObject;
43 * The {@link EaseeSiteHandler} is responsible for handling commands, which are
44 * sent to one of the channels.
46 * @author Alexander Friese - initial contribution
49 public class EaseeSiteHandler extends BaseBridgeHandler implements EaseeBridgeHandler {
50 private final Logger logger = LoggerFactory.getLogger(EaseeSiteHandler.class);
52 private @Nullable DiscoveryService discoveryService;
55 * Interface object for querying the Easee web interface
57 private WebInterface webInterface;
59 public EaseeSiteHandler(Bridge bridge, HttpClient httpClient) {
61 this.webInterface = new WebInterface(scheduler, this, httpClient, super::updateStatus);
65 public void initialize() {
66 logger.debug("About to initialize Easee Site");
67 EaseeConfiguration config = getBridgeConfiguration();
68 logger.debug("Easee Site initialized with configuration: {}", config.toString());
70 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, STATUS_WAITING_FOR_LOGIN);
73 enqueueCommand(new GetSite(this, this::updateProperties));
76 private void updateProperties(CommunicationStatus status, JsonObject jsonObject) {
77 Map<String, String> properties = editProperties();
78 String name = Utils.getAsString(jsonObject, JSON_KEY_GENERIC_NAME);
80 properties.put(JSON_KEY_GENERIC_NAME, name);
82 String siteKey = Utils.getAsString(jsonObject, JSON_KEY_SITE_KEY);
83 if (siteKey != null) {
84 properties.put(JSON_KEY_SITE_KEY, siteKey);
86 updateProperties(properties);
90 * Disposes the bridge.
93 public void dispose() {
94 logger.debug("Handler disposed.");
95 webInterface.dispose();
99 public EaseeConfiguration getBridgeConfiguration() {
100 return this.getConfigAs(EaseeConfiguration.class);
104 public Collection<Class<? extends ThingHandlerService>> getServices() {
105 return Collections.singleton(EaseeSiteDiscoveryService.class);
108 public void setDiscoveryService(EaseeSiteDiscoveryService discoveryService) {
109 this.discoveryService = discoveryService;
113 public void startDiscovery() {
114 DiscoveryService discoveryService = this.discoveryService;
115 if (discoveryService != null) {
116 discoveryService.startScan(null);
121 public void enqueueCommand(EaseeCommand command) {
122 webInterface.enqueueCommand(command);
126 public Logger getLogger() {