|
| 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.frenchgovtenergydata.internal.dto; |
| 14 | + |
| 15 | +import java.time.LocalDate; |
| 16 | +import java.time.ZoneOffset; |
| 17 | +import java.time.ZonedDateTime; |
| 18 | +import java.time.format.DateTimeFormatter; |
| 19 | +import java.time.format.DateTimeParseException; |
| 20 | + |
| 21 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 22 | +import org.eclipse.jdt.annotation.Nullable; |
| 23 | + |
| 24 | +/** |
| 25 | + * The {@link Tariff} is the base class holding common information for Tariffs |
| 26 | + * |
| 27 | + * @author Gaël L'hopital - Initial contribution |
| 28 | + */ |
| 29 | +@NonNullByDefault |
| 30 | +public class Tariff { |
| 31 | + protected static final DateTimeFormatter TARIFF_DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy"); |
| 32 | + |
| 33 | + protected final String[] values; |
| 34 | + public final ZonedDateTime dateDebut; |
| 35 | + public final @Nullable ZonedDateTime dateFin; |
| 36 | + public final int puissance; |
| 37 | + public final double fixeHT; |
| 38 | + public final double fixeTTC; |
| 39 | + |
| 40 | + public Tariff(String line, int lenControl) { |
| 41 | + this.values = line.replace(',', '.').split(";"); |
| 42 | + if (values.length == lenControl) { |
| 43 | + try { |
| 44 | + this.dateDebut = LocalDate.parse(values[0], TARIFF_DATE_FORMAT).atStartOfDay(ZoneOffset.UTC); |
| 45 | + this.dateFin = !values[1].isEmpty() |
| 46 | + ? LocalDate.parse(values[1], TARIFF_DATE_FORMAT).atStartOfDay(ZoneOffset.UTC) |
| 47 | + : null; |
| 48 | + this.puissance = Integer.parseInt(values[2]); |
| 49 | + this.fixeHT = Double.parseDouble(values[3]); |
| 50 | + this.fixeTTC = Double.parseDouble(values[4]); |
| 51 | + } catch (NumberFormatException | DateTimeParseException e) { |
| 52 | + throw new IllegalArgumentException("Incorrect data in '%s'".formatted(line), e); |
| 53 | + } |
| 54 | + } else { |
| 55 | + throw new IllegalArgumentException("Unexpected number of data, %d expected".formatted(lenControl)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public boolean isActive() { |
| 60 | + return dateFin == null; |
| 61 | + } |
| 62 | +} |
0 commit comments