> On Friday, April 1, 2016 8:41 AM, James Cancilla <[email protected]> 
> wrote:


> Are there any instructions for running Quarks on Android?

Sample code, this was in the MainActivity.onCreate method:

SensorManager mSensorManager = (SensorManager) 
getSystemService(Context.SENSOR_SERVICE); 

dp = new DirectProvider(); 

topology = dp.newTopology("QuarkSimpleSensor"); 

// Create a source stream of SensorEvents for temp & pressure 
// Topology has a variety of methods to create sources 
// This is a utility method for Android sensors 

TStream<SensorEvent> sensors = SensorStreams.sensors(topology, mSensorManager, 
Sensor.TYPE_AMBIENT_TEMPERATURE, Sensor.TYPE_PRESSURE); 

// Example of a filter 
// (Java 8 would be: 
// e -> e.values[0] > 15.0 
sensors = sensors.filter(new Predicate<SensorEvent>() { 
public boolean test(SensorEvent event) { 
return event.values[0] > 15.0; 
} 
}); 

final TextView tv = (TextView) findViewById(R.id.text); 

// Terminate the stream by printing to a TextView 
ActivityStreams.sinkOnUIThread(this, sensors, new Consumer<SensorEvent>() { 
@Override 
public void accept(SensorEvent event) { 
tv.setText("QUARKS:" + event.timestamp + event.sensor.getName() + 
event.values[0]); 
} 
}); 


// Convert sensors stream to a JSON event for IoTF 
TStream<JsonObject> sensorsJson = 
sensors.map(new Function<SensorEvent, JsonObject>() { 
@Override 
public JsonObject apply(SensorEvent event) { 
JsonObject j = new JsonObject(); 
j.addProperty("name", event.sensor.getName()); 
j.addProperty("value", event.values[0]); 
return j; 
} 
}); 


// Send to iotf  as device events - need valid device id from an IoTF server 
// Can also load config from a file. 
Properties options = new Properties(); 
options.setProperty("org", "XXXXX"); // Use your IoTF org and other properties 

IotfDevice device = new IotfDevice(topology, options); 

// publish to IoTF as Sensor events 
// Note at this point there is no local 
// analytics,. every event (after the filter) 
// is sent to IoTF. Sample will be improved 
// to show some aggregation. 
device.events(sensorsJson, "sensors", 0); 

// Separate source stream 
// Get the time every 5 seconds. 
TStream<Date> dates = topology.poll(new Supplier<Date>() { 
@Override 
public Date get() { 
return new Date(); 
} 
}, 5, TimeUnit.SECONDS); 

// Utility method to map/transform a string to a stream of string objects 
TStream<String> datesStr = dates.asString(); 

final TextView tv2 = (TextView) findViewById(R.id.text2); 

// Terminate the stream by printing to a TextView 
ActivityStreams.sinkOnUIThread(this, datesStr, new Consumer<String>() { 
@Override 
public void accept(String msg) { 
tv2.setText("QUARKS_2:" + msg); 
} 
}); 

// At the point the topology (streaming graph) 
// has just been declared. Nothing is runing, 
// TStream instances are a declaration of a stream 
// not a runtime object. 

// Now submit (start) the application. 
// Will run on its own threads 
dp.submit(topology);

Reply via email to