Skip to content

Instantly share code, notes, and snippets.

@ckundo
Created December 16, 2010 19:04
Show Gist options
  • Save ckundo/743804 to your computer and use it in GitHub Desktop.
Save ckundo/743804 to your computer and use it in GitHub Desktop.
package earthquake_detector;
import java.io.IOException;
import java.util.Map;
import org.osgi.util.position.Position;
import com.buglabs.bug.accelerometer.pub.AccelerometerSample;
import com.buglabs.bug.accelerometer.pub.IAccelerometerSampleProvider;
import com.buglabs.bug.module.gps.pub.IPositionProvider;
import com.buglabs.bug.swarm.connector.pub.IReplicatedModel;
import com.buglabs.bug.swarm.connector.pub.IReplicationPolicy;
import com.buglabs.bug.swarm.connector.pub.ISwarmConnector;
import com.buglabs.application.ServiceTrackerHelper;
/**
* Read accelerometer data and send to Swarm server. This class behaves like a Runnable in that when it's created by ServiceTrackerHelper.openServiceTracker(),
* It is run within a new thread, and all the requested services that were passed into ServiceTrackerHelper.openServiceTracker() are available in the map that
* is passed as a parameter to the run() method.
*
* @author kgilmer
*
*/
public class Monitor implements ServiceTrackerHelper.ManagedRunnable {
@Override
public void run(Map services) {
//Get local references to all requested services.
ISwarmConnector connector = (ISwarmConnector) services.get(ISwarmConnector.class.getName());
IAccelerometerSampleProvider accel = (IAccelerometerSampleProvider) services.get(IAccelerometerSampleProvider.class.getName());
IPositionProvider positionProvider = (IPositionProvider) services.get(IPositionProvider.class.getName());
try {
//Initialize a ReplicatedModel instance. This will contact the swarm server and setup a new document.
IReplicatedModel model = connector.getModel("EarthquakeDB", IReplicationPolicy.POLICY_PUSH, new AccelerometerSampleFilter(positionProvider));
//Send our current location once.
Position p = positionProvider.getPosition();
model.put(Activator.KEY_LOCATION, positionAsArray(p));
while (!Thread.interrupted()) {
Thread.sleep(1000);
AccelerometerSample sample = accel.readSample();
model.put(Activator.KEY_ACCELERATION, accelerationAsArray(sample));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
}
}
protected static double[] positionAsArray(Position p) {
return new double[] {p.getLatitude().getValue(), p.getLongitude().getValue()};
}
protected static float[] accelerationAsArray(AccelerometerSample sample) {
return new float[] {sample.getAccelerationX(), sample.getAccelerationY(), sample.getAccelerationZ()};
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment