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.connector.StatusUpdateListener;
26 import org.openhab.binding.solaredge.internal.handler.SolarEdgeHandler;
27 import org.openhab.binding.solaredge.internal.model.AggregateDataResponsePrivateApi;
28 import org.openhab.binding.solaredge.internal.model.AggregateDataResponseTransformerPrivateApi;
29 import org.openhab.binding.solaredge.internal.model.AggregatePeriod;
32 * command that retrieves status values for aggregate data channels via private API
34 * @author Alexander Friese - initial contribution
37 public class AggregateDataUpdatePrivateApi extends AbstractCommand implements SolarEdgeCommand {
40 * the solaredge handler
42 private final SolarEdgeHandler handler;
43 private final AggregateDataResponseTransformerPrivateApi transformer;
46 * data aggregation level
48 private final AggregatePeriod period;
51 * url suffix depending on aggregation level
53 private final String urlSuffix;
54 private int retries = 0;
62 public AggregateDataUpdatePrivateApi(SolarEdgeHandler handler, AggregatePeriod period,
63 StatusUpdateListener listener) {
64 super(handler.getConfiguration(), listener);
65 this.handler = handler;
66 this.transformer = new AggregateDataResponseTransformerPrivateApi(handler);
70 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
73 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
76 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
79 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
87 protected Request prepareRequest(Request requestToPrepare) {
88 requestToPrepare.followRedirects(false);
89 requestToPrepare.param(PRIVATE_DATA_API_AGGREGATE_DATA_CHARTFIELD_FIELD, period.toString());
90 requestToPrepare.method(HttpMethod.GET);
92 return requestToPrepare;
96 protected String getURL() {
97 return PRIVATE_DATA_API_URL + config.getSolarId() + urlSuffix;
101 public void onComplete(@Nullable Result result) {
102 logger.debug("onComplete()");
104 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
105 updateListenerStatus();
106 if (retries++ < MAX_RETRIES) {
107 handler.getWebInterface().enqueueCommand(this);
110 String json = getContentAsString(StandardCharsets.UTF_8);
112 logger.debug("JSON String: {}", json);
113 AggregateDataResponsePrivateApi jsonObject = fromJson(json, AggregateDataResponsePrivateApi.class);
114 if (jsonObject != null) {
115 handler.updateChannelStatus(transformer.transform(jsonObject, period));