diff --git a/.gitignore b/.gitignore index 9154f4c..9012474 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,10 @@ hs_err_pid* replay_pid* +.DS_Store + +.idea +.vscode + +target + diff --git a/README.md b/README.md deleted file mode 100644 index 586d1b5..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# weather_service - diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..5aee1e2 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,5 @@ +# Springboot weather service + + +#### The _URI_ to access the current weather data: +https://group-4-75.pvt.dsv.su.se/weather-0.0.1-SNAPSHOT/weather diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b0d51df --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.smhi + weather + 0.0.1-SNAPSHOT + war + weather + Collecting current weather data in Stockholm + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/smhi/weather/ServletInitializer.java b/src/main/java/com/smhi/weather/ServletInitializer.java new file mode 100644 index 0000000..5bc6f8e --- /dev/null +++ b/src/main/java/com/smhi/weather/ServletInitializer.java @@ -0,0 +1,13 @@ +package com.smhi.weather; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(WeatherApplication.class); + } + +} diff --git a/src/main/java/com/smhi/weather/WeatherApplication.java b/src/main/java/com/smhi/weather/WeatherApplication.java new file mode 100644 index 0000000..c05cd39 --- /dev/null +++ b/src/main/java/com/smhi/weather/WeatherApplication.java @@ -0,0 +1,13 @@ +package com.smhi.weather; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WeatherApplication { + + public static void main(String[] args) { + SpringApplication.run(WeatherApplication.class, args); + } + +} diff --git a/src/main/java/com/smhi/weather/controllers/MainController.java b/src/main/java/com/smhi/weather/controllers/MainController.java new file mode 100644 index 0000000..48afa48 --- /dev/null +++ b/src/main/java/com/smhi/weather/controllers/MainController.java @@ -0,0 +1,33 @@ +package com.smhi.weather.controllers; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +import com.smhi.weather.services.OpenWeatherService; +import com.smhi.weather.vo.OpenResponce; + + +@RestController +public class MainController { + + final OpenWeatherService weatherService; + public MainController(OpenWeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/weather") + public OpenResponce index() { + System.out.println("Open responce:"); + try { + return weatherService.getWeatherInfo(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/src/main/java/com/smhi/weather/services/OpenWeatherService.java b/src/main/java/com/smhi/weather/services/OpenWeatherService.java new file mode 100644 index 0000000..0e2c863 --- /dev/null +++ b/src/main/java/com/smhi/weather/services/OpenWeatherService.java @@ -0,0 +1,82 @@ +package com.smhi.weather.services; + +import com.smhi.weather.vo.*; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.time.Duration; + +import static java.time.temporal.ChronoUnit.SECONDS; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.URI; +import java.net.URISyntaxException; + +import com.fasterxml.jackson.databind.ObjectMapper; + +@Service +public class OpenWeatherService { + + private final String WHAT = "mesan1g"; + private final float LON = 18.0686f; + private final float LAT = 59.3293f; + + public OpenResponce getWeatherInfo() throws IOException, InterruptedException { + System.out.println("getWeatherInfo:"); + String uri = buildURI(WHAT, LON, LAT); + System.out.println("URI: " + uri); + + HttpRequest request = null; + try { + request = HttpRequest.newBuilder() + .uri(new URI(uri)) + .timeout(Duration.of(10, SECONDS)) + .GET() + .build(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + HttpResponse responce = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println("Responce.body: " + responce.body()); + + ObjectMapper objectMapper = new ObjectMapper(); + com.smhi.weather.vo.Root root = objectMapper.readValue(responce.body(), com.smhi.weather.vo.Root.class); + return buildResult(root); + } + + private OpenResponce buildResult(Root root) { + OpenResponce responce = new OpenResponce(); + for (TimeSeries ts : root.timeSeries) { + for (Parameter p : ts.parameters) { + if (p.getName().equalsIgnoreCase("Wsymb2")) { + DataVO dvo = new DataVO(); + dvo.setWsymb2(p.getValues().get(0).intValue()); + responce.setDataVO(dvo); + return responce; + } + } + } + return responce; + } + + + private String buildURI(String what, float lon, float lat) { + StringBuilder sb = new StringBuilder(); + sb.append("https://opendata-download-metanalys.smhi.se") + .append("/api/category/").append(what) + .append("/version/2") + .append("/geotype/point/lon/").append(lon) + .append("/lat/").append(lat) + .append("/data.json"); + + return sb.toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/smhi/weather/vo/DataVO.java b/src/main/java/com/smhi/weather/vo/DataVO.java new file mode 100644 index 0000000..d00b812 --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/DataVO.java @@ -0,0 +1,25 @@ +package com.smhi.weather.vo; + +import java.io.Serializable; + +public class DataVO implements Serializable { + + int Wsymb2; + + public DataVO() {} + + public int getWsymb2() { + return Wsymb2; + } + + public void setWsymb2(int wsymb2) { + Wsymb2 = wsymb2; + } + + @Override + public String toString() { + return "DataVO{" + + "Wsymb2=" + Wsymb2 + + '}'; + } +} diff --git a/src/main/java/com/smhi/weather/vo/Geometry.java b/src/main/java/com/smhi/weather/vo/Geometry.java new file mode 100644 index 0000000..3458475 --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/Geometry.java @@ -0,0 +1,34 @@ +package com.smhi.weather.vo; + +import java.util.ArrayList; + +public class Geometry { + public String type; + public ArrayList> coordinates; + + public Geometry() {} + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public ArrayList> getCoordinates() { + return coordinates; + } + + public void setCoordinates(ArrayList> coordinates) { + this.coordinates = coordinates; + } + + @Override + public String toString() { + return "Geometry{" + + "type='" + type + '\'' + + ", coordinates=" + coordinates + + '}'; + } +} diff --git a/src/main/java/com/smhi/weather/vo/OpenResponce.java b/src/main/java/com/smhi/weather/vo/OpenResponce.java new file mode 100644 index 0000000..d930163 --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/OpenResponce.java @@ -0,0 +1,26 @@ +package com.smhi.weather.vo; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonInclude; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class OpenResponce implements Serializable{ + + private com.smhi.weather.vo.DataVO dataVO; + + public DataVO getDataVO() { + return dataVO; + } + + public void setDataVO(DataVO dataVO) { + this.dataVO = dataVO; + } + + @Override + public String toString() { + return "OpenResponce{" + + "dataVO=" + dataVO + + '}'; + } +} diff --git a/src/main/java/com/smhi/weather/vo/Parameter.java b/src/main/java/com/smhi/weather/vo/Parameter.java new file mode 100644 index 0000000..d8b36b5 --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/Parameter.java @@ -0,0 +1,68 @@ +package com.smhi.weather.vo; + +import java.util.ArrayList; + +public class Parameter{ + + public Parameter() {} + + public String name; + public String levelType; + public int level; + public String unit; + public ArrayList values; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLevelType() { + return levelType; + } + + public void setLevelType(String levelType) { + this.levelType = levelType; + } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public ArrayList getValues() { + return values; + } + + public void setValues(ArrayList values) { + this.values = values; + } + + @Override + public String toString() { + return "Parameter{" + + "name='" + name + '\'' + + ", levelType='" + levelType + '\'' + + ", level=" + level + + ", unit='" + unit + '\'' + + ", values=" + values + + '}'; + } +} + + diff --git a/src/main/java/com/smhi/weather/vo/Result.java b/src/main/java/com/smhi/weather/vo/Result.java new file mode 100644 index 0000000..89c579e --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/Result.java @@ -0,0 +1,27 @@ +package com.smhi.weather.vo; + +import java.io.Serializable; +import java.util.ArrayList; + +public class Result implements Serializable { + + private ArrayList data = new ArrayList<>(); + + public Result() {} + + public ArrayList getData() { + return data; + } + + public void setData(ArrayList data) { + this.data = data; + } + + public void add(DataVO dvo) { + data.add(dvo); + } + + +} + + diff --git a/src/main/java/com/smhi/weather/vo/Root.java b/src/main/java/com/smhi/weather/vo/Root.java new file mode 100644 index 0000000..2f38032 --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/Root.java @@ -0,0 +1,56 @@ +package com.smhi.weather.vo; + +import java.util.ArrayList; +import java.util.Date; + +public class Root{ + + public Date approvedTime; + public Date referenceTime; + public Geometry geometry; + public ArrayList timeSeries; + + public Root() {} + + public Date getApprovedTime() { + return approvedTime; + } + + public void setApprovedTime(Date approvedTime) { + this.approvedTime = approvedTime; + } + + public Date getReferenceTime() { + return referenceTime; + } + + public void setReferenceTime(Date referenceTime) { + this.referenceTime = referenceTime; + } + + public Geometry getGeometry() { + return geometry; + } + + public void setGeometry(Geometry geometry) { + this.geometry = geometry; + } + + public ArrayList getTimeSeries() { + return timeSeries; + } + + public void setTimeSeries(ArrayList timeSeries) { + this.timeSeries = timeSeries; + } + + @Override + public String toString() { + return "Root{" + + "approvedTime=" + approvedTime + + ", referenceTime=" + referenceTime + + ", geometry=" + geometry + + ", timeSeries=" + timeSeries + + '}'; + } +} diff --git a/src/main/java/com/smhi/weather/vo/TimeSeries.java b/src/main/java/com/smhi/weather/vo/TimeSeries.java new file mode 100644 index 0000000..d9bd90e --- /dev/null +++ b/src/main/java/com/smhi/weather/vo/TimeSeries.java @@ -0,0 +1,37 @@ +package com.smhi.weather.vo; + +import java.util.ArrayList; +import java.util.Date; + +public class TimeSeries{ + public Date validTime; + public ArrayList parameters; + + public TimeSeries() {} + + public Date getValidTime() { + return validTime; + } + + public void setValidTime(Date validTime) { + this.validTime = validTime; + } + + public ArrayList getParameters() { + return parameters; + } + + public void setParameters(ArrayList parameters) { + this.parameters = parameters; + } + + @Override + public String toString() { + return "TimeSeries{" + + "validTime=" + validTime + + ", parameters=" + parameters + + '}'; + } +} + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/test/java/com/smhi/weather/OpenWeatherServiceTest.java b/src/test/java/com/smhi/weather/OpenWeatherServiceTest.java new file mode 100644 index 0000000..ec0f763 --- /dev/null +++ b/src/test/java/com/smhi/weather/OpenWeatherServiceTest.java @@ -0,0 +1,12 @@ +package com.smhi.weather; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class OpenWeatherServiceTest { + + @Test + void getWeatherInfo() { + } +} \ No newline at end of file diff --git a/src/test/java/com/smhi/weather/WeatherApplicationTests.java b/src/test/java/com/smhi/weather/WeatherApplicationTests.java new file mode 100644 index 0000000..c5127a8 --- /dev/null +++ b/src/test/java/com/smhi/weather/WeatherApplicationTests.java @@ -0,0 +1,13 @@ +package com.smhi.weather; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class WeatherApplicationTests { + + @Test + void contextLoads() { + } + +}