]> git.basschouten.com Git - openhab-addons.git/blob
d5b0946344008f83c733f8ea9884a3bf2552aabb
[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.solarwatt.internal.handler;
14
15 import java.net.URI;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.client.api.ContentResponse;
24 import org.eclipse.jetty.client.api.Request;
25 import org.eclipse.jetty.http.HttpStatus;
26 import org.openhab.binding.solarwatt.internal.configuration.SolarwattBridgeConfiguration;
27 import org.openhab.binding.solarwatt.internal.domain.EnergyManagerCollection;
28 import org.openhab.binding.solarwatt.internal.domain.dto.EnergyManagerDTO;
29 import org.openhab.binding.solarwatt.internal.exception.SolarwattConnectionException;
30 import org.openhab.binding.solarwatt.internal.factory.EnergyManagerDevicesFactory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.gson.Gson;
35 import com.google.gson.GsonBuilder;
36 import com.google.gson.JsonSyntaxException;
37
38 /**
39  * Class to talk to the energy anager via HTTP and return the concrete device instances.
40  *
41  * @author Sven Carstens - Initial contribution
42  */
43 @NonNullByDefault
44 public class EnergyManagerConnector {
45     private static final String PROTOCOL = "http://";
46     private static final String WIZARD_DEVICES_URL = "/rest/kiwigrid/wizard/devices";
47     private static final long CONNECT_TIMEOUT_SECONDS = 30;
48
49     private final Logger logger = LoggerFactory.getLogger(EnergyManagerConnector.class);
50     private final Gson gson = new GsonBuilder().create();
51     private final HttpClient httpClient;
52     private @Nullable URI energyManagerURI;
53
54     public EnergyManagerConnector(final HttpClient httpClient) {
55         this.httpClient = httpClient;
56     }
57
58     /**
59      * Pass in the configuration to know which host to talk to.
60      *
61      * @param configuration containing the hostname.
62      */
63     public void setConfiguration(final @Nullable SolarwattBridgeConfiguration configuration) {
64         if (configuration != null) {
65             String hostname = configuration.hostname;
66
67             if (!hostname.isEmpty()) {
68                 this.energyManagerURI = URI.create(PROTOCOL + hostname + WIZARD_DEVICES_URL);
69             }
70         }
71     }
72
73     /**
74      * Get the collection of devices represented by the energy manager.
75      *
76      * Read the JSON and transform everything into concrete instances.
77      *
78      * @return wrapping the devices
79      * @throws SolarwattConnectionException on any communication error
80      */
81     public EnergyManagerCollection retrieveDevices() throws SolarwattConnectionException {
82         try {
83             final Request request = this.httpClient.newRequest(this.energyManagerURI).timeout(CONNECT_TIMEOUT_SECONDS,
84                     TimeUnit.SECONDS);
85             final ContentResponse response = request.send();
86
87             return this.getEnergyManagerCollectionFromJson(response);
88         } catch (final InterruptedException e) {
89             Thread.currentThread().interrupt();
90             throw new SolarwattConnectionException("Interrupted");
91         } catch (TimeoutException | ExecutionException e) {
92             throw new SolarwattConnectionException("Connection problem", e);
93         }
94     }
95
96     /**
97      * Parse body content from energy manager from json into our DTO.
98      *
99      * @param response
100      * @return collection containing all {@link DeviceDTO}s
101      * @throws SolarwattConnectionException on communication errors
102      */
103     private EnergyManagerCollection getEnergyManagerCollectionFromJson(ContentResponse response)
104             throws SolarwattConnectionException {
105         final String content = response.getContentAsString();
106
107         try {
108             if (response.getStatus() == HttpStatus.OK_200) {
109                 EnergyManagerDTO energyManagerDTO = this.gson.fromJson(content, EnergyManagerDTO.class);
110                 if (energyManagerDTO == null) {
111                     throw new SolarwattConnectionException("No data received");
112                 }
113                 return EnergyManagerDevicesFactory.getEnergyManagerCollection(energyManagerDTO);
114             } else {
115                 throw new SolarwattConnectionException(response.getReason());
116             }
117         } catch (final JsonSyntaxException e) {
118             this.logger.warn("Error parsing json: {}", content, e);
119             throw new SolarwattConnectionException(e.getMessage());
120         }
121     }
122 }