]> git.basschouten.com Git - openhab-addons.git/blob
06606bdc8c11052a261c618442addbce67feef15
[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
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.callback.AbstractCommandCallback;
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;
30
31 /**
32  * command that retrieves status values for aggregate data channels via private API
33  *
34  * @author Alexander Friese - initial contribution
35  */
36 @NonNullByDefault
37 public class AggregateDataUpdatePrivateApi extends AbstractCommandCallback implements SolarEdgeCommand {
38
39     /**
40      * the solaredge handler
41      */
42     private final SolarEdgeHandler handler;
43     private final AggregateDataResponseTransformerPrivateApi transformer;
44
45     /**
46      * data aggregation level
47      */
48     private final AggregatePeriod period;
49
50     /**
51      * url suffix depending on aggregation level
52      */
53     private final String urlSuffix;
54     private int retries = 0;
55
56     /**
57      * the constructor
58      *
59      * @param handler
60      * @param period
61      */
62     public AggregateDataUpdatePrivateApi(SolarEdgeHandler handler, AggregatePeriod period) {
63         super(handler.getConfiguration());
64         this.handler = handler;
65         this.transformer = new AggregateDataResponseTransformerPrivateApi(handler);
66         this.period = period;
67         switch (period) {
68             case DAY:
69                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
70                 break;
71             case WEEK:
72                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
73                 break;
74             case MONTH:
75                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
76                 break;
77             case YEAR:
78                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
79                 break;
80             default:
81                 this.urlSuffix = "";
82         }
83     }
84
85     @Override
86     protected Request prepareRequest(Request requestToPrepare) {
87         requestToPrepare.followRedirects(false);
88         requestToPrepare.param(PRIVATE_DATA_API_AGGREGATE_DATA_CHARTFIELD_FIELD, period.toString());
89         requestToPrepare.method(HttpMethod.GET);
90
91         return requestToPrepare;
92     }
93
94     @Override
95     protected String getURL() {
96         return PRIVATE_DATA_API_URL + config.getSolarId() + urlSuffix;
97     }
98
99     @Override
100     public void onComplete(@Nullable Result result) {
101         logger.debug("onComplete()");
102
103         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
104             updateListenerStatus();
105             if (retries++ < MAX_RETRIES) {
106                 handler.getWebInterface().enqueueCommand(this);
107             }
108         } else {
109             String json = getContentAsString(StandardCharsets.UTF_8);
110             if (json != null) {
111                 logger.debug("JSON String: {}", json);
112                 AggregateDataResponsePrivateApi jsonObject = fromJson(json, AggregateDataResponsePrivateApi.class);
113                 if (jsonObject != null) {
114                     handler.updateChannelStatus(transformer.transform(jsonObject, period));
115                 }
116             }
117         }
118     }
119 }