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.solaredge.internal.command;
15 import static org.openhab.binding.solaredge.internal.SolarEdgeBindingConstants.*;
17 import java.nio.charset.StandardCharsets;
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;
31 * command that retrieves status values for live data channels via private API
33 * @author Alexander Friese - initial contribution
36 public class LiveDataUpdatePrivateApi extends AbstractCommandCallback implements SolarEdgeCommand {
38 private final SolarEdgeHandler handler;
39 private final LiveDataResponseTransformer transformer;
40 private int retries = 0;
42 public LiveDataUpdatePrivateApi(SolarEdgeHandler handler) {
43 super(handler.getConfiguration());
44 this.handler = handler;
45 this.transformer = new LiveDataResponseTransformer(handler);
49 protected Request prepareRequest(Request requestToPrepare) {
50 requestToPrepare.followRedirects(false);
51 requestToPrepare.method(HttpMethod.GET);
53 return requestToPrepare;
57 protected String getURL() {
58 return PRIVATE_DATA_API_URL + config.getSolarId() + PRIVATE_DATA_API_URL_LIVE_DATA_SUFFIX;
62 public void onComplete(@Nullable Result result) {
63 logger.debug("onComplete()");
65 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
66 updateListenerStatus();
67 if (retries++ < MAX_RETRIES) {
68 handler.getWebInterface().enqueueCommand(this);
71 String json = getContentAsString(StandardCharsets.UTF_8);
73 logger.debug("JSON String: {}", json);
74 LiveDataResponse jsonObject = fromJson(json, LiveDataResponse.class);
75 if (jsonObject != null) {
76 handler.updateChannelStatus(transformer.transform(jsonObject));