Skip to content
Browse files

Whiteboard callback working, demo app updated

  • Loading branch information...
1 parent 4db20c1 commit b4c2a37029c11c79d309e1331d750d94ccd8658a @theterg theterg committed Sep 26, 2011
View
2 RemoteMonDemoAPI/remotemondemoapi/Activator.java
@@ -34,7 +34,7 @@
*/
public void start(BundleContext context) throws Exception {
//Begin tracking services, and when all services are available, create thread and call ManagedRunnable.run().
- serviceTracker = ServiceTrackerHelper.openServiceTracker(context, services, new RemoteMonDemoAPIApplication());
+ serviceTracker = ServiceTrackerHelper.openServiceTracker(context, services, new RemoteMonDemoAPIApplication(context));
}
View
25 RemoteMonDemoAPI/remotemondemoapi/RemoteMonDemoAPIApplication.java
@@ -2,10 +2,12 @@
import java.util.Map;
+import com.buglabs.xbee.XBeeCallback;
import com.buglabs.xbee.XBeeController;
import com.buglabs.xbee.protocol.SparkfunWeatherboard;
import com.buglabs.xbee.protocol.XBeeProtocol;
+import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
import com.buglabs.application.ServiceTrackerHelper.ManagedRunnable;
@@ -24,13 +26,34 @@
private XBeeController xbc;
private LogService ls;
+ private XBeeCallback callback;
+ private SparkfunWeatherboard weather;
+
+ public RemoteMonDemoAPIApplication(BundleContext context){
+ callback = new XBeeCallback() {
+ @Override
+ public void dataRecieved(Map<String, Object> data) {
+ if ((XBeeProtocol)data.get("protocol") == weather){
+ ilog("Weather data from "+Integer.toHexString(((int[])data.get("address"))[1]));
+ ilog("Temperature: "+data.get("Temperature"));
+ ilog("Humidity: "+data.get("Humidity"));
+ ilog("Dewpoint: "+data.get("Dewpoint"));
+ ilog("Pressure: "+data.get("Pressure"));
+ ilog("Light: "+data.get("Light"));
+ }
+ }
+
+ };
+ context.registerService(XBeeCallback.class.getName(), callback, null);
+ }
@Override
public void run(Map<Object, Object> services) {
xbc = (XBeeController) services.get(XBeeController.class.getName());
ls = (LogService) services.get(LogService.class.getName());
ilog("start");
- xbc.addListener(new SparkfunWeatherboard(address));
+ weather = new SparkfunWeatherboard(address);
+ xbc.addListener(weather);
}
@Override
View
27 com.buglabs.xbee/com/buglabs/xbee/XBeeMonitor.java
@@ -9,6 +9,7 @@
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
import com.buglabs.application.ServiceTrackerHelper.ManagedRunnable;
import com.buglabs.xbee.protocol.XBeeProtocol;
@@ -42,6 +43,7 @@
private int baud = 9600;
private Map<XBeeAddress, XBeeProtocol> protocols;
private BundleContext _context;
+ private ServiceTracker tracker;
public XBee xb;
public boolean connected = false;
@@ -67,19 +69,33 @@ public void processResponse(XBeeResponse res) {
if (protocols.containsKey(pkt.getSourceAddress())){
Map<String,Object> ret;
ret = protocols.get(pkt.getSourceAddress()).parse(res);
+ ret.put("protocol", protocols.get(pkt.getSourceAddress()));
+ ret.put("address", pkt.getSourceAddress().getAddress());
+ ret.put("raw", pkt.getProcessedPacketBytes());
//TODO - implement a whiteboard callback
+ dlog("Got a packet belonging to "+protocols.get(pkt.getSourceAddress()).toString());
+ whiteboardNotify(ret);
//For now, just printing keyz
- for (Map.Entry<String, Object> entry : ret.entrySet())
- {
- ilog(entry.getKey()+": "+entry.getValue());
- }
+// for (Map.Entry<String, Object> entry : ret.entrySet())
+// {
+// ilog(entry.getKey()+": "+entry.getValue());
+// }
//If we don't know where the packet came from, we can't parse it!
} else {
dlog("Unknown sender ("+ByteUtils.toBase16(pkt.getSourceAddress().getAddress())
+") "+ByteUtils.toString(pkt.getProcessedPacketBytes()));
}
}
+ private void whiteboardNotify(Map<String,Object> data){
+ Object[] services = tracker.getServices();
+ if (services == null)
+ return;
+ for (Object s:services){
+ ((XBeeCallback) s).dataRecieved(data);
+ }
+ }
+
@Override
public void addListener(XBeeProtocol proto) {
dlog("adding protocol "+ByteUtils.toBase16(proto.getAddr().getAddress())+":"+proto.getClass().getName());
@@ -112,6 +128,8 @@ public void run(Map<Object, Object> services) {
t = new Thread(this, "XBee Monitor");
t.start();
_context.registerService(XBeeController.class.getName(), this, null);
+ tracker = new ServiceTracker(_context, XBeeCallback.class.getName(),null);
+ tracker.open();
}
@Override
@@ -168,6 +186,7 @@ public void run() {
public void shutdown() {
running = false;
t.interrupt();
+ tracker.close();
}
//Wrappers for the log service (to standardize logged messages)
View
12 com.buglabs.xbee/com/buglabs/xbee/protocol/SparkfunWeatherboard.java
@@ -58,4 +58,16 @@ public XBeeAddress getAddr() {
return addr;
}
+ @Override
+ public String toString(Map<String, Object> data) {
+ String ret = "";
+ ret += "Weather data from "+Integer.toHexString(((int[])data.get("address"))[1])+"\r\n";
+ ret += "Temperature: "+data.get("Temperature")+"\r\n";
+ ret += "Humidity: "+data.get("Humidity")+"\r\n";
+ ret += "Dewpoint: "+data.get("Dewpoint")+"\r\n";
+ ret += "Pressure: "+data.get("Pressure")+"\r\n";
+ ret += "Light: "+data.get("Light")+"\r\n";
+ return ret;
+ }
+
}
View
1 com.buglabs.xbee/com/buglabs/xbee/protocol/XBeeProtocol.java
@@ -9,4 +9,5 @@
public Map<String, Object> parse(XBeeResponse res);
public Map<String, Object> lastSample();
public XBeeAddress getAddr();
+ public String toString(Map<String, Object> data);
};

0 comments on commit b4c2a37

Please sign in to comment.
Something went wrong with that request. Please try again.