====== Context Simulator ====== Context Simulator is a tool that allows to simulate context events stored in SQL database in real time. It is being designed to work with [[http://awareframework.com|AWARE framework]]. This tool is under development, but the developer version can be downloaded from: https://bitbucket.org/sbobek/context-simulator ===== Quickstart ===== ==== Running the simluation ==== This example can be found in the project files, in ''edu.agh.awaresim.example.Main''. // 1. First, create DataSource (in this example MysqlDataSource) by providing // hostname, port, username, password, and name of the databese where the data for simluation are DataSource ds = new MysqlDataSource("localhost", 3306, "root", "toor", "aware"); // 2. Then, we create a new simulator, which will be using DataSource created in the previous step // We set the timestamp from which the simulation should start, and UUID of the device which data should // be used for the simulation AwareSimulator sim = new AwareSimulator(ds, 1391062684000L, UUID.fromString("92d47d9d-a600-4309-b340-b58314c2e429")); // 3. We can set the simulation rate. In the example below we set the simulation // to be 1000x faster than in real time. sim.setSpeed(1000.0); // 4. We then create a listener that will listen for the specific event (context) during a simulation. // In this example we create ApplicationForeground listener, which will be printing out the // name of the application that was launched according to the simulation data launched. sim.applicationsForeground.addListener(new AwareSimulator.Listener() { public void onEvent(ApplicationsForeground event) { System.out.println("App in foreground: " + event.applicationName); } }); // 5. Now we start the simulation sim.start(); You can also set the speed of the simulation while the simulation is running (''AwareSimulator#setSpeed'' ). This also apply to adding new listeners (''AwareSimulator.Listener'') and turing on/off event handlers (''AwareSimulator.EventsHandler''). ==== Adding new sensor to Context Simulator ==== - Open the project and find ''table'' package within it:{{:pub:software:contextsimulator:table_package.jpg?500|}} - Create the new class within the ''table'' package. Use the name that corresponds to the table name in your database. This is not a requirement, but helps in later code management:{{:pub:software:contextsimulator:create_new_class.jpg?500|}} - Implement in this calss methods and fields that correspond to the table columns in your database. In particular create a construcotr and ''toString()'' method: package edu.agh.awaresim.table; import edu.agh.awaresim.AbstractEvent; import java.util.UUID; public class Accelerometer implements AbstractEvent{ public int id() { return _id; } public long timestamp() { return _timestamp; } public UUID device() { return _device; } private final int _id; private final long _timestamp; private final UUID _device; public final double doubleValues0; public final double doubleValues1; public final double doubleValues2; public final int accuracy; public final String label; public Accelerometer(int id, long timestamp, UUID device, double doubleValues0, double doubleValues1, double doubleValues2, int accuracy, String label) { this._id = id; this._timestamp = timestamp; this._device = device; this.doubleValues0 = doubleValues0; this.doubleValues1 = doubleValues1; this.doubleValues2 = doubleValues2; this.accuracy = accuracy; this.label = label; } public String toString() { return "["+id()+"] - ["+timestamp()+"] - ["+device()+"] - ["+ doubleValues0 +"] - ["+ doubleValues1 +"] - ["+ doubleValues2 +"] - ["+ accuracy +"] - "+ label +""; } } - Open ''AwareSimulator'' class and add appropriate event handlers: {{:pub:software:contextsimulator:awaresimulator.jpg?500|}} package edu.agh.awaresim; ... public final EventsHandler accelerometer; ... this.accelerometer = new EventsHandler(dataSource.accelerometer()); - Open ''DataSource'' interface and create new source: {{:pub:software:contextsimulator:datasource.jpg?500|}} package edu.agh.awaresim; ... public Source accelerometer(); - Open ''MysqlDataSource'' and create a method that will return records from your AWARe database table: {{:pub:software:contextsimulator:mysqldatasource.jpg?500|}} package edu.agh.awaresim; ... public Source accelerometer() { return new Source() { public List apply(UUID device, int withIdGreaterThan, long withTimestampGreaterEqualTo, int number) { List rv = new ArrayList(); try{ ResultSet result = query("accelerometer", device, withIdGreaterThan, withTimestampGreaterEqualTo, number); while (result.next()) rv.add(new Accelerometer( result.getInt("_id"), result.getLong("timestamp"), UUID.fromString(result.getString("device_id")), result.getDouble("double_value_0"), result.getDouble("double_value_1"), result.getDouble("double_value_2"), result.getInt("accuracy"), result.getString("label"))); } catch (SQLException e) { e.printStackTrace(); } return rv; } }; } - In the main class in the ''main'' method, add a listener that will handle the new context events:package edu.agh.awaresim.example; ... sim.accelerometer.addListener(new AwareSimulator.Listener() { public void onEvent(Accelerometer event) { System.out.println("Accelerometer: " + event); } }); - Run the simulation and check if the listener works: {{:pub:software:contextsimulator:lastcheck.jpg?500|}}