]> Pileus Git - ~andy/csm213a-hw/blobdiff - syc/src/com/example/sycapp/XivelyUL.java
Add Android app from Yue
[~andy/csm213a-hw] / syc / src / com / example / sycapp / XivelyUL.java
diff --git a/syc/src/com/example/sycapp/XivelyUL.java b/syc/src/com/example/sycapp/XivelyUL.java
new file mode 100644 (file)
index 0000000..b36ed6b
--- /dev/null
@@ -0,0 +1,260 @@
+package com.example.sycapp;
+
+
+import java.io.InputStream;
+import java.util.Vector;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import android.os.AsyncTask;
+
+public class XivelyUL {                                        // Xively Uploader 
+
+       //need to check things
+       private static final String url = "http://api.xively.com/v2/feeds/";
+       private static final String FEEDID = "2132088837";
+       private              String feedID = "2132088837";
+       private static final String APIKEY = "gj9NLNCwZdUh7eI5ocDJJVgqFT3yT8MkhdFZpw8y8T0YFKGi";
+       private              String apiKey = "gj9NLNCwZdUh7eI5ocDJJVgqFT3yT8MkhdFZpw8y8T0YFKGi";
+       private static String dataStream1 = "mbed1";
+       private static String dataStream2 = "mbed2";
+       private final int CAPACITY_PER_RQST = 500;
+       private int numInBuffer = 0;
+       private int value1 = 0;
+       private int value2 = 0;
+       private int ls_vl1 = 0;                 // for the current value thing
+       private int ls_vl2 = 0;
+       private boolean xvlEnable = true;
+
+       
+       
+       private JSONObject xivelyData;
+       private JSONArray  mbed1Data;
+       private JSONArray  mbed2Data;
+       private DefaultHttpClient xivelyRequester;
+       //private HttpPost xivelyPost;
+       private HttpPut  xivelyUpload;
+       private HttpPost  xivelyChannel;
+       
+       
+       XivelyUL(){
+               mbed1Data      = new JSONArray();
+               mbed2Data      = new JSONArray();
+               xivelyData     = new JSONObject();
+               xivelyRequester= new DefaultHttpClient();
+               
+               //createChannel(dataStream1);
+               //createChannel(dataStream2);
+       }
+       
+       
+       /*public void XvlUlTest(){
+               
+               String data = "{\"version\":\"1.0.0\",\"datastreams\" : [ {"
+        +"\"id\" : \"mbed1\",\"datapoints\":["
+        +"{\"at\":\"2013-04-22T00:35:43Z\",\"value\":\"42\"},"
+        +"{\"at\":\"2013-04-22T00:55:43Z\",\"value\":\"84\"},"
+        +"{\"at\":\"2013-04-22T01:15:43Z\",\"value\":\"41\"},"
+        +"{\"at\":\"2013-04-22T01:35:43Z\",\"value\":\"83\"}],"
+        +"\"current_value\" : \"40\"}]}";
+               try {
+                       StringEntity se = new StringEntity(data);
+                       xivelyUpload.setEntity(se);
+                       xivelyUpload.setHeader("Accept","application/json");
+                       xivelyUpload.addHeader("X-ApiKey", apiKey);
+                       ResponseHandler responseHandler = new BasicResponseHandler();
+                       //xivelyRequester.execute(xivelyUpload, responseHandler);
+                       new xivelyHelper().execute();
+               } catch (Exception e){
+                       Util.debug("[X#2]"+e.toString());
+               }
+       }*/
+       
+       public void setFeedID(String newID){
+               feedID = newID;
+       }
+       
+       public void setApiKey(String newKey){
+               apiKey = newKey;
+       }
+       
+       public void resetFeedID(){
+               feedID = FEEDID;
+       }
+       
+       public void resetApiKey(){
+               apiKey = APIKEY;
+       }
+       
+       
+       public void setEnable(boolean bool){
+               xvlEnable = bool;
+       }
+       
+       // Pack Data
+       public void packSerialData(Vector<SerialData> sdata){
+               for(int i=0; i<sdata.size(); i++){
+                       if(numInBuffer>=CAPACITY_PER_RQST) return;
+                       packData(sdata.get(i));
+                       numInBuffer++;
+                       Util.debug("[Buffer#2]"+Integer.toString(numInBuffer));
+               }
+       }
+       
+       private void packData(SerialData data){
+               try {
+                       //TODO: deviceID stuff!!!
+                       if(data.devID==1){
+                               if (ls_vl1==-1){
+                                       ls_vl1=0;
+                               }else{
+                                       JSONObject dataPoint = new JSONObject();
+                                       dataPoint.put("at", data.timeStamp());
+                                       dataPoint.put("value",flip(data.devID));
+                                       mbed1Data.put(dataPoint);
+                               }
+                       }else if(data.devID==2){
+                               if (ls_vl2==-1){
+                                       ls_vl2=0;
+                               }else{
+                                       JSONObject dataPoint = new JSONObject();
+                                       dataPoint.put("value",flip(data.devID));
+                                       dataPoint.put("at", data.timeStamp());
+                                       mbed2Data.put(dataPoint);
+                               }
+                       }
+               }catch (JSONException e) {
+                       Util.debug("[X#0]"+e.toString());
+               }
+       }
+       
+       
+       
+       public void createChannel(String newChannel){
+               new XivelyChannel().execute(url, newChannel);
+       }
+       
+       
+       
+       // Data Upload
+       public void uploadData(){
+               if(numInBuffer>10){
+                       packToStream();
+                       new XivelyUpload().execute(url);
+                       numInBuffer = 0;
+                       Util.debug("[Buffer#1]"+Integer.toString(numInBuffer));
+               }
+       }
+       
+
+       private void packToStream(){
+               //JSONObject dataFlow  = new JSONObject();
+               JSONArray  dataPoints= new JSONArray();
+               JSONObject eachPoints= new JSONObject();
+               
+               try{
+                       eachPoints.put("id", dataStream1);
+                       //eachPoints.put("current_value",flip(1));
+                       eachPoints.put("datapoints", mbed1Data);
+                       
+                       dataPoints.put(eachPoints);
+                       eachPoints = new  JSONObject();
+                       
+                       //dataPoints.put
+                       eachPoints.put("id", dataStream2);
+                       //eachPoints.put("current_value",flip(2));
+                       eachPoints.put("datapoints", mbed2Data);
+                       
+                       dataPoints.put(eachPoints);
+                       
+                       xivelyData.put("datastreams", dataPoints);
+                       xivelyData.put("version", "1.0.0");
+                       Util.debug("[X#5]"+xivelyData.toString(1));
+               }
+               catch(Exception e){
+                       Util.debug("[X#1]"+e.toString());
+               }
+
+               mbed1Data = new JSONArray();            // clear uploaded datapoints
+               mbed2Data = new JSONArray();            
+       }
+       
+       
+       private int flip(int mbed){
+               if (mbed==1){
+                       value1 = (value1==1)?0:1;
+                       return value1;
+               }
+               if (mbed==2){
+                       value2 = (value2==1)?0:1;
+                       return value2;
+               }
+               return -1;
+       }
+
+       // Http Service Helper
+       private class XivelyUpload extends AsyncTask<String,Void,Void>{
+
+               @Override
+               protected Void doInBackground(String... url_)  {
+                       try{
+                               String s = url_[0]+feedID+".json";
+                               s = s+"";
+                               xivelyUpload    = new HttpPut(url_[0]+feedID+".json");
+                               StringEntity se = new StringEntity(xivelyData.toString());
+                               xivelyUpload.setEntity(se);
+                               xivelyUpload.setHeader("Accept","application/json");
+                               xivelyUpload.addHeader("X-ApiKey", apiKey);
+                               
+                               HttpResponse response   = xivelyRequester.execute(xivelyUpload);
+                               InputStream inputStream = response.getEntity().getContent();
+                               xivelyData = new JSONObject();
+                               
+                               if(inputStream != null)
+                                       Util.debug("[X#3]"+inputStream.toString());
+                               else
+                                       Util.debug("[X#3]Not working!!");
+                       }catch (Exception e){
+                               Util.debug("[X#4]"+e.toString());
+                       }
+                       return null;
+               }
+       }
+       
+       private class XivelyChannel extends AsyncTask<String,Void,Void>{
+
+               @Override
+               protected Void doInBackground(String... params)  {
+                       try{
+                               JSONObject channelFrame = new JSONObject();
+                               JSONObject chanRqstBody = new JSONObject();
+                               chanRqstBody.put("tags", "");
+                               chanRqstBody.put("id", params[1]);
+                               chanRqstBody.put("unit", new JSONObject().put("symbol", "").put("label", ""));
+                               channelFrame.put("datastreams",new JSONArray().put(chanRqstBody));
+                               
+                               xivelyChannel   = new HttpPost(params[0]+feedID+"/datastreams.json");
+                               StringEntity se = new StringEntity(channelFrame.toString());
+                               xivelyChannel.setEntity(se);
+                               xivelyChannel.setHeader("Accept","application/json");
+                               xivelyChannel.addHeader("X-ApiKey", apiKey);
+                               
+                               HttpResponse response   = xivelyRequester.execute(xivelyChannel);
+                               InputStream inputStream = response.getEntity().getContent();
+                               
+                               if(inputStream != null)
+                                       Util.debug("[X#3]"+inputStream.toString());
+                               else
+                                       Util.debug("[X#3]Not working!!");
+                       }catch (Exception e){
+                               Util.debug("[X#4]"+e.toString());
+                       }
+                       return null;
+               }
+       }
+}
\ No newline at end of file