diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java index fe48e90a..6e875fa8 100644 --- a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java @@ -19,12 +19,14 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import com.opendatahub.traffic.a22.forecast.mapping.Coordinate; import com.opendatahub.traffic.a22.forecast.mapping.ForecastMap; import com.opendatahub.traffic.a22.forecast.mapping.TollBothCoordinatesMap; import com.opendatahub.traffic.a22.forecast.mapping.TollBothMap; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficData; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficDataLine; +import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficValue; import com.opendatahub.traffic.a22.forecast.services.A22Service; import com.opendatahub.traffic.a22.forecast.services.OdhClient; @@ -88,18 +90,39 @@ public void syncData(YearMonth from, YearMonth to) { StationList stations = new StationList(); - List forecastMaps = new ArrayList<>(); + ForecastMap forecastMap = new ForecastMap(); TollBothMap tollBothMap = new TollBothMap(a22Service.getTollBooths()); TollBothCoordinatesMap coordinateMap = new TollBothCoordinatesMap(a22Service.getCoordinates()); - while (from.isBefore(to)) { - forecastMaps.add(new ForecastMap(a22Service.getForecasts(from))); - from = from.plusMonths(1); - } + // get forecast data + for (YearMonth i = from; i.isBefore(to); i = i.plusMonths(1)) + forecastMap.add(a22Service.getForecasts(i), i); // sync with Open Data Hub + for (String toolBoothName : forecastMap.keySet()) { + Coordinate coordinate = coordinateMap.get("10"); + + StationDto stationDto = new StationDto(toolBoothName, toolBoothName, coordinate.latitude, + coordinate.latitude); + stations.add(stationDto); + } - LOG.info("Sync done. Imported {} months.", forecastMaps.size()); + LOG.info("Sync done. Imported {} months.", forecastMap.size()); + } + + private String mapTrafficValue(TrafficValue value) { + switch (value.type) { + case 0: + return "regular"; + case 1: + return "heavy"; + case 2: + return "severe"; + case 3: + return "critical"; + default: + return "not defined"; + } } } diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java index 010b9a5f..a387b869 100644 --- a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java @@ -67,34 +67,34 @@ public static class TrafficData { @JsonProperty("Nord") @JsonIgnoreProperties("Tipo") - public Map north; + public Map north; @JsonProperty("Sud") @JsonIgnoreProperties("Tipo") - public Map south; + public Map south; } @ToString - public static class Values { + public static class TrafficValues { @JsonProperty("Max") public int max; @JsonProperty("_0_6") - public Value value0to6; + public TrafficValue value0to6; @JsonProperty("_6_12") - public Value value6to12; + public TrafficValue value6to12; @JsonProperty("_12_18") - public Value value12to18; + public TrafficValue value12to18; @JsonProperty("_18_24") - public Value value18to24; + public TrafficValue value18to24; } @ToString - public static class Value { + public static class TrafficValue { @JsonProperty("Tipo") public int type; diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/Coordinate.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/Coordinate.java new file mode 100644 index 00000000..031da636 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/Coordinate.java @@ -0,0 +1,11 @@ +package com.opendatahub.traffic.a22.forecast.mapping; + +public class Coordinate { + public double longitude; + public double latitude; + + public Coordinate(double longitude, double latitude) { + this.longitude = longitude; + this.latitude = latitude; + } +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/ForecastMap.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/ForecastMap.java index b391ff5d..c011570e 100644 --- a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/ForecastMap.java +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/ForecastMap.java @@ -4,34 +4,37 @@ package com.opendatahub.traffic.a22.forecast.mapping; +import java.time.YearMonth; import java.util.HashMap; import java.util.Map; +import java.util.Set; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficData; import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficDataLine; -import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.Values; +import com.opendatahub.traffic.a22.forecast.dto.ForecastDto.TrafficValues; -public class ForecastMap { +// Maps the toll booth name to its direction and its values by date +// example 'Bolzano Sud' -> '09-2023' -> 0,1,0,3 +public class ForecastMap extends HashMap> { - Map> map; - - public ForecastMap(ForecastDto dto) { - map = new HashMap<>(); + public void add(ForecastDto dto, YearMonth date) { for (TrafficDataLine line : dto.data.trafficDataLines) { for (TrafficData data : line.data) { - for (String key : data.south.keySet()) { - Map valuesByDate = new HashMap<>(); - valuesByDate.put(data.date, data.south.get(key)); - map.put(key + " Sud", valuesByDate); - } - - for (String key : data.north.keySet()) { - Map valuesByDate = new HashMap<>(); - valuesByDate.put(data.date, data.north.get(key)); - map.put(key + " Nord", valuesByDate); - } + putValues(date, data.south, "Sud"); + putValues(date, data.north, "Nord"); } } } + + private void putValues(YearMonth date, Map data, String direction) { + data.entrySet().forEach((entry) -> { + String key = entry.getKey() + " " + direction; + Map map = get(key); + if (map == null) + map = new HashMap<>(); + map.put(date.toString(), data.get(entry.getKey())); + put(key, map); + }); + } } diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothCoordinatesMap.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothCoordinatesMap.java index ef0a6e59..14bfe6cf 100644 --- a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothCoordinatesMap.java +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothCoordinatesMap.java @@ -5,29 +5,19 @@ package com.opendatahub.traffic.a22.forecast.mapping; import java.util.HashMap; -import java.util.Map; import com.opendatahub.traffic.a22.forecast.dto.TollBothCoordinatesDto; import com.opendatahub.traffic.a22.forecast.dto.TollBothCoordinatesDto.TollBothCoordinates; -public class TollBothCoordinatesMap { - - Map map; +// Maps the km string to the corresponding coordinate +// example '11' -> 42.23287,11.232323 +public class TollBothCoordinatesMap extends HashMap { public TollBothCoordinatesMap(TollBothCoordinatesDto dto) { - map = new HashMap<>(); for (TollBothCoordinates data : dto.data) { - map.put(data.km, new Coordinate(data.longitude, data.latitude)); + put(data.km, new Coordinate(data.longitude, data.latitude)); } } - public class Coordinate { - public double longitude; - public double latitude; - public Coordinate(double longitude, double latitude) { - this.longitude = longitude; - this.latitude = latitude; - } - } } diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothMap.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothMap.java index c30eccb5..6a58a75a 100644 --- a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothMap.java +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/mapping/TollBothMap.java @@ -5,19 +5,17 @@ package com.opendatahub.traffic.a22.forecast.mapping; import java.util.HashMap; -import java.util.Map; import com.opendatahub.traffic.a22.forecast.dto.TollBoothDto; import com.opendatahub.traffic.a22.forecast.dto.TollBoothDto.TollBoothData; -public class TollBothMap { - - Map map; +// Maps the toll booth name to the corresponding km +// example 'Bolzano Sud' -> '123' +public class TollBothMap extends HashMap { public TollBothMap(TollBoothDto dto) { - map = new HashMap<>(); for (TollBoothData data : dto.data) { - map.put(data.nameIT, data.km); + put(data.nameIT, data.km); } } }