]> git.basschouten.com Git - openhab-addons.git/blob
f662aaf167fc1b660598094724f463f7b530cb46
[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.sonnen.internal.communication;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.sonnen.internal.SonnenConfiguration;
20 import org.openhab.core.io.net.http.HttpUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonSyntaxException;
26
27 /**
28  * This class handles the JSON communication with the sonnen battery
29  *
30  * @author Christian Feininger - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class SonnenJSONCommunication {
35
36     private final Logger logger = LoggerFactory.getLogger(SonnenJSONCommunication.class);
37     private SonnenConfiguration config;
38
39     private Gson gson;
40     private @Nullable SonnenJsonDataDTO batteryData;
41
42     public SonnenJSONCommunication() {
43         gson = new Gson();
44         config = new SonnenConfiguration();
45     }
46
47     /**
48      * Refreshes the battery connection.
49      *
50      * @return an empty string if no error occurred, the error message otherwise.
51      */
52     public String refreshBatteryConnection() {
53         String result = "";
54         String urlStr = "http://" + config.hostIP + "/api/v1/status";
55
56         try {
57             String response = HttpUtil.executeUrl("GET", urlStr, 10000);
58             logger.debug("BatteryData = {}", response);
59             if (response == null) {
60                 throw new IOException("HttpUtil.executeUrl returned null");
61             }
62             batteryData = gson.fromJson(response, SonnenJsonDataDTO.class);
63         } catch (IOException | JsonSyntaxException e) {
64             logger.debug("Error processiong Get request {}:  {}", urlStr, e.getMessage());
65             result = "Cannot find service on given IP " + config.hostIP + ". Please verify the IP address!";
66             batteryData = null;
67         }
68         return result;
69     }
70
71     /**
72      * Set the config for service to communicate
73      *
74      * @param config2
75      */
76     public void setConfig(SonnenConfiguration config2) {
77         this.config = config2;
78     }
79
80     /**
81      * Returns the actual stored Battery Data
82      *
83      * @return JSON Data from the Battery or null if request failed
84      */
85     public @Nullable SonnenJsonDataDTO getBatteryData() {
86         return this.batteryData;
87     }
88 }