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.energidataservice.internal.config;
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;
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;
29 * The {@link DatahubPriceConfiguration} class contains fields mapping channel configuration parameters.
31 * @author Jacob Laursen - Initial contribution
34 public class DatahubPriceConfiguration {
37 * Comma-separated list of charge type codes, e.g. "CD,CD R".
39 public String chargeTypeCodes = "";
42 * Comma-separated list of notes, e.g. "Nettarif C".
44 public String notes = "";
47 * Query start date parameter expressed as either yyyy-mm-dd or one of StartOfDay, StartOfMonth or StartOfYear.
49 public String start = "";
52 * Query start date offset expressed as an ISO 8601 duration.
54 public String offset = "";
57 * Check if any filter values are provided.
59 * @return true if either charge type codes, notes or query start date is provided.
61 public boolean hasAnyFilterOverrides() {
62 return !chargeTypeCodes.isBlank() || !notes.isBlank() || !start.isBlank();
66 * Get parsed set of charge type codes from comma-separated string.
68 * @return Set of charge type codes.
70 public Set<ChargeTypeCode> getChargeTypeCodes() {
71 return chargeTypeCodes.isBlank() ? new HashSet<>()
72 : new HashSet<ChargeTypeCode>(
73 Arrays.stream(chargeTypeCodes.split(",")).map(ChargeTypeCode::new).toList());
77 * Get parsed set of notes from comma-separated string.
79 * @return Set of notes.
81 public Set<String> getNotes() {
82 return notes.isBlank() ? new HashSet<>() : new HashSet<String>(Arrays.asList(notes.split(",")));
86 * Get query start parameter.
88 * @return null if invalid, otherwise an initialized {@link DateQueryParameter}.
90 public @Nullable DateQueryParameter getStart() {
91 if (start.isBlank()) {
92 return DateQueryParameter.EMPTY;
94 Duration durationOffset = Duration.ZERO;
95 if (!offset.isBlank()) {
97 durationOffset = Duration.parse(offset);
98 } catch (DateTimeParseException e) {
102 if (start.equals(DateQueryParameterType.START_OF_DAY.toString())) {
103 return DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, durationOffset);
105 if (start.equals(DateQueryParameterType.START_OF_MONTH.toString())) {
106 return DateQueryParameter.of(DateQueryParameterType.START_OF_MONTH, durationOffset);
108 if (start.equals(DateQueryParameterType.START_OF_YEAR.toString())) {
109 return DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR, durationOffset);
112 return DateQueryParameter.of(LocalDate.parse(start));
113 } catch (DateTimeParseException e) {