]> git.basschouten.com Git - openhab-addons.git/blob
6d33505a1b71c33c6573e62c79599b2bb96c91f3
[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.connector.StatusUpdateListener;
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 AbstractCommand 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             StatusUpdateListener listener) {
64         super(handler.getConfiguration(), listener);
65         this.handler = handler;
66         this.transformer = new AggregateDataResponseTransformerPrivateApi(handler);
67         this.period = period;
68         switch (period) {
69             case DAY:
70                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
71                 break;
72             case WEEK:
73                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_DAY_WEEK_SUFFIX;
74                 break;
75             case MONTH:
76                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
77                 break;
78             case YEAR:
79                 this.urlSuffix = PRIVATE_DATA_API_URL_AGGREGATE_DATA_MONTH_YEAR_SUFFIX;
80                 break;
81             default:
82                 this.urlSuffix = "";
83         }
84     }
85
86     @Override
87     protected Request prepareRequest(Request requestToPrepare) {
88         requestToPrepare.followRedirects(false);
89         requestToPrepare.param(PRIVATE_DATA_API_AGGREGATE_DATA_CHARTFIELD_FIELD, period.toString());
90         requestToPrepare.method(HttpMethod.GET);
91
92         return requestToPrepare;
93     }
94
95     @Override
96     protected String getURL() {
97         return PRIVATE_DATA_API_URL + config.getSolarId() + urlSuffix;
98     }
99
100     @Override
101     public void onComplete(@Nullable Result result) {
102         logger.debug("onComplete()");
103
104         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode())) {
105             updateListenerStatus();
106             if (retries++ < MAX_RETRIES) {
107                 handler.getWebInterface().enqueueCommand(this);
108             }
109         } else {
110             String json = getContentAsString(StandardCharsets.UTF_8);
111             if (json != null) {
112                 logger.debug("JSON String: {}", json);
113                 AggregateDataResponsePrivateApi jsonObject = fromJson(json, AggregateDataResponsePrivateApi.class);
114                 if (jsonObject != null) {
115                     handler.updateChannelStatus(transformer.transform(jsonObject, period));
116                 }
117             }
118         }
119     }
120 }