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;
18 import java.text.SimpleDateFormat;
19 import java.util.Calendar;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.client.api.Result;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.openhab.binding.solaredge.internal.connector.StatusUpdateListener;
28 import org.openhab.binding.solaredge.internal.handler.SolarEdgeHandler;
29 import org.openhab.binding.solaredge.internal.model.AggregateDataResponsePublicApi;
30 import org.openhab.binding.solaredge.internal.model.AggregateDataResponseTransformerPublicApi;
31 import org.openhab.binding.solaredge.internal.model.AggregatePeriod;
34 * command that retrieves status values for aggregate data channels via public API
36 * @author Alexander Friese - initial contribution
39 public class AggregateDataUpdatePublicApi extends AbstractCommand implements SolarEdgeCommand {
42 * the solaredge handler
44 private final SolarEdgeHandler handler;
45 private final AggregateDataResponseTransformerPublicApi transformer;
48 * data aggregation level
50 private final AggregatePeriod period;
53 * date format which is expected by the API
55 private final SimpleDateFormat dateFormat;
56 private int retries = 0;
64 public AggregateDataUpdatePublicApi(SolarEdgeHandler handler, AggregatePeriod period,
65 StatusUpdateListener listener) {
66 super(handler.getConfiguration(), listener);
67 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
68 this.handler = handler;
69 this.transformer = new AggregateDataResponseTransformerPublicApi(handler);
74 protected Request prepareRequest(Request requestToPrepare) {
75 requestToPrepare.followRedirects(false);
76 requestToPrepare.method(HttpMethod.GET);
78 String currentDate = dateFormat.format(Calendar.getInstance().getTime());
80 requestToPrepare.param(PUBLIC_DATA_API_TIME_UNIT_FIELD, period.toString());
81 requestToPrepare.param(PUBLIC_DATA_API_START_TIME_FIELD, currentDate + " " + BEGIN_OF_DAY_TIME);
82 requestToPrepare.param(PUBLIC_DATA_API_END_TIME_FIELD, currentDate + " " + END_OF_DAY_TIME);
84 return requestToPrepare;
88 protected String getURL() {
89 return PUBLIC_DATA_API_URL + config.getSolarId() + PUBLIC_DATA_API_URL_AGGREGATE_DATA_SUFFIX;
93 public void onComplete(@Nullable Result result) {
94 logger.debug("onComplete()");
96 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
97 updateListenerStatus();
98 if (retries++ < MAX_RETRIES) {
99 handler.getWebInterface().enqueueCommand(this);
102 String json = getContentAsString(StandardCharsets.UTF_8);
104 logger.debug("JSON String: {}", json);
105 AggregateDataResponsePublicApi jsonObject = fromJson(json, AggregateDataResponsePublicApi.class);
106 if (jsonObject != null) {
107 handler.updateChannelStatus(transformer.transform(jsonObject, period));