Skip to content

Commit

Permalink
traffic-a22-elaboration: use SenortypeUtil for sensor_type meatadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed May 22, 2024
1 parent 937d265 commit 975c200
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 95 deletions.
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,11 +27,12 @@ public class SyncStation {
private static String origin;
private static String stationtype;

private Map<String, String> 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();
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -139,28 +117,13 @@ public StationList readStationList(Connection connection) throws IOException, SQ
HashMap<String, Object> metadataMap = new HashMap<String, Object>();
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);
}
}
LOG.debug("Return stationlist");
return stationList;

}

}
Loading

0 comments on commit 975c200

Please sign in to comment.