Commit bbdc285e authored by Julian Zobel's avatar Julian Zobel
Browse files

Added jdbc driver in maven pom

GPS Calculation adaptions (moved GPS transformation to GPS!)
Data Converter start for smarter GPS conversion
parent 635d2388
......@@ -28,6 +28,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=enabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
......@@ -69,6 +70,7 @@ org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=igno
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=ignore
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
......
......@@ -213,8 +213,8 @@
<version>4.3.10.Final</version>
</dependency>
<!-- <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version> </dependency> -->
<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version> </dependency>
<!-- Glab Database -->
<dependency>
......
......@@ -26,10 +26,12 @@ import java.util.Set;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
import de.tud.kom.p2psim.impl.topology.movement.smarter.dataanalyzer.Analyzer;
import de.tud.kom.p2psim.impl.topology.movement.smarter.dataanalyzer.Statistics;
import de.tud.kom.p2psim.impl.topology.movement.smarter.host.SmarterHost;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
......@@ -43,6 +45,8 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
* @version 1.0, 12.06.2018
*/
public class SmarterMovementModelNew implements MovementModel, EventHandler {
protected IMapVisualization mapVisualization;
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN);
public static SmarterMovementModelNew movement;
protected long timeBetweenMoveOperations;
......@@ -57,9 +61,20 @@ public class SmarterMovementModelNew implements MovementModel, EventHandler {
public SmarterMovementModelNew() {
movement = this;
dataAnalyzed = false;
// scheduling initalization!
Event.scheduleImmediately(this, null, 42);
}
private void initialize() {
// TODO get bool from xml to decide whether to analyze data or not
if(!dataAnalyzed)
analyzeData();
if (mapVisualization != null) {
VisualizationInjector.injectComponent(mapVisualization);
}
}
/**
......@@ -125,6 +140,11 @@ public class SmarterMovementModelNew implements MovementModel, EventHandler {
*/
@Override
public void eventOccurred(Object content, int type) {
if(type == 42) {
initialize();
}
else {
SmarterHost host = (SmarterHost) content;
Location location = host.getNextLocation();
......@@ -141,6 +161,10 @@ public class SmarterMovementModelNew implements MovementModel, EventHandler {
}
}
}
/**
* Called once during simulation setup for each host. Creates a SmarterHost object at a default location.
*/
......@@ -181,4 +205,10 @@ public class SmarterMovementModelNew implements MovementModel, EventHandler {
public long getTimeBetweenMoveOperations() {
return timeBetweenMoveOperations;
}
public void setIMapVisualization(IMapVisualization mapVisualization) {
this.mapVisualization = mapVisualization;
}
}
......@@ -21,6 +21,8 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.AttractionPointImpl;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tudarmstadt.maki.simonstrator.api.Binder;
......@@ -135,6 +137,28 @@ public class GPSCalculation {
this.lonCenter = lonCenter;
}
/**
* Projects the gps coordinates in the given gps window to the world-coordinates given in world-dimensions
* @param lat
* @param lon
* @return The projected position in world-dimensions
*/
public static PositionVector transformGPSWindowToOwnWorld(double lat, double lon) {
double x = getWorldDimensionsX() * (lon - getLonLeft())/(getLonRight() - getLonLeft());
//Invert the y value, because in Java Swing we start drawing in the upper left corner instead in the lower left one
double y = getWorldDimensionsY() - getWorldDimensionsY() * (lat - getLatLower())/(getLatUpper() - getLatLower());
return new PositionVector(x, y);
}
public static boolean isWithinSimulationBoundaries(double lat, double lon) {
if(lat > getLatLower() && lat < getLatUpper() &&
lon > getLonLeft() && lon < getLonRight()) {
return true;
}
else return false;
}
/**
* Formula http://wiki.openstreetmap.org/wiki/Zoom_levels
*
......
......@@ -54,14 +54,9 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
*/
public class JSONAttractionGenerator implements IAttractionGenerator {
private PositionVector worldDimensions;
private int numberOfAttractionPoints;
private String placementJsonFile;
private double latLeft; //Values from -90 to 90; always smaller than latRight
private double latRight; //Values from -90 to 90
private double lonLeft; //Values from -180 to 180; Always smaller than lonRight
private double lonRight; //Values from -180 to 180
private double maximumRadius = -1; // Values >= 0, or -1 if radius taken from file
/**
......@@ -71,27 +66,7 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
*/
@XMLConfigurableConstructor({"numberOfAttractionPoints"})
public JSONAttractionGenerator(int numberOfAttractionPoints) {
this.worldDimensions = Binder.getComponentOrNull(Topology.class)
.getWorldDimensions();
this.numberOfAttractionPoints = numberOfAttractionPoints;
latLeft = GPSCalculation.getLatLower();
latRight = GPSCalculation.getLatUpper();
lonLeft = GPSCalculation.getLonLeft();
lonRight = GPSCalculation.getLonRight();
}
/**
* Projects the gps coordinates in the given gps window to the world-coordinates given in world-dimensions
* @param lat
* @param lon
* @return The projected position in world-dimensions
*/
private PositionVector transformGPSWindowToOwnWorld(double lat, double lon) {
double x = worldDimensions.getX() * (lon - lonLeft)/(lonRight - lonLeft);
//Invert the y value, because in Java Swing we start drawing in the upper left corner instead in the lower left one
double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
return new PositionVector(x, y);
}
@Override
......@@ -131,11 +106,10 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
AttractionPointImpl ap;
// check that the point is within the simulation boundaries
if(lat > latLeft && lat < latRight &&
lon > lonLeft && lon < lonRight) {
if(GPSCalculation.isWithinSimulationBoundaries(lat, lon)) {
// initialize the attraction point with basic information, will be filled now...
ap = new AttractionPointImpl(barname, transformGPSWindowToOwnWorld(lat, lon));
ap = new AttractionPointImpl(barname, GPSCalculation.transformGPSWindowToOwnWorld(lat, lon));
attractionPoints.add(ap);
// the following setters are allowed to fail
......
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.smarter;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
public class DataConverter {
public DataConverter() {
readTraceDatabases();
}
public static void readTraceDatabases() {
List<IAttractionPoint> aps = IAttractionGenerator.attractionPoints;
DataGrabber grabber = new DataGrabber();
// Set the path to folder containing the .sqlite files here
String sqliteFolderPath = "D:/Simonstrator/simrunner/smarter/databases/";
List<String> filePaths = DataConverter.getFilePaths(sqliteFolderPath);
try {
System.out.println(Class.forName("org.sqlite.JDBC"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (String filePath : filePaths) {
grabber.connect(filePath);
List<SmarterDBEntry> listOfEntries = grabber.select("SELECT * FROM gps_data");
grabber.disconnect();
// Analyze data
for (SmarterDBEntry entry : listOfEntries) {
int id = entry.get_id();
String timestamp = entry.getTimestamp();
double lat = entry.getLatitude();
double lon = entry.getLongitude();
double alt = entry.getAltitude();
double acc = entry.getAccuracy();
if(GPSCalculation.isWithinSimulationBoundaries(lat, lon)) {
PositionVector p = GPSCalculation.transformGPSWindowToOwnWorld(lat, lon);
for (IAttractionPoint ap : aps) {
if(ap.distanceTo(p) <= ap.getRadius()) {
System.out.println(ap.getName());
System.out.println(id + ": " + lat + " / " + lon + " / " + alt + " +/- " + acc + " --- " + timestamp);
System.out.println(p);
}
}
}
}
return;
}
}
/**
* Returns a list of full file paths of a given directory
*
* @param directory
* The
* @return
*/
public static ArrayList<String> getFilePaths(String directory) {
File folder = new File(directory);
File[] listOfFiles = folder.listFiles();
ArrayList<String> filePaths = new ArrayList<String>();
if (listOfFiles.length == 0) {
System.out.println("No files in directory");
return null;
}
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if(listOfFiles[i].getName().contains(".db")) {
filePaths.add(directory + listOfFiles[i].getName());
}
}
}
return filePaths;
}
/*
* @author Marcel Verst
*/
private static class SmarterDBEntry {
// All columns of the row
public int _id;
double longitude;
double latitude;
double altitude;
double accuracy;
String timestamp;
/**
* Creates SmarterDBEntry object
*
* @param _id
* the id entry
* @param longitude
* the longitude entry
* @param latitude
* the latitude entry
* @param altitude
* the altitude entry
* @param accuracy
* the accuracy entry
* @param timestamp
* the timestamp entry
*/
public SmarterDBEntry(int _id, double longitude, double latitude, double altitude, double accuracy, String timestamp) {
super();
this._id = _id;
this.longitude = longitude;
this.latitude = latitude;
this.altitude = altitude;
this.accuracy = accuracy;
this.timestamp = timestamp;
}
/**
* Print entry
*
* @return String containing all entries of the row
*/
@Override
public String toString() {
return "DBEntry [_id=" + _id + ", longitude=" + longitude + ", latitude=" + latitude + ", altitude=" + altitude
+ ", accuracy=" + accuracy + ", timestamp=" + timestamp + "]";
}
/**
* Gets the ID
*
* @return _id
*/
public int get_id() {
return _id;
}
/**
* Gets the longitude
*
* @return longitude
*/
public double getLongitude() {
return longitude;
}
/**
* Gets the latitude
*
* @return latitude
*/
public double getLatitude() {
return latitude;
}
/**
* Gets the altitude
*
* @return altitude
*/
public double getAltitude() {
return altitude;
}
/**
* Gets the accuracy
*
* @return accuracy of GPS signal
*/
public double getAccuracy() {
return accuracy;
}
/**
* Gets the timestamp
*
* @return timestamp
*/
public String getTimestamp() {
return timestamp;
}
}
/**
* DataGrabber class is responsible for establishing a connection to a database
* and to query the data
*
* @author Marcel Verst
* @version 26.05.2018
*/
private static class DataGrabber {
// The connection to the database.
private Connection conn;
/**
* DataGrabber constructor, initializes the connection
*/
public DataGrabber() {
conn = null;
}
/**
* Connect to a database specified in params.properties Code from
* "http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/"
*
* @throws SQLException
* if the connection cannot be established
*/
public void connect(String filePath) {
try {
// db parameters
String url = "jdbc:sqlite:" + filePath;
// create a connection to the database
System.out.println(url);
conn = DriverManager.getConnection(url);
System.out.println("Connection to " + filePath + " has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Disconnects database
*
* @throws SQLException
* if disconnection failed
*/
public void disconnect() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Queries the database with a given query string, stores the values within an
* ArrayList and returns it for further analyzes
*
* @param selection
* The SQL query
* @return listOfEntries ArrayList object containing DBEntry types
* @throws SQLException
* if there are problems with connecting to the database
*/
public ArrayList<SmarterDBEntry> select(String selection) {
ArrayList<SmarterDBEntry> listOfEntries = new ArrayList<SmarterDBEntry>();
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(selection)) {
// loop through the result set
while (rs.next()) {
int _id = rs.getInt("_id");
double longitude = rs.getDouble("longitude");
double latitude = rs.getDouble("latitude");
double altitude = rs.getDouble("altitude");
double accuracy = rs.getDouble("accuracy");
String timestamp = rs.getString("timestamp");
SmarterDBEntry entry = new SmarterDBEntry(_id, longitude, latitude, altitude, accuracy, timestamp);
listOfEntries.add(entry);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return listOfEntries;
}
}
}
/*
* Copyright (c) 2005-2010 KOM � Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.smarter.dataanalyzer;
/**
* Represents a row of the gps_data table from a database
*
* @author Marcel Verst
* @version 26.05.2018
*/
public class DBEntry {
// All columns of the row
public int _id;
double longitude;
double latitude;
double altitude;
double accuracy;
String timestamp;
/**
* Creates DBEntry object
*
* @param _id
* the id entry
* @param longitude
* the longitude entry
* @param latitude
* the latitude entry
* @param altitude
* the altitude entry
* @param accuracy
* the accuracy entry
* @param timestamp
* the timestamp entry
*/
public DBEntry(int _id, double longitude, double latitude, double altitude, double accuracy, String timestamp) {
super();
this._id = _id;
this.longitude = longitude;
this.latitude = latitude;
this.altitude = altitude;
this.accuracy = accuracy;
this.timestamp = timestamp;
}
/**
* Print entry
*
* @return String containing all entries of the row
*/
@Override
public String toString() {
return "DBEntry [_id=" + _id + ", longitude=" + longitude + ", latitude=" + latitude + ", altitude=" + altitude
+ ", accuracy=" + accuracy + ", timestamp=" + timestamp + "]";
}
/**
* Gets the ID
*
* @return _id
*/
public int get_id() {
return _id;
}
/**
* Sets the ID
*
* @param _id
*/
public void set_id(int _id) {
this._id = _id;
}
/**
* Gets the longitude
*
* @return longitude
*/
public double getLongitude() {
return longitude;
}
/**
* Sets the longitude
*
* @param longitude
*/
public void setLongitude(double longitude) {
this.longitude = longitude;
}
/**
* Gets the latitude
*
* @return latitude
*/
public double getLatitude() {
return latitude;
}
/**
* Sets the latitude
*
* @param latitude
*/
public void setLatitude(double latitude) {
this.latitude = latitude;
}
/**
* Gets the altitude
*
* @return altitude
*/
public double getAltitude() {
return altitude;
}
/**
* Sets the altitude
*
* @param altitude
*/
public void setAltitude(double altitude) {
this.altitude = altitude;
}
/**
* Gets the accuracy
*
* @return accuracy of GPS signal
*/
public double getAccuracy() {
return accuracy;
}
/**
* Sets the accuracy
*
* @param accuracy
*/
public void setAccuracy(double accuracy) {
this.accuracy = accuracy;
}
/**
* Gets the timestamp
*
* @return timestamp
*/
public String getTimestamp() {
return timestamp;
}
/**
* Sets the timestamp
*
* @param timestamp
*/
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
/*
* Copyright (c) 2005-2010 KOM � Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.smarter.dataanalyzer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
* DataGrabber class is responsible for establishing a connection to a database
* and to query the data
*
* @author Marcel Verst
* @version 26.05.2018
*/
public class DataGrabber {
// The connection to the database.
private Connection conn;
/**
* DataGrabber constructor, initializes the connection
*/
public DataGrabber() {
conn = null;
}
/**
* Connect to a database specified in params.properties Code from
* "http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/"
*
* @throws SQLException
* if the connection cannot be established
*/
public void connect(String filePath) {
try {
// db parameters
String url = "jdbc:sqlite:" + filePath;
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to " + filePath + " has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Disconnects database
*
* @throws SQLException
* if disconnection failed
*/
public void disconnect() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Queries the database with a given query string, stores the values within an
* ArrayList and returns it for further analyzes
*
* @param selection
* The SQL query
* @return listOfEntries ArrayList object containing DBEntry types
* @throws SQLException
* if there are problems with connecting to the database
*/
public ArrayList<DBEntry> select(String selection) {
ArrayList<DBEntry> listOfEntries = new ArrayList<DBEntry>();
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(selection)) {
// loop through the result set
while (rs.next()) {
int _id = rs.getInt("_id");
double longitude = rs.getDouble("longitude");
double latitude = rs.getDouble("latitude");
double altitude = rs.getDouble("altitude");
double accuracy = rs.getDouble("accuracy");
String timestamp = rs.getString("timestamp");
DBEntry entry = new DBEntry(_id, longitude, latitude, altitude, accuracy, timestamp);
listOfEntries.add(entry);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return listOfEntries;
}
}
......@@ -97,7 +97,6 @@ public class SmarterHost {
nextLocation = new PositionVector(positionX, positionY);
_sleepTime = nextTimestamp * SmarterMovementModelNew.getInstance().getTimeBetweenMoveOperations() - Time.getCurrentTime();
return true;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment