]> git.basschouten.com Git - openhab-addons.git/blob
4be0a17f2785345757f19000204d7a3dc8fc678d
[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.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;
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 AbstractCommand 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             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);
70         this.period = period;
71     }
72
73     @Override
74     protected Request prepareRequest(Request requestToPrepare) {
75         requestToPrepare.followRedirects(false);
76         requestToPrepare.method(HttpMethod.GET);
77
78         String currentDate = dateFormat.format(Calendar.getInstance().getTime());
79
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);
83
84         return requestToPrepare;
85     }
86
87     @Override
88     protected String getURL() {
89         return PUBLIC_DATA_API_URL + config.getSolarId() + PUBLIC_DATA_API_URL_AGGREGATE_DATA_SUFFIX;
90     }
91
92     @Override
93     public void onComplete(@Nullable Result result) {
94         logger.debug("onComplete()");
95
96         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
97             updateListenerStatus();
98             if (retries++ < MAX_RETRIES) {
99                 handler.getWebInterface().enqueueCommand(this);
100             }
101         } else {
102             String json = getContentAsString(StandardCharsets.UTF_8);
103             if (json != null) {
104                 logger.debug("JSON String: {}", json);
105                 AggregateDataResponsePublicApi jsonObject = fromJson(json, AggregateDataResponsePublicApi.class);
106                 if (jsonObject != null) {
107                     handler.updateChannelStatus(transformer.transform(jsonObject, period));
108                 }
109             }
110         }
111     }
112 }