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.callback.AbstractCommandCallback;
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 AbstractCommandCallback 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 super(handler.getConfiguration());
66 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
67 this.handler = handler;
68 this.transformer = new AggregateDataResponseTransformerPublicApi(handler);
73 protected Request prepareRequest(Request requestToPrepare) {
74 requestToPrepare.followRedirects(false);
75 requestToPrepare.method(HttpMethod.GET);
77 String currentDate = dateFormat.format(Calendar.getInstance().getTime());
79 requestToPrepare.param(PUBLIC_DATA_API_TIME_UNIT_FIELD, period.toString());
80 requestToPrepare.param(PUBLIC_DATA_API_START_TIME_FIELD, currentDate + " " + BEGIN_OF_DAY_TIME);
81 requestToPrepare.param(PUBLIC_DATA_API_END_TIME_FIELD, currentDate + " " + END_OF_DAY_TIME);
83 return requestToPrepare;
87 protected String getURL() {
88 return PUBLIC_DATA_API_URL + config.getSolarId() + PUBLIC_DATA_API_URL_AGGREGATE_DATA_SUFFIX;
92 public void onComplete(@Nullable Result result) {
93 logger.debug("onComplete()");
95 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
96 updateListenerStatus();
97 if (retries++ < MAX_RETRIES) {
98 handler.getWebInterface().enqueueCommand(this);
101 String json = getContentAsString(StandardCharsets.UTF_8);
103 logger.debug("JSON String: {}", json);
104 AggregateDataResponsePublicApi jsonObject = fromJson(json, AggregateDataResponsePublicApi.class);
105 if (jsonObject != null) {
106 handler.updateChannelStatus(transformer.transform(jsonObject, period));