]> git.basschouten.com Git - openhab-addons.git/blob
86938610b5c3ab44ad473f279c5b41b4cbc51927
[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.dwdpollenflug.internal.dto;
14
15 import static org.openhab.binding.dwdpollenflug.internal.DWDPollenflugBindingConstants.*;
16
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;
24 import java.util.Map;
25 import java.util.Set;
26
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;
32
33 import com.google.gson.annotations.SerializedName;
34
35 /**
36  * The {@link DWDPollenflug} class is internal DWD data structure.
37  *
38  * @author Johannes Ott - Initial contribution
39  */
40 @NonNullByDefault
41 public class DWDPollenflug {
42     private String sender = "";
43
44     private String name = "";
45
46     private final Date created = new Date();
47
48     @SerializedName("next_update")
49     private @Nullable String nextUpdate;
50
51     @SerializedName("last_update")
52     private @Nullable String lastUpdate;
53
54     @SerializedName("content")
55     private @Nullable Set<DWDRegion> regions;
56
57     public Map<String, String> getProperties() {
58         Map<String, String> map = new HashMap<>();
59
60         map.put(PROPERTY_NAME, name);
61         map.put(PROPERTY_SENDER, sender);
62
63         return Collections.unmodifiableMap(map);
64     }
65
66     public Map<String, State> getChannelsStateMap() {
67         Map<String, State> map = new HashMap<>();
68
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));
72
73         return Collections.unmodifiableMap(map);
74     }
75
76     private State parseDate(final @Nullable String dateString) {
77         try {
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);
82             }
83
84             return UnDefType.NULL;
85         } catch (ParseException e) {
86             return UnDefType.NULL;
87         }
88     }
89
90     private State parseDate(final @Nullable Date date) {
91         if (date == null) {
92             return UnDefType.NULL;
93         } else {
94             ZonedDateTime zoned = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
95             return new DateTimeType(zoned);
96         }
97     }
98
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) {
104                     return region;
105                 }
106             }
107         }
108
109         return null;
110     }
111 }