]> git.basschouten.com Git - openhab-addons.git/blob
016c668d1f9a62441fa88af87581dac9b91f260d
[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.callback.AbstractCommandCallback;
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 public API
32  *
33  * @author Alexander Friese - initial contribution
34  */
35 @NonNullByDefault
36 public class LiveDataUpdatePublicApi extends AbstractCommandCallback implements SolarEdgeCommand {
37
38     private final SolarEdgeHandler handler;
39     private final LiveDataResponseTransformer transformer;
40     private int retries = 0;
41
42     public LiveDataUpdatePublicApi(SolarEdgeHandler handler) {
43         super(handler.getConfiguration());
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 PUBLIC_DATA_API_URL + config.getSolarId() + PUBLIC_DATA_API_URL_LIVE_DATA_SUFFIX;
59     }
60
61     @Override
62     public void onComplete(@Nullable Result result) {
63         logger.debug("onComplete()");
64         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
65             updateListenerStatus();
66             if (retries++ < MAX_RETRIES) {
67                 handler.getWebInterface().enqueueCommand(this);
68             }
69         } else {
70             String json = getContentAsString(StandardCharsets.UTF_8);
71             if (json != null) {
72                 logger.debug("JSON String: {}", json);
73                 LiveDataResponse jsonObject = fromJson(json, LiveDataResponse.class);
74                 if (jsonObject != null) {
75                     handler.updateChannelStatus(transformer.transform(jsonObject));
76                 }
77             }
78         }
79     }
80 }