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.solarwatt.internal.handler;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
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;
34 import com.google.gson.Gson;
35 import com.google.gson.GsonBuilder;
36 import com.google.gson.JsonSyntaxException;
39 * Class to talk to the energy anager via HTTP and return the concrete device instances.
41 * @author Sven Carstens - Initial contribution
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;
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;
54 public EnergyManagerConnector(final HttpClient httpClient) {
55 this.httpClient = httpClient;
59 * Pass in the configuration to know which host to talk to.
61 * @param configuration containing the hostname.
63 public void setConfiguration(final @Nullable SolarwattBridgeConfiguration configuration) {
64 if (configuration != null) {
65 String hostname = configuration.hostname;
67 if (!hostname.isEmpty()) {
68 this.energyManagerURI = URI.create(PROTOCOL + hostname + WIZARD_DEVICES_URL);
74 * Get the collection of devices represented by the energy manager.
76 * Read the JSON and transform everything into concrete instances.
78 * @return wrapping the devices
79 * @throws SolarwattConnectionException on any communication error
81 public EnergyManagerCollection retrieveDevices() throws SolarwattConnectionException {
83 final Request request = this.httpClient.newRequest(this.energyManagerURI).timeout(CONNECT_TIMEOUT_SECONDS,
85 final ContentResponse response = request.send();
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);
97 * Parse body content from energy manager from json into our DTO.
100 * @return collection containing all {@link DeviceDTO}s
101 * @throws SolarwattConnectionException on communication errors
103 private EnergyManagerCollection getEnergyManagerCollectionFromJson(ContentResponse response)
104 throws SolarwattConnectionException {
105 final String content = response.getContentAsString();
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");
113 return EnergyManagerDevicesFactory.getEnergyManagerCollection(energyManagerDTO);
115 throw new SolarwattConnectionException(response.getReason());
117 } catch (final JsonSyntaxException e) {
118 this.logger.warn("Error parsing json: {}", content, e);
119 throw new SolarwattConnectionException(e.getMessage());