1 package com.example.sycapp;
4 import java.io.InputStream;
5 import java.util.Vector;
6 import org.apache.http.HttpResponse;
7 import org.apache.http.client.methods.HttpPost;
8 import org.apache.http.client.methods.HttpPut;
9 import org.apache.http.entity.StringEntity;
10 import org.apache.http.impl.client.DefaultHttpClient;
11 import org.json.JSONArray;
12 import org.json.JSONException;
13 import org.json.JSONObject;
14 import android.os.AsyncTask;
16 public class XivelyUL { // Xively Uploader
18 //need to check things
19 private static final String url = "http://api.xively.com/v2/feeds/";
20 private static final String FEEDID = "2132088837";
21 private String feedID = "2132088837";
22 private static final String APIKEY = "gj9NLNCwZdUh7eI5ocDJJVgqFT3yT8MkhdFZpw8y8T0YFKGi";
23 private String apiKey = "gj9NLNCwZdUh7eI5ocDJJVgqFT3yT8MkhdFZpw8y8T0YFKGi";
24 private static String dataStream1 = "mbed1";
25 private static String dataStream2 = "mbed2";
26 private final int CAPACITY_PER_RQST = 500;
27 private int numInBuffer = 0;
28 private int value1 = 0;
29 private int value2 = 0;
30 private int ls_vl1 = 0; // for the current value thing
31 private int ls_vl2 = 0;
32 private boolean xvlEnable = true;
36 private JSONObject xivelyData;
37 private JSONArray mbed1Data;
38 private JSONArray mbed2Data;
39 private DefaultHttpClient xivelyRequester;
40 //private HttpPost xivelyPost;
41 private HttpPut xivelyUpload;
42 private HttpPost xivelyChannel;
46 mbed1Data = new JSONArray();
47 mbed2Data = new JSONArray();
48 xivelyData = new JSONObject();
49 xivelyRequester= new DefaultHttpClient();
51 //createChannel(dataStream1);
52 //createChannel(dataStream2);
56 /*public void XvlUlTest(){
58 String data = "{\"version\":\"1.0.0\",\"datastreams\" : [ {"
59 +"\"id\" : \"mbed1\",\"datapoints\":["
60 +"{\"at\":\"2013-04-22T00:35:43Z\",\"value\":\"42\"},"
61 +"{\"at\":\"2013-04-22T00:55:43Z\",\"value\":\"84\"},"
62 +"{\"at\":\"2013-04-22T01:15:43Z\",\"value\":\"41\"},"
63 +"{\"at\":\"2013-04-22T01:35:43Z\",\"value\":\"83\"}],"
64 +"\"current_value\" : \"40\"}]}";
66 StringEntity se = new StringEntity(data);
67 xivelyUpload.setEntity(se);
68 xivelyUpload.setHeader("Accept","application/json");
69 xivelyUpload.addHeader("X-ApiKey", apiKey);
70 ResponseHandler responseHandler = new BasicResponseHandler();
71 //xivelyRequester.execute(xivelyUpload, responseHandler);
72 new xivelyHelper().execute();
73 } catch (Exception e){
74 Util.debug("[X#2]"+e.toString());
78 public void setFeedID(String newID){
82 public void setApiKey(String newKey){
86 public void resetFeedID(){
90 public void resetApiKey(){
95 public void setEnable(boolean bool){
100 public void packSerialData(Vector<SerialData> sdata){
101 for(int i=0; i<sdata.size(); i++){
102 if(numInBuffer>=CAPACITY_PER_RQST) return;
103 packData(sdata.get(i));
105 Util.debug("[Buffer#2]"+Integer.toString(numInBuffer));
109 private void packData(SerialData data){
111 //TODO: deviceID stuff!!!
116 JSONObject dataPoint = new JSONObject();
117 dataPoint.put("at", data.timeStamp());
118 dataPoint.put("value",flip(data.devID));
119 mbed1Data.put(dataPoint);
121 }else if(data.devID==2){
125 JSONObject dataPoint = new JSONObject();
126 dataPoint.put("value",flip(data.devID));
127 dataPoint.put("at", data.timeStamp());
128 mbed2Data.put(dataPoint);
131 }catch (JSONException e) {
132 Util.debug("[X#0]"+e.toString());
138 public void createChannel(String newChannel){
139 new XivelyChannel().execute(url, newChannel);
145 public void uploadData(){
148 new XivelyUpload().execute(url);
150 Util.debug("[Buffer#1]"+Integer.toString(numInBuffer));
155 private void packToStream(){
156 //JSONObject dataFlow = new JSONObject();
157 JSONArray dataPoints= new JSONArray();
158 JSONObject eachPoints= new JSONObject();
161 eachPoints.put("id", dataStream1);
162 //eachPoints.put("current_value",flip(1));
163 eachPoints.put("datapoints", mbed1Data);
165 dataPoints.put(eachPoints);
166 eachPoints = new JSONObject();
169 eachPoints.put("id", dataStream2);
170 //eachPoints.put("current_value",flip(2));
171 eachPoints.put("datapoints", mbed2Data);
173 dataPoints.put(eachPoints);
175 xivelyData.put("datastreams", dataPoints);
176 xivelyData.put("version", "1.0.0");
177 Util.debug("[X#5]"+xivelyData.toString(1));
180 Util.debug("[X#1]"+e.toString());
183 mbed1Data = new JSONArray(); // clear uploaded datapoints
184 mbed2Data = new JSONArray();
188 private int flip(int mbed){
190 value1 = (value1==1)?0:1;
194 value2 = (value2==1)?0:1;
200 // Http Service Helper
201 private class XivelyUpload extends AsyncTask<String,Void,Void>{
204 protected Void doInBackground(String... url_) {
206 String s = url_[0]+feedID+".json";
208 xivelyUpload = new HttpPut(url_[0]+feedID+".json");
209 StringEntity se = new StringEntity(xivelyData.toString());
210 xivelyUpload.setEntity(se);
211 xivelyUpload.setHeader("Accept","application/json");
212 xivelyUpload.addHeader("X-ApiKey", apiKey);
214 HttpResponse response = xivelyRequester.execute(xivelyUpload);
215 InputStream inputStream = response.getEntity().getContent();
216 xivelyData = new JSONObject();
218 if(inputStream != null)
219 Util.debug("[X#3]"+inputStream.toString());
221 Util.debug("[X#3]Not working!!");
222 }catch (Exception e){
223 Util.debug("[X#4]"+e.toString());
229 private class XivelyChannel extends AsyncTask<String,Void,Void>{
232 protected Void doInBackground(String... params) {
234 JSONObject channelFrame = new JSONObject();
235 JSONObject chanRqstBody = new JSONObject();
236 chanRqstBody.put("tags", "");
237 chanRqstBody.put("id", params[1]);
238 chanRqstBody.put("unit", new JSONObject().put("symbol", "").put("label", ""));
239 channelFrame.put("datastreams",new JSONArray().put(chanRqstBody));
241 xivelyChannel = new HttpPost(params[0]+feedID+"/datastreams.json");
242 StringEntity se = new StringEntity(channelFrame.toString());
243 xivelyChannel.setEntity(se);
244 xivelyChannel.setHeader("Accept","application/json");
245 xivelyChannel.addHeader("X-ApiKey", apiKey);
247 HttpResponse response = xivelyRequester.execute(xivelyChannel);
248 InputStream inputStream = response.getEntity().getContent();
250 if(inputStream != null)
251 Util.debug("[X#3]"+inputStream.toString());
253 Util.debug("[X#3]Not working!!");
254 }catch (Exception e){
255 Util.debug("[X#4]"+e.toString());