]> git.basschouten.com Git - openhab-addons.git/blob
a9a32b5a1fe59bc136151d9d5c12bfbf12ebc9b9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.energidataservice.internal.config;
14
15 import java.time.Duration;
16 import java.time.LocalDate;
17 import java.time.format.DateTimeParseException;
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
25 import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
26 import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
27
28 /**
29  * The {@link DatahubPriceConfiguration} class contains fields mapping channel configuration parameters.
30  *
31  * @author Jacob Laursen - Initial contribution
32  */
33 @NonNullByDefault
34 public class DatahubPriceConfiguration {
35
36     /**
37      * Comma-separated list of charge type codes, e.g. "CD,CD R".
38      */
39     public String chargeTypeCodes = "";
40
41     /**
42      * Comma-separated list of notes, e.g. "Nettarif C".
43      */
44     public String notes = "";
45
46     /**
47      * Query start date parameter expressed as either yyyy-mm-dd or one of StartOfDay, StartOfMonth or StartOfYear.
48      */
49     public String start = "";
50
51     /**
52      * Query start date offset expressed as an ISO 8601 duration.
53      */
54     public String offset = "";
55
56     /**
57      * Check if any filter values are provided.
58      *
59      * @return true if either charge type codes, notes or query start date is provided.
60      */
61     public boolean hasAnyFilterOverrides() {
62         return !chargeTypeCodes.isBlank() || !notes.isBlank() || !start.isBlank();
63     }
64
65     /**
66      * Get parsed set of charge type codes from comma-separated string.
67      *
68      * @return Set of charge type codes.
69      */
70     public Set<ChargeTypeCode> getChargeTypeCodes() {
71         return chargeTypeCodes.isBlank() ? new HashSet<>()
72                 : new HashSet<ChargeTypeCode>(
73                         Arrays.stream(chargeTypeCodes.split(",")).map(ChargeTypeCode::new).toList());
74     }
75
76     /**
77      * Get parsed set of notes from comma-separated string.
78      *
79      * @return Set of notes.
80      */
81     public Set<String> getNotes() {
82         return notes.isBlank() ? new HashSet<>() : new HashSet<String>(Arrays.asList(notes.split(",")));
83     }
84
85     /**
86      * Get query start parameter.
87      *
88      * @return null if invalid, otherwise an initialized {@link DateQueryParameter}.
89      */
90     public @Nullable DateQueryParameter getStart() {
91         if (start.isBlank()) {
92             return DateQueryParameter.EMPTY;
93         }
94         Duration durationOffset = Duration.ZERO;
95         if (!offset.isBlank()) {
96             try {
97                 durationOffset = Duration.parse(offset);
98             } catch (DateTimeParseException e) {
99                 return null;
100             }
101         }
102         if (start.equals(DateQueryParameterType.START_OF_DAY.toString())) {
103             return DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, durationOffset);
104         }
105         if (start.equals(DateQueryParameterType.START_OF_MONTH.toString())) {
106             return DateQueryParameter.of(DateQueryParameterType.START_OF_MONTH, durationOffset);
107         }
108         if (start.equals(DateQueryParameterType.START_OF_YEAR.toString())) {
109             return DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR, durationOffset);
110         }
111         try {
112             return DateQueryParameter.of(LocalDate.parse(start));
113         } catch (DateTimeParseException e) {
114             return null;
115         }
116     }
117 }