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.dwdpollenflug.internal.dto;
15 import static org.openhab.binding.dwdpollenflug.internal.DWDPollenflugBindingConstants.*;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashMap;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.core.library.types.DateTimeType;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
33 import com.google.gson.annotations.SerializedName;
36 * The {@link DWDPollenflug} class is internal DWD data structure.
38 * @author Johannes Ott - Initial contribution
41 public class DWDPollenflug {
42 private String sender = "";
44 private String name = "";
46 private final Date created = new Date();
48 @SerializedName("next_update")
49 private @Nullable String nextUpdate;
51 @SerializedName("last_update")
52 private @Nullable String lastUpdate;
54 @SerializedName("content")
55 private @Nullable Set<DWDRegion> regions;
57 public Map<String, String> getProperties() {
58 Map<String, String> map = new HashMap<>();
60 map.put(PROPERTY_NAME, name);
61 map.put(PROPERTY_SENDER, sender);
63 return Collections.unmodifiableMap(map);
66 public Map<String, State> getChannelsStateMap() {
67 Map<String, State> map = new HashMap<>();
69 map.put(CHANNEL_UPDATES + "#" + CHANNEL_REFRESHED, parseDate(created));
70 map.put(CHANNEL_UPDATES + "#" + CHANNEL_LAST_UPDATE, parseDate(lastUpdate));
71 map.put(CHANNEL_UPDATES + "#" + CHANNEL_NEXT_UPDATE, parseDate(nextUpdate));
73 return Collections.unmodifiableMap(map);
76 private State parseDate(final @Nullable String dateString) {
78 if (dateString != null) {
79 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
80 Date date = formatter.parse(dateString.replace("Uhr", "").trim());
81 return parseDate(date);
84 return UnDefType.NULL;
85 } catch (ParseException e) {
86 return UnDefType.NULL;
90 private State parseDate(final @Nullable Date date) {
92 return UnDefType.NULL;
94 ZonedDateTime zoned = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
95 return new DateTimeType(zoned);
99 public @Nullable DWDRegion getRegion(int key) {
100 final Set<DWDRegion> localRegions = regions;
101 if (localRegions != null) {
102 for (DWDRegion region : localRegions) {
103 if (region.getRegionID() == key) {