0

I have extracted live JavaScript data into processing from a public website, which updates its numbers every 4 seconds.

The numbers need to be changed to integers so they can be mapped to a live updated radius of an ellipse.

The following links are the closest to the solution and have not worked for me:

Processing: How to convert a char datatype into its utf-8 int representation? Convert int to char in java Java: parse int value from a char

My Code:

String url = "http://avail.mdx.ac.uk:8090/avail.js";
  String[] site = loadStrings(url);
   int[] list = int(split(site[0], ';')); 

 println(list[0]);
    println(list[1]);
    println(list[2]);

Prints: 
0
0
0

I am trying to get integers out of the characters printed from when I previously used

String[] list = split(site[0], ';');

in replacement of int[] list = int(split(site[0], ';'));

As a test u can swap it above to have a better understanding. All code goes into

void draw(){

}

I think it is a variable issue, 'int' doesn't seem to know where the data is meant to come from, though when I try to do:

String[] list = split(site[0], ';');

In the same file, although the error code: 'duplicate local variable appears'.

Any solution on getting my numerical characters out of the string into integers with best way to map to ellipse?

Community
  • 1
  • 1

2 Answers2

0

I do not know if this will solve your problem, but maybe it will point you in the right direction. The trick about encoding in java is converting strings to and from byte arrays..

byte[] dataFromJavaScript = {49,50,51,52};
String value = new String(dataFromJavaScript, Charset.forName("UTF-8"));
System.out.println(value);
Lars Juel Jensen
  • 1,643
  • 1
  • 22
  • 31
0

As far as I understand from the link, it is essentially this page you are trying to reproduce:

http://avail.mdx.ac.uk/

Also as far as I understand, the numbers you are trying to extract are the total and used numbers of computers, which seem to be the trailing numbers in the avail variable from the js file, after you split it where there's a ";". If you do split there you get Strings like "HESLGFOA01:84:32" which essentially seem to come down to "ID:Total:Used". Thus if you take that String again and split into its elements wherever you see a ":" you get an array of three elements (elm in the code below), ID:"HESLGFOA01", Total:"84", Used:"32". After that you only need to convert elements 1 and [2] to ints to get the numbers!

As a side note: The availgroup variable contains information about each room referencing the same ID mentioned above, thus when we find a string that contains that variable we stop the loop!

String url = "http://avail.mdx.ac.uk:8090/avail.js?";
// don't remove the questionmark otherwise the page won't update every time you run it
String [] site = loadStrings(url);
String [] list = split(site[0], ';');
for (int i = 0; i < list.length; i++) {
  if (list[i].contains("availgroup")) break; //stops the loop
  String [] elm = list[i].split(":");
  String ID = elm[0];
  int total = int(elm[1]);
  int used = int(elm[2]);
  println("Total: " + total + "  Used:" + used + "  Free: " + (total - used));
}

Here's a simplified version of the code with more comments to explain each step. This one only takes the first element (the first computer room) for simplicity

void setup() {
  // do this every 4 seconds as it is refreshed. We don't want to spam 
  // the website... This method changes how many times should draw() be
  // executed per second. The default value is 24 frames per second. Here
  // we set it to .25 which essentially means 1 draw() every 4 seconds...
  frameRate(0.25);
}
void draw() { 
  // set the background to a boring gray colour...
  background(204); 
  // set the website url. Don't forget the "http://"
  String url = "http://avail.mdx.ac.uk:8090/avail.js?"; 
  // get an array of big strings from the website, one element per line
  // as it is in the page there is actually only one line...
  String [] site = loadStrings(url); 
  // break the first line (site[0]) into an array of strings where
  // there is a ";" and store the array into the 'list' variable
  // Each element looks like this: "HESLGFOA01:84:32"
  String [] list = split(site[0], ';'); 
  // break the first element of list (list[0]) where there are ":" and 
  // store the result into the 'elm' variable
  String [] elm = list[0].split(":"); 
  // ID if the first element in the elm array (elm[0]): HESLGFOA01
  String ID = elm[0]; 
  // total is the second element in the elm array (elm[1]): 84
  int total = int(elm[1]); 
  // used is the third element in the elm array (elm[2]) 32
  int used = int(elm[2]); 
  // calculate the free desks by subtracting the used from the total
  int free = total - used;
  // print stuff / draw ellipses / do fun stuff
  println("Total: " + total + " Used:" + used + " Free: " + free); 
  float x1 = map(mouseX, 0, width, 50, 150); 
  ellipse(x1, 75, 50, 50); 
  float x2 = map(free, 0, width, 0, 200); 
  ellipse(x2, 125, 50, 50);
} 

To keep things together, this is the first piece of avail.js looks right now:

var avail="HESLBAOA01:61:20;HESLGFOA01:84:31;HESLGFLSS12A:16:6;HESLGFLSS12B:16:8;HESLGFLSS13A:16:0;HESLGFLSS13B:16:0;HESL01OA01L:52:22;HESL01LSS106:30:4;HESL01LSS111:10:3;HESL01LSS112:33:0;HESL01LSS118:59:26;HESL02OA01L:11:4;HESL02LSS206:11:4;HESL02LSS216:14:4;HESL02LSS217:35:5;HESL02LSS219:6:3;HESL03LSS304:17:1;HESL03LSS305:33:5;HEWIGFOA01:36:25;cal:9:0";

Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20
  • Yes... much appreciated that is the correct page and correct data, apologies for any unclear questioning. Thank you, though I am stuck on how to map() the integers from this code into a set of four ellipse() functions. – user3408462 Mar 13 '14 at 05:51
  • void draw(){ background(204); float x1 = map(mouseX, 0, width, 50, 150); ellipse(x1, 75, 50, 50); float x2 = map(free, 0, width, 0, 200); ellipse(x2, 125, 50, 50); String url = "http://avail.mdx.ac.uk:8090/avail.js?";String [] site = loadStrings(url); String [] list = split(site[0], ';'); for (int i = 0; i < list.length; i++) { if (list[i].contains("availgroup")) break; String [] elm = list[i].split(":"); String ID = elm[0]; int total = int(elm[1]); int used = int(elm[2]); println("Total: " + total + " Used:" + used + " Free: " + (total - used)); //exit(); } } – user3408462 Mar 13 '14 at 05:55
  • The code above is what I have tried to use and I get 'Cannot find anything' error. As I have replaced the mousex with the integer variable I would assume it would reference the ellipse to the integer inside the live data string. What is missing? Possible 'IF' statements? – user3408462 Mar 13 '14 at 06:00
  • Note the error: **Cannot find anything named "free"**. So what is the free variable? You never set it in your code so of course Processing cannot find it! It can not be "grabbed" or "referenced" on its own! It has to be defined before you use it sort of like this: **int free = total - used**. That of course means that **total** and **used** must be created before this calculation. I think you are missing some of the basics in coding in Processing and need to look up some tutorials. Stack overflow is not the place to go over the basics i'm afraid... – Petros Koutsolampros Mar 13 '14 at 09:59
  • I added a simplified and explained version to my answer, take a look – Petros Koutsolampros Mar 13 '14 at 10:24
  • Ok you are right I do deserve that was a rookie mistake. I was not able to write much i.e. which variable i used, as I was limited to characters inside the comment box. Dw I understand variables must reference. I was not diligent enough. The code has been very helpful. The reason why I was considering an 'IF' statement, as I would like to change the x and y diameter during any increase/decrease of 'int free' every four seconds. This is what I cannot find how to change diameter of ellipse by live updated integers. – user3408462 Mar 13 '14 at 11:37