base of weather service #1
7
.gitignore
vendored
7
.gitignore
vendored
@ -24,3 +24,10 @@
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
|
||||
.DS_Store
|
||||
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
target
|
||||
|
||||
|
5
ReadMe.md
Normal file
5
ReadMe.md
Normal file
@ -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
|
61
pom.xml
Normal file
61
pom.xml
Normal file
@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.7</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.smhi</groupId>
|
||||
<artifactId>weather</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>weather</name>
|
||||
<description>Collecting current weather data in Stockholm</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.13.2</version>
|
||||
</dependency>
|
||||
|
||||
https://mvnrepository.com/artifact/commons-io/commons-io
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
13
src/main/java/com/smhi/weather/ServletInitializer.java
Normal file
13
src/main/java/com/smhi/weather/ServletInitializer.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
13
src/main/java/com/smhi/weather/WeatherApplication.java
Normal file
13
src/main/java/com/smhi/weather/WeatherApplication.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<String> 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();
|
||||
}
|
||||
|
||||
}
|
25
src/main/java/com/smhi/weather/vo/DataVO.java
Normal file
25
src/main/java/com/smhi/weather/vo/DataVO.java
Normal file
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
34
src/main/java/com/smhi/weather/vo/Geometry.java
Normal file
34
src/main/java/com/smhi/weather/vo/Geometry.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.smhi.weather.vo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Geometry {
|
||||
public String type;
|
||||
public ArrayList<ArrayList<Double>> coordinates;
|
||||
|
||||
public Geometry() {}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<Double>> getCoordinates() {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
public void setCoordinates(ArrayList<ArrayList<Double>> coordinates) {
|
||||
this.coordinates = coordinates;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Geometry{" +
|
||||
"type='" + type + '\'' +
|
||||
", coordinates=" + coordinates +
|
||||
'}';
|
||||
}
|
||||
}
|
26
src/main/java/com/smhi/weather/vo/OpenResponce.java
Normal file
26
src/main/java/com/smhi/weather/vo/OpenResponce.java
Normal file
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
68
src/main/java/com/smhi/weather/vo/Parameter.java
Normal file
68
src/main/java/com/smhi/weather/vo/Parameter.java
Normal file
@ -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<Double> 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<Double> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public void setValues(ArrayList<Double> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Parameter{" +
|
||||
"name='" + name + '\'' +
|
||||
", levelType='" + levelType + '\'' +
|
||||
", level=" + level +
|
||||
", unit='" + unit + '\'' +
|
||||
", values=" + values +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
27
src/main/java/com/smhi/weather/vo/Result.java
Normal file
27
src/main/java/com/smhi/weather/vo/Result.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.smhi.weather.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Result implements Serializable {
|
||||
|
||||
private ArrayList<DataVO> data = new ArrayList<>();
|
||||
|
||||
public Result() {}
|
||||
|
||||
public ArrayList<DataVO> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(ArrayList<DataVO> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void add(DataVO dvo) {
|
||||
data.add(dvo);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
56
src/main/java/com/smhi/weather/vo/Root.java
Normal file
56
src/main/java/com/smhi/weather/vo/Root.java
Normal file
@ -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> 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<TimeSeries> getTimeSeries() {
|
||||
return timeSeries;
|
||||
}
|
||||
|
||||
public void setTimeSeries(ArrayList<TimeSeries> timeSeries) {
|
||||
this.timeSeries = timeSeries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Root{" +
|
||||
"approvedTime=" + approvedTime +
|
||||
", referenceTime=" + referenceTime +
|
||||
", geometry=" + geometry +
|
||||
", timeSeries=" + timeSeries +
|
||||
'}';
|
||||
}
|
||||
}
|
37
src/main/java/com/smhi/weather/vo/TimeSeries.java
Normal file
37
src/main/java/com/smhi/weather/vo/TimeSeries.java
Normal file
@ -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<Parameter> parameters;
|
||||
|
||||
public TimeSeries() {}
|
||||
|
||||
public Date getValidTime() {
|
||||
return validTime;
|
||||
}
|
||||
|
||||
public void setValidTime(Date validTime) {
|
||||
this.validTime = validTime;
|
||||
}
|
||||
|
||||
public ArrayList<Parameter> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(ArrayList<Parameter> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TimeSeries{" +
|
||||
"validTime=" + validTime +
|
||||
", parameters=" + parameters +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
||||
|
12
src/test/java/com/smhi/weather/OpenWeatherServiceTest.java
Normal file
12
src/test/java/com/smhi/weather/OpenWeatherServiceTest.java
Normal file
@ -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() {
|
||||
}
|
||||
}
|
13
src/test/java/com/smhi/weather/WeatherApplicationTests.java
Normal file
13
src/test/java/com/smhi/weather/WeatherApplicationTests.java
Normal file
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user