]> git.basschouten.com Git - openhab-addons.git/blob
1806d5aebe5d752b6eddf36ec59aa4ca738a2f76
[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.solaredge.internal.command;
14
15 import static org.openhab.binding.solaredge.internal.SolarEdgeBindingConstants.*;
16
17 import java.nio.charset.StandardCharsets;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.api.Request;
22 import org.eclipse.jetty.client.api.Result;
23 import org.eclipse.jetty.http.HttpMethod;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.openhab.binding.solaredge.internal.connector.StatusUpdateListener;
26 import org.openhab.binding.solaredge.internal.handler.SolarEdgeHandler;
27 import org.openhab.binding.solaredge.internal.model.LiveDataResponse;
28 import org.openhab.binding.solaredge.internal.model.LiveDataResponseTransformer;
29
30 /**
31  * command that retrieves status values for live data channels via private API
32  *
33  * @author Alexander Friese - initial contribution
34  */
35 @NonNullByDefault
36 public class LiveDataUpdatePrivateApi extends AbstractCommand implements SolarEdgeCommand {
37
38     private final SolarEdgeHandler handler;
39     private final LiveDataResponseTransformer transformer;
40     private int retries = 0;
41
42     public LiveDataUpdatePrivateApi(SolarEdgeHandler handler, StatusUpdateListener listener) {
43         super(handler.getConfiguration(), listener);
44         this.handler = handler;
45         this.transformer = new LiveDataResponseTransformer(handler);
46     }
47
48     @Override
49     protected Request prepareRequest(Request requestToPrepare) {
50         requestToPrepare.followRedirects(false);
51         requestToPrepare.method(HttpMethod.GET);
52
53         return requestToPrepare;
54     }
55
56     @Override
57     protected String getURL() {
58         return PRIVATE_DATA_API_URL + config.getSolarId() + PRIVATE_DATA_API_URL_LIVE_DATA_SUFFIX;
59     }
60
61     @Override
62     public void onComplete(@Nullable Result result) {
63         logger.debug("onComplete()");
64
65         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
66             updateListenerStatus();
67             if (retries++ < MAX_RETRIES) {
68                 handler.getWebInterface().enqueueCommand(this);
69             }
70         } else {
71             String json = getContentAsString(StandardCharsets.UTF_8);
72             if (json != null) {
73                 logger.debug("JSON String: {}", json);
74                 LiveDataResponse jsonObject = fromJson(json, LiveDataResponse.class);
75                 if (jsonObject != null) {
76                     handler.updateChannelStatus(transformer.transform(jsonObject));
77                 }
78             }
79         }
80     }
81 }