Skip to content

Commit

Permalink
traffic-a22-forecast: forecast mapping is now correct
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed Sep 25, 2023
1 parent 3c04d9c commit 26e9120
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -88,18 +90,39 @@ public void syncData(YearMonth from, YearMonth to) {

StationList stations = new StationList();

List<ForecastMap> 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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ public static class TrafficData {

@JsonProperty("Nord")
@JsonIgnoreProperties("Tipo")
public Map<String, Values> north;
public Map<String, TrafficValues> north;

@JsonProperty("Sud")
@JsonIgnoreProperties("Tipo")
public Map<String, Values> south;
public Map<String, TrafficValues> 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, TrafficValues>> {

Map<String, Map<String, Values>> 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<String, Values> valuesByDate = new HashMap<>();
valuesByDate.put(data.date, data.south.get(key));
map.put(key + " Sud", valuesByDate);
}

for (String key : data.north.keySet()) {
Map<String, Values> 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<String, TrafficValues> data, String direction) {
data.entrySet().forEach((entry) -> {
String key = entry.getKey() + " " + direction;
Map<String, TrafficValues> map = get(key);
if (map == null)
map = new HashMap<>();
map.put(date.toString(), data.get(entry.getKey()));
put(key, map);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Coordinate> map;
// Maps the km string to the corresponding coordinate
// example '11' -> 42.23287,11.232323
public class TollBothCoordinatesMap extends HashMap<String, Coordinate> {

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> map;
// Maps the toll booth name to the corresponding km
// example 'Bolzano Sud' -> '123'
public class TollBothMap extends HashMap<String, String> {

public TollBothMap(TollBoothDto dto) {
map = new HashMap<>();
for (TollBoothData data : dto.data) {
map.put(data.nameIT, data.km);
put(data.nameIT, data.km);
}
}
}

0 comments on commit 26e9120

Please sign in to comment.