]> git.basschouten.com Git - openhab-addons.git/blob
28d3c0195cbac8e1f521a7a7cef9dfedff37aff6
[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 import java.text.SimpleDateFormat;
19 import java.util.Calendar;
20
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;
32
33 /**
34  * command that retrieves status values for aggregate data channels via public API
35  *
36  * @author Alexander Friese - initial contribution
37  */
38 @NonNullByDefault
39 public class AggregateDataUpdatePublicApi extends AbstractCommandCallback implements SolarEdgeCommand {
40
41     /**
42      * the solaredge handler
43      */
44     private final SolarEdgeHandler handler;
45     private final AggregateDataResponseTransformerPublicApi transformer;
46
47     /**
48      * data aggregation level
49      */
50     private final AggregatePeriod period;
51
52     /**
53      * date format which is expected by the API
54      */
55     private final SimpleDateFormat dateFormat;
56     private int retries = 0;
57
58     /**
59      * the constructor
60      *
61      * @param handler
62      * @param period
63      */
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);
69         this.period = period;
70     }
71
72     @Override
73     protected Request prepareRequest(Request requestToPrepare) {
74         requestToPrepare.followRedirects(false);
75         requestToPrepare.method(HttpMethod.GET);
76
77         String currentDate = dateFormat.format(Calendar.getInstance().getTime());
78
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);
82
83         return requestToPrepare;
84     }
85
86     @Override
87     protected String getURL() {
88         return PUBLIC_DATA_API_URL + config.getSolarId() + PUBLIC_DATA_API_URL_AGGREGATE_DATA_SUFFIX;
89     }
90
91     @Override
92     public void onComplete(@Nullable Result result) {
93         logger.debug("onComplete()");
94
95         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
96             updateListenerStatus();
97             if (retries++ < MAX_RETRIES) {
98                 handler.getWebInterface().enqueueCommand(this);
99             }
100         } else {
101             String json = getContentAsString(StandardCharsets.UTF_8);
102             if (json != null) {
103                 logger.debug("JSON String: {}", json);
104                 AggregateDataResponsePublicApi jsonObject = fromJson(json, AggregateDataResponsePublicApi.class);
105                 if (jsonObject != null) {
106                     handler.updateChannelStatus(transformer.transform(jsonObject, period));
107                 }
108             }
109         }
110     }
111 }