Permalink
Browse files

fix directory structure, add moved files.

  • Loading branch information...
1 parent c5eab7c commit 0e5a932e43fef00c9e1d6c77b2adf9d5d563c9d3 John E. Connolly committed Aug 24, 2011
View
@@ -0,0 +1,34 @@
+# Compiled source #
+###################
+*.com
+*.class
+*.dll
+*.exe
+*.o
+*.so
+
+# Packages #
+############
+# it's better to unpack these files and commit the raw source
+# git has its own built in compression methods
+*.7z
+*.dmg
+*.gz
+*.iso
+*.rar
+*.tar
+*.zip
+
+# Logs and databases #
+######################
+*.log
+*.sql
+*.sqlite
+
+# OS generated files #
+######################
+.DS_Store?
+ehthumbs.db
+Icon?
+Thumbs.db
+
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path=""/>
+ <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="isidorey-api.jar"/>
+ <classpathentry kind="lib" path="mqtt-client.jar"/>
+ <classpathentry kind="lib" path="netty-3.2.5.Final.jar"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="con" path="com.buglabs.dragonfly.jdt.BugClasspathContainerInitializer"/>
+
+ <classpathentry kind="output" path=""/>
+</classpath>
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>bugcam_helloworld</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.buglabs.dragonfly.BugApplicationNature</nature>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,18 @@
+Manifest-Version: 1.0
+Bug-Bundle-Type: Application
+Bundle-Vendor: John Connolly
+Bundle-Version: 1.0.0
+Bundle-Name: BUGcam_helloworld
+Bundle-ClassPath: .,
+ isidorey-api.jar,
+ mqtt-client.jar,
+ netty-3.2.5.Final.jar
+Bundle-Activator: bugcam_helloworld.Activator
+Import-Package: com.buglabs.application,
+ com.buglabs.bug.module.camera.pub,
+ com.buglabs.util,
+ javax.net.ssl,
+ org.osgi.framework,
+ org.osgi.util.tracker
+Bundle-SymbolicName: BUGcam_helloworld
+
@@ -0,0 +1,47 @@
+package bugcam_helloworld.servicetracker;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.isidorey.api.APIConstants;
+import com.isidorey.api.FileAPI;
+import com.isidorey.api.PublishAPI;
+import com.isidorey.api.exception.APIException;
+
+public class ApiInteraction {
+
+ /**
+ * This call is used to send key/value pairs that relate to a device; it
+ * emulates an MQtt publish through our API, and is also the same one used
+ * to control devices!
+ */
+ public void publish(String company, String project, String tag,
+ String asset, String field, Map<String, String> keyValueMap) {
+
+ System.out.println("PUBLISHING DATA ON:" + company + "/" + project
+ + "/" + tag + "/" + asset + "/" + field);
+
+ PublishAPI api = new PublishAPI(APIConstants.API_URL,
+ Constants.username, Constants.password);
+ try {
+ api.publish(company, project, tag, asset, field, keyValueMap);
+ } catch (APIException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * This call is used to upload a file to cloud storage in S3.
+ */
+ public void uploadFile() {
+ FileAPI api = new FileAPI(APIConstants.API_URL, Constants.username,
+ Constants.password);
+ try {
+ api.uploadFile(Constants.bucket, new File(Constants.filePath));
+ } catch (APIException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
@@ -0,0 +1,213 @@
+package bugcam_helloworld.servicetracker;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.BundleContext;
+
+
+import com.albin.mqtt.ConnConfig;
+import com.albin.mqtt.IsidoreyClient;
+import com.albin.mqtt.MqttListener;
+import com.albin.mqtt.message.QoS;
+import com.buglabs.application.AbstractServiceTracker;
+import com.buglabs.bug.module.camera.pub.ICamera2Device;
+import com.buglabs.bug.module.camera.pub.ICamera2ModuleControl;
+import com.buglabs.bug.module.camera.pub.ICameraModuleControl;
+
+public class BUGcam_helloworldServiceTracker extends AbstractServiceTracker {
+
+ private static IsidoreyClient client;
+
+ private ICameraModuleControl camControl;
+ private ICamera2Device camera;
+
+ public BUGcam_helloworldServiceTracker(BundleContext context) {
+ super(context);
+ }
+
+ public boolean canStart() {
+ return super.canStart();
+ }
+
+ public void doStart() {
+ System.out.println("BUGcam_helloworldServiceTracker: start");
+ }
+
+ public void doStop() {
+ System.out.println("BUGcam_helloworldServiceTracker: stop");
+ }
+
+ /**
+ * This thread is used as a demo. We wait 20 seconds after initServices is
+ * called and then publish data through our API, emulating an MQtt message.
+ * Since the device is subscribed on the published topic, it handles the
+ * logic appropriately by taking a picture.
+ *
+ */
+ class DemoPublishThread extends Thread {
+ public void run() {
+ try {
+ System.out.println("SLEEPING THREAD 20 SECONDS");
+ Thread.sleep(1000 * 20);
+
+ ApiInteraction api = new ApiInteraction();
+
+ Map<String, String> keyValueMap = new HashMap<String, String>();
+ keyValueMap.put(Constants.controlCamera,
+ "VALUE_WHICH_COULD_BE_USED_FOR_NESTED_LOGIC");
+
+ api.publish(Constants.company, Constants.project,
+ Constants.tag, Constants.asset, Constants.controlField,
+ keyValueMap);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void initServices() {
+ System.out.println("INIT SERVICES");
+
+ /**
+ * Start the thread to handle the picture taking
+ */
+ Thread thread = new DemoPublishThread();
+ thread.start();
+
+ /**
+ * Attempt pub/sub connection
+ */
+ pubsub();
+
+ /**
+ * Demo an API call by upserting the time
+ */
+ ApiInteraction api = new ApiInteraction();
+
+ Map<String, String> keyValueMap = new HashMap<String, String>();
+ Date date = new Date();
+ keyValueMap.put("lastInit", String.valueOf(date.getTime()));
+
+ api.publish(Constants.company, Constants.project, Constants.tag,
+ Constants.asset, Constants.field, keyValueMap);
+
+ /**
+ * Handle services
+ */
+ getServices().add(
+ "com.buglabs.bug.module.camera.pub.ICamera2ModuleControl");
+ getServices().add("com.buglabs.bug.module.camera.pub.ICamera2Device");
+
+ }
+
+ /**
+ * Netty issue (?) here that I'm seeing on devices (including Android
+ * mobile), but not in testing -- Caught exception: java.nio.channels.UnresolvedAddressException. Works sporadically --
+ * can I check to see if network services are available for sure?
+ */
+ public void pubsub() {
+ connect();
+ }
+
+ /**
+ * Make an MQtt connection, and subscribe on a topic to see data being
+ * pumped in by device simulations.
+ */
+ public void connect() {
+ try {
+ System.out.println("TRYING CONNECT");
+
+ ConnConfig config = new ConnConfig(Constants.clientId,
+ Constants.brokerUrl, 1883, 30, Constants.username,
+ Constants.password);
+
+ config.addSubscribedTopic(Constants.subscribedTopic,
+ QoS.AT_MOST_ONCE);
+
+ client = new IsidoreyClient(config);
+ client.setListener(new EchoListener());
+
+ client.connect();
+ client.subscribeToConfigTopics();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Any devices sending data on topics we subscribe (e.g. have permissions
+ * to) will be spit out here.
+ */
+ private class EchoListener implements MqttListener {
+ public void disconnected() {
+ System.err.println("DISCONNECTED");
+ }
+
+ public void publishArrived(String topic, byte[] data) {
+ String payload = new String(data);
+ System.out.println("[" + topic + "]: " + payload);
+
+ if (topic.equals(Constants.controlTopic))
+ handleCommands(payload);
+
+ }
+ }
+
+ /**
+ * Take a picture if the correct parameter comes through on a given topic.
+ *
+ * @param payload
+ */
+ private void handleCommands(String payload) {
+ String[] controlParamsArray = payload.split("~~");
+ for (String entry : controlParamsArray) {
+ String[] keyValuePair = entry.split("=");
+ String key = keyValuePair[0];
+ String value = keyValuePair[1];
+ if (key.equals(Constants.controlCamera)) {
+ System.out.println("CAMERA COMMAND RECEIVED, TAKING PICTURE");
+ takePicture();
+ }
+ }
+ }
+
+ /**
+ * Why all the deprecated methods? Save the file and upload the image to
+ * your bucket.
+ */
+ private void takePicture() {
+ camera = (ICamera2Device) super.getService(ICamera2Device.class);
+ camControl = (ICameraModuleControl) super
+ .getService(ICamera2ModuleControl.class);
+
+ try {
+ camControl.setFlashBeamIntensity(0);
+ camControl.setLEDFlash(true);
+
+ FileOutputStream fos = new FileOutputStream(new File(
+ Constants.filePath));
+ fos.write(camera.grabFull());
+
+ System.out.println("Picture written to file at /tmp/image.jpeg");
+
+ camControl.setLEDFlash(false);
+
+ /**
+ * Upload the file!
+ */
+ ApiInteraction api = new ApiInteraction();
+ api.uploadFile();
+ } catch (IOException e) {
+ System.err
+ .println("Bugcam_helloworld error: Camera control is not accessible.");
+ e.printStackTrace();
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit 0e5a932

Please sign in to comment.