]> Pileus Git - ~andy/csm213a-hw/blob - syc/src/com/example/sycapp/XivelyUL.java
Add Android app from Yue
[~andy/csm213a-hw] / syc / src / com / example / sycapp / XivelyUL.java
1 package com.example.sycapp;
2
3
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;
15
16 public class XivelyUL {                                 // Xively Uploader 
17
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;
33
34         
35         
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;
43         
44         
45         XivelyUL(){
46                 mbed1Data      = new JSONArray();
47                 mbed2Data      = new JSONArray();
48                 xivelyData     = new JSONObject();
49                 xivelyRequester= new DefaultHttpClient();
50                 
51                 //createChannel(dataStream1);
52                 //createChannel(dataStream2);
53         }
54         
55         
56         /*public void XvlUlTest(){
57                 
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\"}]}";
65                 try {
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());
75                 }
76         }*/
77         
78         public void setFeedID(String newID){
79                 feedID = newID;
80         }
81         
82         public void setApiKey(String newKey){
83                 apiKey = newKey;
84         }
85         
86         public void resetFeedID(){
87                 feedID = FEEDID;
88         }
89         
90         public void resetApiKey(){
91                 apiKey = APIKEY;
92         }
93         
94         
95         public void setEnable(boolean bool){
96                 xvlEnable = bool;
97         }
98         
99         // Pack Data
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));
104                         numInBuffer++;
105                         Util.debug("[Buffer#2]"+Integer.toString(numInBuffer));
106                 }
107         }
108         
109         private void packData(SerialData data){
110                 try {
111                         //TODO: deviceID stuff!!!
112                         if(data.devID==1){
113                                 if (ls_vl1==-1){
114                                         ls_vl1=0;
115                                 }else{
116                                         JSONObject dataPoint = new JSONObject();
117                                         dataPoint.put("at", data.timeStamp());
118                                         dataPoint.put("value",flip(data.devID));
119                                         mbed1Data.put(dataPoint);
120                                 }
121                         }else if(data.devID==2){
122                                 if (ls_vl2==-1){
123                                         ls_vl2=0;
124                                 }else{
125                                         JSONObject dataPoint = new JSONObject();
126                                         dataPoint.put("value",flip(data.devID));
127                                         dataPoint.put("at", data.timeStamp());
128                                         mbed2Data.put(dataPoint);
129                                 }
130                         }
131                 }catch (JSONException e) {
132                         Util.debug("[X#0]"+e.toString());
133                 }
134         }
135         
136         
137         
138         public void createChannel(String newChannel){
139                 new XivelyChannel().execute(url, newChannel);
140         }
141         
142         
143         
144         // Data Upload
145         public void uploadData(){
146                 if(numInBuffer>10){
147                         packToStream();
148                         new XivelyUpload().execute(url);
149                         numInBuffer = 0;
150                         Util.debug("[Buffer#1]"+Integer.toString(numInBuffer));
151                 }
152         }
153         
154
155         private void packToStream(){
156                 //JSONObject dataFlow  = new JSONObject();
157                 JSONArray  dataPoints= new JSONArray();
158                 JSONObject eachPoints= new JSONObject();
159                 
160                 try{
161                         eachPoints.put("id", dataStream1);
162                         //eachPoints.put("current_value",flip(1));
163                         eachPoints.put("datapoints", mbed1Data);
164                         
165                         dataPoints.put(eachPoints);
166                         eachPoints = new  JSONObject();
167                         
168                         //dataPoints.put
169                         eachPoints.put("id", dataStream2);
170                         //eachPoints.put("current_value",flip(2));
171                         eachPoints.put("datapoints", mbed2Data);
172                         
173                         dataPoints.put(eachPoints);
174                         
175                         xivelyData.put("datastreams", dataPoints);
176                         xivelyData.put("version", "1.0.0");
177                         Util.debug("[X#5]"+xivelyData.toString(1));
178                 }
179                 catch(Exception e){
180                         Util.debug("[X#1]"+e.toString());
181                 }
182
183                 mbed1Data = new JSONArray();            // clear uploaded datapoints
184                 mbed2Data = new JSONArray();            
185         }
186         
187         
188         private int flip(int mbed){
189                 if (mbed==1){
190                         value1 = (value1==1)?0:1;
191                         return value1;
192                 }
193                 if (mbed==2){
194                         value2 = (value2==1)?0:1;
195                         return value2;
196                 }
197                 return -1;
198         }
199
200         // Http Service Helper
201         private class XivelyUpload extends AsyncTask<String,Void,Void>{
202
203                 @Override
204                 protected Void doInBackground(String... url_)  {
205                         try{
206                                 String s = url_[0]+feedID+".json";
207                                 s = s+"";
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);
213                                 
214                                 HttpResponse response   = xivelyRequester.execute(xivelyUpload);
215                                 InputStream inputStream = response.getEntity().getContent();
216                                 xivelyData = new JSONObject();
217                                 
218                                 if(inputStream != null)
219                                         Util.debug("[X#3]"+inputStream.toString());
220                                 else
221                                         Util.debug("[X#3]Not working!!");
222                         }catch (Exception e){
223                                 Util.debug("[X#4]"+e.toString());
224                         }
225                         return null;
226                 }
227         }
228         
229         private class XivelyChannel extends AsyncTask<String,Void,Void>{
230
231                 @Override
232                 protected Void doInBackground(String... params)  {
233                         try{
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));
240                                 
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);
246                                 
247                                 HttpResponse response   = xivelyRequester.execute(xivelyChannel);
248                                 InputStream inputStream = response.getEntity().getContent();
249                                 
250                                 if(inputStream != null)
251                                         Util.debug("[X#3]"+inputStream.toString());
252                                 else
253                                         Util.debug("[X#3]Not working!!");
254                         }catch (Exception e){
255                                 Util.debug("[X#4]"+e.toString());
256                         }
257                         return null;
258                 }
259         }
260 }