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