diff --git a/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SensorTypeUtil.java b/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SensorTypeUtil.java new file mode 100644 index 00000000..db4ec848 --- /dev/null +++ b/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SensorTypeUtil.java @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// 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 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"); + } + } +} diff --git a/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SyncStation.java b/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SyncStation.java index 7aab2c4e..e0a6b92a 100644 --- a/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SyncStation.java +++ b/data-collectors/traffic-a22-elaborations/src/main/java/it/bz/noi/a22elaborations/SyncStation.java @@ -4,20 +4,14 @@ 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.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; -import java.util.Map; import java.util.Properties; -import javax.annotation.PostConstruct; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -33,11 +27,12 @@ public class SyncStation { private static String origin; private static String stationtype; - private Map sensorTypeByStation; - @Autowired private A22TrafficJSONPusher pusher; + @Autowired + private SensorTypeUtil sensorTypeUtil; + // Development / Testing only public static void main(String[] args) throws IOException, SQLException { Connection connection = Utility.createConnection(); @@ -53,24 +48,6 @@ public static void main(String[] args) throws IOException, SQLException { } } - @PostConstruct - private void postConstruct() throws IOException { - // 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; - while ((line = br.readLine()) != null) { - String[] values = line.split(","); - String code = values[0]; - String sensorType = values[1]; - sensorTypeByStation.put(code, sensorType); - } - } /** * Saves all stations and data types to the bdp-core @@ -84,7 +61,8 @@ public StationList saveStations(Connection connection) throws IOException, SQLEx LOG.debug("Read stations"); StationList stationList = readStationList(connection); LOG.debug("Size stationlist: " + stationList.size()); - + // add sensor_type metadata + sensorTypeUtil.addSensorTypeMetadata(stationList); LOG.debug("Push stations"); pusher.syncStations(stationList); @@ -139,20 +117,6 @@ public StationList readStationList(Connection connection) throws IOException, SQ HashMap metadataMap = new HashMap(); String metadata = resultSet.getString("metadata"); metadataMap.put("a22_metadata", metadata); - - // add sensor type metadata field, that will be used for simplification of - // further elaborations - String[] codes = code.split(":"); - if (codes.length > 1) { - String stationId = codes[1].trim(); - String sensorType = sensorTypeByStation.getOrDefault(stationId, null); - if (sensorType != null && !sensorType.isEmpty()) { - metadataMap.put("sensor_type", sensorType.trim()); - } - } else { - LOG.info("Station with code {} has not correct format A22:station_id:sensor_id", code); - } - station.setMetaData(metadataMap); LOG.debug("Add stationDto to stationList"); stationList.add(station); @@ -160,7 +124,6 @@ public StationList readStationList(Connection connection) throws IOException, SQ } LOG.debug("Return stationlist"); return stationList; - } } diff --git a/data-collectors/traffic-a22-elaborations/src/main/resources/sensor-type-mapping.csv b/data-collectors/traffic-a22-elaborations/src/main/resources/sensor-type-mapping.csv index f864474a..77af7d26 100644 --- a/data-collectors/traffic-a22-elaborations/src/main/resources/sensor-type-mapping.csv +++ b/data-collectors/traffic-a22-elaborations/src/main/resources/sensor-type-mapping.csv @@ -1,54 +1,237 @@ CODE,SENSOR TECHNOLOGY -6023,camera -6024,camera -678,induction_loop -5414,radar -6025,camera -6026,camera -6027,camera -6028,camera -5415,radar -6029,camera -6030,camera -677,induction_loop -666,induction_loop -6031,camera -6032,camera -676,induction_loop -6033,camera -6034,camera -5416,radar -684,induction_loop -6035,camera -6036,camera -679,induction_loop -5687,induction_loop -5936,camera -5954,camera -680,induction_loop -5937,camera -5953,camera -685,radar -668,induction_loop -5807,radar -674,induction_loop -5955,camera -5938,camera -5417,radar -5418,radar -5419,radar -1886,induction_loop -5939,camera -5960,camera -889,radar -890,radar -5553,induc7ion_loop -1887,induction_loop -686,induction_loop -5958,camera -5959,camera -686,induction_loop -5438,induction_loop -5956,camera -5957,camera -683,induction_loop +A22:5416:4,radar +A22:1871:1,induction_loop +A22:1871:2,induction_loop +A22:1872:3,induction_loop +A22:1872:4,induction_loop +A22:1886:1,induction_loop +A22:1886:2,induction_loop +A22:1886:3,induction_loop +A22:1886:4,induction_loop +A22:1887:1,induction_loop +A22:1887:2,induction_loop +A22:1887:3,induction_loop +A22:1887:4,induction_loop +A22:5414:3,radar +A22:5415:2,radar +A22:5416:1,radar +A22:5416:2,radar +A22:5417:3,radar +A22:5417:4,radar +A22:5417:6,radar +A22:5418:3,radar +A22:5418:4,radar +A22:5418:6,radar +A22:5419:3,radar +A22:5419:4,radar +A22:5419:6,radar +A22:5687:1,induction_loop +A22:5687:2,induction_loop +A22:5687:3,induction_loop +A22:5687:4,induction_loop +A22:5767:1,induction_loop +A22:5767:2,induction_loop +A22:5807:1,radar +A22:5807:2,radar +A22:5807:3,radar +A22:5807:4,radar +A22:5807:6,radar +A22:5845:1,induction_loop +A22:5845:2,induction_loop +A22:5845:3,induction_loop +A22:5845:4,induction_loop +A22:657:1,induction_loop +A22:657:2,induction_loop +A22:658:2,induction_loop +A22:658:4,induction_loop +A22:659:1,induction_loop +A22:659:2,induction_loop +A22:660:1,induction_loop +A22:660:3,induction_loop +A22:661:1,induction_loop +A22:661:2,induction_loop +A22:661:3,induction_loop +A22:661:4,induction_loop +A22:663:1,induction_loop +A22:663:2,induction_loop +A22:663:3,induction_loop +A22:663:4,induction_loop +A22:666:1,induction_loop +A22:666:2,induction_loop +A22:666:3,induction_loop +A22:666:4,induction_loop +A22:668:1,induction_loop +A22:668:2,induction_loop +A22:668:3,induction_loop +A22:668:4,induction_loop +A22:671:1,induction_loop +A22:671:2,induction_loop +A22:671:3,induction_loop +A22:671:4,induction_loop +A22:672:1,induction_loop +A22:672:2,induction_loop +A22:672:3,induction_loop +A22:672:4,induction_loop +A22:673:3,induction_loop +A22:673:4,induction_loop +A22:674:1,induction_loop +A22:674:2,induction_loop +A22:674:3,induction_loop +A22:674:4,induction_loop +A22:676:1,induction_loop +A22:676:2,induction_loop +A22:676:3,induction_loop +A22:676:4,induction_loop +A22:677:1,induction_loop +A22:677:2,induction_loop +A22:677:3,induction_loop +A22:677:4,induction_loop +A22:678:1,induction_loop +A22:678:2,induction_loop +A22:678:3,induction_loop +A22:678:4,induction_loop +A22:679:1,induction_loop +A22:679:2,induction_loop +A22:679:3,induction_loop +A22:679:4,induction_loop +A22:680:1,induction_loop +A22:680:2,induction_loop +A22:680:3,induction_loop +A22:680:4,induction_loop +A22:681:1,induction_loop +A22:681:2,induction_loop +A22:681:3,induction_loop +A22:681:4,induction_loop +A22:683:1,induction_loop +A22:683:2,induction_loop +A22:683:3,induction_loop +A22:683:4,induction_loop +A22:684:1,induction_loop +A22:684:2,induction_loop +A22:684:3,induction_loop +A22:684:4,induction_loop +A22:685:2,induction_loop +A22:685:3,induction_loop +A22:685:4,induction_loop +A22:686:1,induction_loop +A22:686:2,induction_loop +A22:686:3,induction_loop +A22:686:4,induction_loop +A22:687:1,induction_loop +A22:687:2,induction_loop +A22:687:3,induction_loop +A22:687:4,induction_loop +A22:889:3,radar +A22:889:4,radar +A22:889:6,radar +A22:890:3,radar +A22:890:4,radar +A22:890:6,radar +A22:1871:3,induction_loop +A22:1871:4,induction_loop +A22:1872:1,induction_loop +A22:1872:2,induction_loop +A22:1886:5,induction_loop +A22:1886:6,induction_loop +A22:1887:5,induction_loop +A22:5415:1,radar +A22:5416:3,radar +A22:5438:1,induction_loop +A22:5438:2,induction_loop +A22:5438:3,induction_loop +A22:5438:4,induction_loop +A22:5553:5,induction_loop +A22:5569:1,induction_loop +A22:5569:2,induction_loop +A22:5569:3,induction_loop +A22:5569:4,induction_loop +A22:5753:1,induction_loop +A22:5753:2,induction_loop +A22:5753:3,induction_loop +A22:5753:4,induction_loop +A22:5767:3,induction_loop +A22:5767:4,induction_loop +A22:5776:1,induction_loop +A22:5776:2,induction_loop +A22:5776:3,induction_loop +A22:5776:4,induction_loop +A22:658:1,induction_loop +A22:658:3,induction_loop +A22:660:2,induction_loop +A22:660:4,induction_loop +A22:662:1,induction_loop +A22:662:2,induction_loop +A22:662:3,induction_loop +A22:662:4,induction_loop +A22:664:1,induction_loop +A22:664:2,induction_loop +A22:664:3,induction_loop +A22:664:4,induction_loop +A22:673:1,induction_loop +A22:673:2,induction_loop +A22:682:1,induction_loop +A22:682:2,induction_loop +A22:682:3,induction_loop +A22:682:4,induction_loop +A22:685:1,radar +A22:889:1,radar +A22:889:2,radar +A22:890:1,radar +A22:890:2,radar +A22:5414:4,radar +A22:5553:1,induction_loop +A22:5553:2,induction_loop +A22:5553:3,induction_loop +A22:5553:4,induction_loop +A22:5936:1,camera +A22:5936:2,camera +A22:5937:1,camera +A22:5937:2,camera +A22:5938:1,camera +A22:5938:2,camera +A22:5939:1,camera +A22:5939:2,camera +A22:5953:1,camera +A22:5953:2,camera +A22:5954:1,camera +A22:5954:2,camera +A22:5955:1,camera +A22:5955:2,camera +A22:5956:1,camera +A22:5956:2,camera +A22:5957:1,camera +A22:5957:2,camera +A22:5958:1,camera +A22:5958:2,camera +A22:5959:1,camera +A22:5959:2,camera +A22:5960:1,camera +A22:5960:2,camera +A22:6023:1,camera +A22:6023:2,camera +A22:6024:1,camera +A22:6024:2,camera +A22:6025:1,camera +A22:6025:2,camera +A22:6026:1,camera +A22:6026:2,camera +A22:6027:1,camera +A22:6027:2,camera +A22:6028:1,camera +A22:6028:2,camera +A22:6029:1,camera +A22:6029:2,camera +A22:6030:1,camera +A22:6030:2,camera +A22:6031:1,camera +A22:6031:2,camera +A22:6032:1,camera +A22:6032:2,camera +A22:6033:1,camera +A22:6033:2,camera +A22:6034:1,camera +A22:6034:2,camera +A22:6035:1,camera +A22:6035:2,camera +A22:6036:1,camera +A22:6036:2,camera \ No newline at end of file