SHARE
TWEET

naive parser

iampiergiu Mar 12th, 2012 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  This sketch gets the data from the coupled Paraimpu actuator.          */
  4. /*  Thanks to Roberto Musanti                                              */
  5. /*                                                                         */
  6. /*  http://paraimpu.crs4.it                                                */
  7. /*                                                                         */
  8. /*                                                                         */
  9. /* counter = counts the readings                                           */
  10. /* url = actuator address                                                  */
  11. /* metro = tenths of a second of delay between readings                    */
  12. /*                                                                         */
  13. /*  This sketch is released under GPL v3. For more details, please read    */
  14. /*  http://www.gnu.org/licenses/gpl-3.0.txt                                */
  15. /***************************************************************************/
  16.  
  17.  
  18. import processing.net.*;
  19.  
  20. int counter;
  21. int metro;
  22. int module;
  23. String url;
  24. String data;
  25. ArrayList<String> words;
  26. char temp;
  27. String tmpstr;
  28.  
  29. void setup() {
  30.   counter = 0;
  31.   url = "http://paraimpu.crs4.it/use?token=0895060a-fa3e-4da4-acf0-6d3411754bcb";
  32.   metro = 10;
  33.   data = "";
  34.   words = new ArrayList();
  35.   module = floor(millis() / 100);
  36. }
  37.  
  38. void draw() {
  39.   data = polling(url, metro);
  40.  
  41.   /******************************************************************/
  42.   /*  add your code to process the read values and enjoy yourself!  */
  43.   /******************************************************************/
  44.   //lets extract the strings between " and " in the json
  45.   words=parse_string(data);
  46.  
  47.   //now that we have words, we elaborate them
  48.   elaborate_words(words);
  49.  
  50.   //we reset data containing json and start all over, also reset words
  51.   cleanAll();
  52.  
  53. }
  54.  
  55. //code for resetting stuff at every draw
  56. void cleanAll(){
  57.   data="";
  58.   words.clear();
  59. }
  60.  
  61. //code for elaborating words
  62. void elaborate_words(ArrayList<String> words){
  63.   //print only the string you want
  64.   String content="";
  65.   String author="";
  66.   if(words.size() >0){
  67.       for (int i=0; i< words.size();i++){
  68.         //println(words.get(i));
  69.        
  70.         if((words.get(i)).equals("text")){
  71.           content=words.get(++i);    
  72.         }
  73.         if((words.get(i)).equals("user")){
  74.           author=words.get(++i);
  75.         }    
  76.       }
  77.      
  78.       println(author);
  79.       println(content);
  80.       println("");
  81.   }
  82.  
  83. }
  84.  
  85.  
  86. //code for extractin string from the json string
  87. ArrayList<String> parse_string(String data){
  88.   ArrayList<String> working =new ArrayList();
  89.   for(int i=0; i< data.length();i++){
  90.       temp=data.charAt(i);
  91.       if (temp=='"') {
  92.         String tempPhrase="";        
  93.         i++;
  94.         while((temp=data.charAt(i)) != '"'){
  95.           tempPhrase=tempPhrase+temp;
  96.           i++;
  97.         }
  98.         i++;
  99.         //println(tempPhrase);
  100.         working.add(tempPhrase);
  101.       }
  102.   }  
  103.   return working;
  104. }
  105.  
  106.  
  107. /******************************************************************************/
  108. /* Polling Paraimpu URL. Until a new data is get, the last read is returned.  */
  109. /******************************************************************************/
  110. String polling(String url, int metro) {
  111.   String value = "";
  112.   if(floor(millis() / 100) == module) {
  113.     module = module + metro;
  114.     counter++;
  115.     //println("tick: " + counter + " - tenths of a second: " + floor(millis() / 100));
  116.     String lines[] = loadStrings(url);
  117.     if(lines != null && lines.length > 0){
  118.         for (int i = 0; i < lines.length; i++) {
  119.           value = value + lines[i];
  120.         //  println("New data - " + value);
  121.         }
  122.     }
  123.   }
  124.   if (value.length() < 1){
  125.       value = data;
  126.   }
  127.   return value;
  128. }
RAW Paste Data
Top