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.fmiweather.internal.client;
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.time.format.DateTimeFormatter;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * Request class for FIM weather
25 * @author Sami Salonen - Initial contribution
29 public class Request {
31 public static final String FMI_WFS_URL = "https://opendata.fmi.fi/wfs";
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;
40 private static ZoneId UTC = ZoneId.of("Z");
41 private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
43 public Request(String storedQueryId, QueryParameter location, long startEpoch, long endEpoch,
44 long timestepMinutes) {
45 this(storedQueryId, location, startEpoch, endEpoch, timestepMinutes, new String[0]);
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;
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("×tep=").append(timestepMinutes);
63 location.toRequestParameters().forEach(entry -> {
64 urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
66 if (parameters.length > 0) {
67 urlBuilder.append("&").append("parameters=").append(String.join(",", parameters));
69 return urlBuilder.toString();
73 * Convert epoch value (representing UTC time) to ISO formatted date time
78 private static String epochToIsoDateTime(long epoch) {
79 return ZonedDateTime.ofInstant(Instant.ofEpochSecond(epoch), UTC).format(FORMATTER);