2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ecowatt.internal.restapi;
15 import java.time.ZonedDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.temporal.ChronoUnit;
18 import java.util.List;
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;
27 * The {@link EcowattApiResponse} class contains fields mapping the response to the Ecowatt API request /signals.
29 * It also includes an exception field to be set in case the API request fails.
31 * @author Laurent Garnier - Initial contribution
34 public class EcowattApiResponse {
35 private final Logger logger = LoggerFactory.getLogger(EcowattApiResponse.class);
37 public @Nullable List<EcowattDaySignals> signals;
38 private @Nullable CommunicationException exception;
40 public EcowattApiResponse() {
42 this.exception = null;
45 public EcowattApiResponse(@Nullable List<EcowattDaySignals> signals) {
46 this.signals = signals;
47 this.exception = null;
50 public EcowattApiResponse(CommunicationException exception) {
52 this.exception = exception;
56 * Search the data for the day of the given date and time
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
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();
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));
86 public boolean succeeded() {
87 return signals != null;
90 public @Nullable CommunicationException getException() {