]> git.basschouten.com Git - openhab-addons.git/blob
612c985fd714d7bbd659af01097414bcb2115aba
[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.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20
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;
39
40 import com.google.gson.JsonObject;
41
42 /**
43  * The {@link EaseeSiteHandler} is responsible for handling commands, which are
44  * sent to one of the channels.
45  *
46  * @author Alexander Friese - initial contribution
47  */
48 @NonNullByDefault
49 public class EaseeSiteHandler extends BaseBridgeHandler implements EaseeBridgeHandler {
50     private final Logger logger = LoggerFactory.getLogger(EaseeSiteHandler.class);
51
52     private @Nullable DiscoveryService discoveryService;
53
54     /**
55      * Interface object for querying the Easee web interface
56      */
57     private WebInterface webInterface;
58
59     public EaseeSiteHandler(Bridge bridge, HttpClient httpClient) {
60         super(bridge);
61         this.webInterface = new WebInterface(scheduler, this, httpClient, super::updateStatus);
62     }
63
64     @Override
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());
69
70         updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, STATUS_WAITING_FOR_LOGIN);
71         webInterface.start();
72
73         enqueueCommand(new GetSite(this, this::updateProperties));
74     }
75
76     private void updateProperties(CommunicationStatus status, JsonObject jsonObject) {
77         Map<String, String> properties = editProperties();
78         String name = Utils.getAsString(jsonObject, JSON_KEY_GENERIC_NAME);
79         if (name != null) {
80             properties.put(JSON_KEY_GENERIC_NAME, name);
81         }
82         String siteKey = Utils.getAsString(jsonObject, JSON_KEY_SITE_KEY);
83         if (siteKey != null) {
84             properties.put(JSON_KEY_SITE_KEY, siteKey);
85         }
86         updateProperties(properties);
87     }
88
89     /**
90      * Disposes the bridge.
91      */
92     @Override
93     public void dispose() {
94         logger.debug("Handler disposed.");
95         webInterface.dispose();
96     }
97
98     @Override
99     public EaseeConfiguration getBridgeConfiguration() {
100         return this.getConfigAs(EaseeConfiguration.class);
101     }
102
103     @Override
104     public Collection<Class<? extends ThingHandlerService>> getServices() {
105         return Collections.singleton(EaseeSiteDiscoveryService.class);
106     }
107
108     public void setDiscoveryService(EaseeSiteDiscoveryService discoveryService) {
109         this.discoveryService = discoveryService;
110     }
111
112     @Override
113     public void startDiscovery() {
114         DiscoveryService discoveryService = this.discoveryService;
115         if (discoveryService != null) {
116             discoveryService.startScan(null);
117         }
118     }
119
120     @Override
121     public void enqueueCommand(EaseeCommand command) {
122         webInterface.enqueueCommand(command);
123     }
124
125     @Override
126     public Logger getLogger() {
127         return logger;
128     }
129 }