]> git.basschouten.com Git - openhab-addons.git/blob
b146e9a6d2e0967bc11da2bfb557976787d3ecb8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ecowatt.internal.restapi;
14
15 import java.time.ZonedDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.temporal.ChronoUnit;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.i18n.CommunicationException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link EcowattApiResponse} class contains fields mapping the response to the Ecowatt API request /signals.
28  *
29  * It also includes an exception field to be set in case the API request fails.
30  *
31  * @author Laurent Garnier - Initial contribution
32  */
33 @NonNullByDefault
34 public class EcowattApiResponse {
35     private final Logger logger = LoggerFactory.getLogger(EcowattApiResponse.class);
36
37     public @Nullable List<EcowattDaySignals> signals;
38     private @Nullable CommunicationException exception;
39
40     public EcowattApiResponse() {
41         this.signals = null;
42         this.exception = null;
43     }
44
45     public EcowattApiResponse(@Nullable List<EcowattDaySignals> signals) {
46         this.signals = signals;
47         this.exception = null;
48     }
49
50     public EcowattApiResponse(CommunicationException exception) {
51         this.signals = null;
52         this.exception = exception;
53     }
54
55     /**
56      * Search the data for the day of the given date and time
57      *
58      * @param dateTime a date and time
59      * @return the data for the searched day or null if no data is found for this day
60      */
61     public @Nullable EcowattDaySignals getDaySignals(ZonedDateTime dateTime) {
62         List<EcowattDaySignals> localSignals = signals;
63         if (localSignals != null) {
64             for (EcowattDaySignals daySignals : localSignals) {
65                 ZonedDateTime zdt = daySignals.getDay();
66                 if (zdt != null) {
67                     // Adjust date/times to the same offset/zone
68                     ZonedDateTime dateTime2 = dateTime.withZoneSameInstant(zdt.getZone());
69                     logger.trace("zdt {} offset {} - dateTime2 {} offset {}",
70                             zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME), zdt.getOffset(),
71                             dateTime2.format(DateTimeFormatter.ISO_ZONED_DATE_TIME), dateTime2.getOffset());
72                     // Check if the two date/times are in the same day
73                     if (zdt.truncatedTo(ChronoUnit.DAYS).toInstant()
74                             .equals(dateTime2.truncatedTo(ChronoUnit.DAYS).toInstant())) {
75                         logger.debug("getDaySignals for {} returns signal {} : {} ( {} )",
76                                 dateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME), daySignals.getDaySignal(),
77                                 daySignals.getDayMessage(), zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
78                         return daySignals;
79                     }
80                 }
81             }
82         }
83         return null;
84     }
85
86     public boolean succeeded() {
87         return signals != null;
88     }
89
90     public @Nullable CommunicationException getException() {
91         return exception;
92     }
93 }