diff --git a/src/main/java/no/nibio/vips/logic/service/LogicService.java b/src/main/java/no/nibio/vips/logic/service/LogicService.java index 2bf2f79d53e29d7d3d1660ac4d2dc2f89a61b856..ffd2d14c5bb4ec44c85c94027057245293dec13f 100644 --- a/src/main/java/no/nibio/vips/logic/service/LogicService.java +++ b/src/main/java/no/nibio/vips/logic/service/LogicService.java @@ -22,6 +22,8 @@ package no.nibio.vips.logic.service; import com.ibm.icu.util.ULocale; import java.util.TimeZone; import de.micromata.opengis.kml.v_2_2_0.Kml; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -66,6 +68,7 @@ import no.nibio.vips.util.CSVPrintUtil; import no.nibio.vips.util.ServletUtil; import no.nibio.vips.util.SolarRadiationUtil; import no.nibio.vips.util.weather.ALabDataParser; +import no.nibio.vips.util.weather.FruitWebDavisDataParser; import no.nibio.vips.util.weather.MetosDataParser; import no.nibio.vips.util.weather.ParseWeatherDataException; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; @@ -537,6 +540,35 @@ public class LogicService { return Response.ok().entity(observations).build(); } + @GET + @POST + @Path("weather/proxy/fruitwebdavis/{stationId}") + @Produces("application/json;charset=UTF-8") + public Response getFruitWebDavisWeatherData( + @PathParam("stationId") String stationId, + @FormParam("timeZone") String timeZonePOST, + @QueryParam("timeZone") String timeZoneGET, + @FormParam("startDate") String startDatePOST, + @QueryParam("startDate") String startDateGET + ) + { + List<WeatherObservation> observations; + try + { + String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; + TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + format.setTimeZone(timeZone); + String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; + Date startDate1 = format.parse(startDateParam); + observations = new FruitWebDavisDataParser().getWeatherObservations(URLDecoder.decode(stationId, "UTF-8"), timeZone, startDate1); + } catch (ParseException | ParseWeatherDataException | NullPointerException | UnsupportedEncodingException ex) { + return Response.serverError().entity(ex).build(); + } + return Response.ok().entity(observations).build(); + } + + @GET @POST @Path("weather/proxy/alab/{stationId}") diff --git a/src/main/java/no/nibio/vips/logic/util/Globals.java b/src/main/java/no/nibio/vips/logic/util/Globals.java index 3e66ae872e4e97398820f786c2a1e449373c8e4f..7ef168ef4999efb360cde061dc251f82463aba17 100644 --- a/src/main/java/no/nibio/vips/logic/util/Globals.java +++ b/src/main/java/no/nibio/vips/logic/util/Globals.java @@ -76,7 +76,9 @@ public class Globals { "Europe/Sarajevo", "Europe/Stockholm", "Europe/Helsinki", + "Europe/Riga", "Europe/Sofia" + }; public static int DEFAULT_UUID_VALIDITY_DURATION_DAYS = 30; diff --git a/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java b/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java index 50fb663d5e1965e108518e43b36db312f158134b..8f0c239ef2be7ffa0155d9faf41c074ed32f48ae 100644 --- a/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java +++ b/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java @@ -22,6 +22,7 @@ package no.nibio.vips.util.weather; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.net.URISyntaxException; import java.net.URL; import java.text.MessageFormat; import java.text.ParseException; @@ -71,9 +72,9 @@ public class FruitWebDavisDataParser { String[] headers; Map<Integer, Integer> elementOrdering = new HashMap<>(); try { - URL metosURL = new URL(MessageFormat.format(FruitWebDavisDataParser.FRUITWEB_URL_TEMPLATE, stationID,urlDFormat.format(startDate))); + URL fruitwebDavisURL = new URL(MessageFormat.format(FruitWebDavisDataParser.FRUITWEB_URL_TEMPLATE, stationID,urlDFormat.format(startDate))); BufferedReader in = new BufferedReader( - new InputStreamReader(metosURL.openStream())); + new InputStreamReader(fruitwebDavisURL.openStream())); String inputLine; Date testTimestamp; @@ -82,6 +83,7 @@ public class FruitWebDavisDataParser { // If the resolution is 30 minutes or 1 hour while ((inputLine = in.readLine()) != null) { + //System.out.println(inputLine); String[] lineData = inputLine.split(";"); // Skip empty lines if(lineData.length <= 1) @@ -124,11 +126,12 @@ public class FruitWebDavisDataParser { // Data comes in half-hour chunks (resolution = 30 minutes) + // Unlike Metos, the hour starts at :30 if(logIntervalId.equals(WeatherObservation.LOG_INTERVAL_ID_30M)) { - Boolean shouldBe00Now = true; - String[] data00 = null; + Boolean shouldBe30Now = true; String[] data30 = null; + String[] data00 = null; Date timestamp = null; for(String[] lineData: data) { @@ -140,7 +143,14 @@ public class FruitWebDavisDataParser { { continue; } - else if(lineData[1].split(":")[1].equals("00") && shouldBe00Now) + else if(lineData[1].split(":")[1].equals("30") && shouldBe30Now) + { + data30 = lineData; + + shouldBe30Now = false; + continue; // So that we summarize only after :00 data has been set too + } + else if(lineData[1].split(":")[1].equals("00") && !shouldBe30Now) { data00 = lineData; try @@ -151,20 +161,14 @@ public class FruitWebDavisDataParser { { throw new ParseWeatherDataException("Error with time stamp in weather data from Davis/FruitWeb station: " + ex.getMessage()); } - shouldBe00Now = false; - continue; // So that we summarize only after :30 data has been set too - } - else if(lineData[1].split(":")[1].equals("30") && !shouldBe00Now) - { - data30 = lineData; - shouldBe00Now = true; + shouldBe30Now = true; } else { - throw new ParseWeatherDataException("Doesn't make sense!"); + throw new ParseWeatherDataException("Doesn't make sense at " + lineData[0] + " " + lineData[1] + "!"); } - for(Integer i=2;i<data00.length;i++) + for(Integer i=2;i<data30.length;i++) { Double aggregateValue = null; Double value00 = Double.valueOf(data00[i].replaceAll(",",".")); diff --git a/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java index 0b2887c32676cdd8d4413dc30e856a20dd2fc239..b8d5b30074d1458d4c04361c260a6e3e4d73ffcf 100644 --- a/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java +++ b/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java @@ -69,10 +69,10 @@ public class FruitWebDavisDataParserTest { FruitWebDavisDataParser instance = new FruitWebDavisDataParser(); List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate); - for(WeatherObservation obs:result) + /*for(WeatherObservation obs:result) { System.out.println(obs); - } + }*/ assertNotNull( result); }