-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
traffic-a22-elaboration: use SenortypeUtil for sensor_type meatadata
- Loading branch information
Showing
3 changed files
with
306 additions
and
95 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...tors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SensorTypeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-FileCopyrightText: NOI Techpark <[email protected]> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package it.bz.noi.a22elaborations; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import it.bz.idm.bdp.dto.StationDto; | ||
import it.bz.idm.bdp.dto.StationList; | ||
|
||
@Component | ||
public class SensorTypeUtil { | ||
|
||
private Logger logger = LoggerFactory.getLogger(SensorTypeUtil.class); | ||
|
||
private Map<String, String> sensorTypeByStation; | ||
|
||
public void addSensorTypeMetadata(StationList stations) { | ||
if (sensorTypeByStation == null) { | ||
initializeMap(); | ||
} | ||
for (StationDto station : stations) { | ||
String sensorType = sensorTypeByStation.getOrDefault(station.getId(), null); | ||
if (sensorType != null && !sensorType.isEmpty()) { | ||
station.getMetaData().put("sensor_type", sensorType.trim()); | ||
} else { | ||
logger.info("Station with code {} not found in sensor-type-mapping.csv", station.getId()); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void initializeMap() { | ||
// read sensor type <--> station code mapping from csv | ||
sensorTypeByStation = new HashMap<>(); | ||
|
||
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); | ||
InputStream inputStream = classloader.getResourceAsStream("sensor-type-mapping.csv"); | ||
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); | ||
|
||
BufferedReader br = new BufferedReader(streamReader); | ||
String line; | ||
try { | ||
while ((line = br.readLine()) != null) { | ||
String[] values = line.split(","); | ||
String code = values[0]; | ||
String sensorType = values[1]; | ||
sensorTypeByStation.put(code, sensorType); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Error while reading sensor-type-mapping.csv"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.