]> git.basschouten.com Git - openhab-addons.git/blob
26ccc703c6c08883337d772a9a5c298b14666a49
[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.fmiweather.internal.client;
14
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.time.format.DateTimeFormatter;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Request class for FIM weather
24  *
25  * @author Sami Salonen - Initial contribution
26  *
27  */
28 @NonNullByDefault
29 public class Request {
30
31     public static final String FMI_WFS_URL = "https://opendata.fmi.fi/wfs";
32
33     public final QueryParameter location;
34     public final long startEpoch;
35     public final long endEpoch;
36     public final long timestepMinutes;
37     public final String storedQueryId;
38     public final String[] parameters;
39
40     private static ZoneId UTC = ZoneId.of("Z");
41     private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
42
43     public Request(String storedQueryId, QueryParameter location, long startEpoch, long endEpoch,
44             long timestepMinutes) {
45         this(storedQueryId, location, startEpoch, endEpoch, timestepMinutes, new String[0]);
46     }
47
48     public Request(String storedQueryId, QueryParameter location, long startEpoch, long endEpoch, long timestepMinutes,
49             String[] parameters) {
50         this.storedQueryId = storedQueryId;
51         this.location = location;
52         this.startEpoch = startEpoch;
53         this.endEpoch = endEpoch;
54         this.timestepMinutes = timestepMinutes;
55         this.parameters = parameters;
56     }
57
58     public String toUrl() {
59         StringBuilder urlBuilder = new StringBuilder(FMI_WFS_URL)
60                 .append("?service=WFS&version=2.0.0&request=getFeature&storedquery_id=").append(storedQueryId)
61                 .append("&starttime=").append(epochToIsoDateTime(startEpoch)).append("&endtime=")
62                 .append(epochToIsoDateTime(endEpoch)).append("&timestep=").append(timestepMinutes);
63         location.toRequestParameters().forEach(entry -> {
64             urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
65         });
66         if (parameters.length > 0) {
67             urlBuilder.append("&").append("parameters=").append(String.join(",", parameters));
68         }
69         return urlBuilder.toString();
70     }
71
72     /**
73      * Convert epoch value (representing UTC time) to ISO formatted date time
74      *
75      * @param epoch
76      * @return
77      */
78     private static String epochToIsoDateTime(long epoch) {
79         return ZonedDateTime.ofInstant(Instant.ofEpochSecond(epoch), UTC).format(FORMATTER);
80     }
81 }