Last active
August 29, 2015 14:18
-
-
Save fjenett/a84c8bacf4d7bf7ab790 to your computer and use it in GitHub Desktop.
Piecemeta API with Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connecting to the Piecemeta API from Processing 3 | |
2015 @ Motion Bank CCL-3 Melbourne | |
http://piecemeta.com/ | |
https://motionbank.hackpad.com/collection/yQotmp9y1bR | |
You need to add these: | |
code/commons-codec-1.3.jar | |
code/commons-httpclient-3.1.jar | |
code/commons-logging-1.1.jar | |
code/commons-validator-1.4.0.jar | |
code/json.jar | |
Get them here: | |
http://archive.apache.org/dist/httpcomponents/commons-httpclient/binary/ | |
http://archive.apache.org/dist/commons/codec/binaries/ | |
http://commons.apache.org/proper/commons-logging/download_logging.cgi | |
http://commons.apache.org/proper/commons-validator/download_validator.cgi | |
https://code.google.com/p/org-json-java/downloads/list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import org.json.*; | |
import org.apache.commons.httpclient.*; | |
import org.apache.commons.httpclient.methods.*; | |
import org.apache.commons.httpclient.params.*; | |
import processing.core.*; | |
import java.lang.reflect.*; | |
class ApiRequest | |
{ | |
PApplet papplet; | |
Method packageLoadedMethod = null, | |
userLoadedMethod = null, | |
channelsLoadedMethod = null, | |
streamsLoadedMethod = null, | |
oneStreamLoadedMethod = null; | |
String baseUrl; | |
String httpMethod = "GET"; | |
ApiRequest( PApplet _papplet ) | |
{ | |
papplet = _papplet; | |
Method[] meths = papplet.getClass().getDeclaredMethods(); | |
for ( Method m : meths ) | |
{ | |
if ( m.getName().equals( "packageLoaded" ) ) packageLoadedMethod = m; | |
else if ( m.getName().equals( "userLoaded" ) ) userLoadedMethod = m; | |
else if ( m.getName().equals( "channelsLoaded" ) ) channelsLoadedMethod = m; | |
else if ( m.getName().equals( "streamsLoaded" ) ) streamsLoadedMethod = m; | |
else if ( m.getName().equals( "streamLoaded" ) ) oneStreamLoadedMethod = m; | |
} | |
} | |
void setBaseUrl ( String url ) | |
{ | |
baseUrl = new String(url); | |
} | |
void get ( String route ) | |
{ | |
httpMethod = "GET"; | |
request( route ); | |
} | |
void post ( String route ) | |
{ | |
httpMethod = "POST"; | |
request( route ); | |
} | |
void put ( String route ) | |
{ | |
httpMethod = "PUT"; | |
request( route ); | |
} | |
void delete ( String route ) | |
{ | |
httpMethod = "DELETE"; | |
request( route ); | |
} | |
void request ( String route ) | |
{ | |
System.out.println( route ); | |
Method meth = null; | |
String [] routeParts = route.split("/"); | |
if ( routeParts[0].equals("packages") ) { | |
if ( routeParts.length == 3 ) { | |
if ( routeParts[2].equals("channels") ) { | |
meth = channelsLoadedMethod; | |
} | |
} | |
else if ( routeParts.length == 2 ) meth = packageLoadedMethod; | |
} | |
else if ( routeParts[0].equals("users") ) meth = userLoadedMethod; | |
else if ( routeParts[0].equals("channels") ) { | |
if ( routeParts.length == 2 ) { | |
} else if ( routeParts[2].equals("streams") ) { | |
meth = streamsLoadedMethod; | |
} | |
} | |
else if ( routeParts[0].equals("streams") ) | |
{ | |
meth = oneStreamLoadedMethod; | |
} | |
new Thread( | |
new RequestImpl( baseUrl + "/" + route, papplet, meth ) | |
).start(); | |
} | |
class RequestImpl implements Runnable { | |
String url; | |
String method; | |
Object target; | |
Method callback; | |
RequestImpl ( String _url, Object _target, Method _callback ) { | |
url = new String (_url); | |
target = _target; | |
callback = _callback; | |
} | |
public void run () | |
{ | |
HttpClient client = new HttpClient(); | |
HttpMethodBase method = null; | |
if ( httpMethod.equals( "GET" ) ) { | |
GetMethod getMethod = new GetMethod( url ); | |
method = getMethod; | |
} else if ( httpMethod.equals( "POST" ) ) { | |
} else if ( httpMethod.equals( "PUT" ) ) { | |
} else if ( httpMethod.equals( "DELETE" ) ) { | |
} | |
method.addRequestHeader( new Header( "Content-Type", "application/json" ) ); | |
int statusCode = -1; | |
try { | |
statusCode = client.executeMethod(method); | |
} catch ( Exception e ) { | |
e.printStackTrace(); | |
} | |
// System.out.println( "Status code: " + statusCode ); | |
if ( statusCode == 200 ) | |
{ | |
String response = null; | |
InputStream io = null; | |
try { | |
io = method.getResponseBodyAsStream(); | |
} catch ( Exception e ) { | |
e.printStackTrace(); | |
} | |
BufferedReader bufferedReader = new BufferedReader( | |
new InputStreamReader( | |
io | |
) | |
); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line = null; | |
try { | |
while ( (line = bufferedReader.readLine()) != null ) | |
{ | |
stringBuilder.append(line + "\n"); | |
} | |
bufferedReader.close(); | |
} catch ( Exception e ) { | |
e.printStackTrace(); | |
} | |
response = stringBuilder.toString(); | |
//System.out.println( response ); | |
Object jsonResponse = null; | |
try { | |
if ( response.startsWith("{") ) { | |
jsonResponse = new JSONObject( response ); | |
} else if ( response.startsWith("[") ) { | |
jsonResponse = new JSONArray( response ); | |
} | |
} catch (Exception e ) { | |
e.printStackTrace(); | |
} | |
//System.out.println( jsonResponse ); | |
try { | |
callback.invoke( target, new Object[]{ jsonResponse } ); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import oscP5.*; | |
import netP5.*; | |
String baseUrl = "http://192.168.56.101"; | |
String packageUrl = "packages/eb471dcb-af2f-45a9-870c-909e069bccbf"; | |
ApiRequest api; | |
OscP5 oscP5; | |
int broadcastPort = 9090, listeningPort = 9091; | |
String broadcastHost = "localhost"; | |
NetAddress broadcastLocation; | |
float[][] travelPath; | |
int atFrame = 0; | |
float[][] history = null; | |
ArrayList streamsToLoad; | |
void setup () { | |
size( 200, 200 ); | |
oscP5 = new OscP5( this, listeningPort ); | |
broadcastLocation = new NetAddress( broadcastHost, 9090 ); | |
api = new ApiRequest( this ); | |
api.setBaseUrl( baseUrl ); | |
api.get( packageUrl ); | |
frameRate( 25 ); | |
} | |
void testSendOsc () { | |
OscMessage msg = new OscMessage("/test"); | |
msg.add(100); | |
oscP5.send(msg, new NetAddress( broadcastHost, listeningPort ) ); | |
} | |
void draw () { | |
background( 255 ); | |
if ( travelPath != null ) | |
{ | |
pushMatrix(); | |
translate( 20, height-20 ); | |
float x = travelPath[0][atFrame] * 10, | |
y = travelPath[1][atFrame] * 10; | |
if ( history == null ) { | |
history = new float[2][20]; | |
history[0][0] = x; | |
history[1][0] = y; | |
} else if ( dist(x,y,history[0][0],history[1][0]) > 5 ) { | |
float[][] tmp = new float[2][20]; | |
System.arraycopy(history[0],0,tmp[0],1,history[0].length-1); | |
System.arraycopy(history[1],0,tmp[1],1,history[1].length-1); | |
history = tmp; | |
history[0][0] = x; | |
history[1][0] = y; | |
} | |
noFill(); | |
stroke( 0 ); | |
beginShape(); | |
vertex( x,-y ); | |
for ( int i = 0; i < 20; i++ ) | |
{ | |
vertex( history[0][i], -history[1][i] ); | |
} | |
endShape(); | |
fill( 255, 0, 0 ); | |
noStroke(); | |
ellipse( x, -y, 3, 3 ); | |
popMatrix(); | |
fill( 0 ); | |
text( atFrame, 2, 12 ); | |
OscMessage msg = new OscMessage("/streams/3d-position/xy"); | |
msg.add( x ); | |
msg.add( y ); | |
oscP5.send( msg, broadcastLocation ); | |
atFrame++; | |
atFrame %= travelPath[0].length; | |
} | |
} | |
// ---------- | |
void oscEvent(OscMessage m) | |
{ | |
println( m.addrPattern() ); | |
} | |
// ---------- | |
void packageLoaded ( org.json.JSONObject packageJson ) throws Exception | |
{ | |
//println( packageJson ); | |
String userUUID = packageJson.getString("user_uuid"); | |
api.get( "users/" + userUUID ); | |
String packageUUID = packageJson.getString("uuid"); | |
api.get( "packages/" + packageUUID + "/channels" ); | |
} | |
void userLoaded ( org.json.JSONObject userJson ) throws Exception | |
{ | |
// println( userJson ); | |
} | |
void channelsLoaded ( org.json.JSONArray channelsJson ) throws Exception | |
{ | |
// println( channelsJson ); | |
for ( int i = 0, n = channelsJson.length(); i < n; i++ ) | |
{ | |
org.json.JSONObject channelJson = channelsJson.getJSONObject(i); | |
String channelUUID = channelJson.getString("uuid"); | |
String channelTitle = channelJson.getString("title"); | |
if ( channelTitle.startsWith("D01T01") ) | |
{ | |
api.get("channels/" + channelUUID + "/streams"); | |
return; | |
} | |
} | |
} | |
void streamsLoaded ( org.json.JSONArray streamsJson ) throws Exception | |
{ | |
streamsToLoad = new ArrayList<String>(); | |
for ( int i = 0, n = streamsJson.length(); i < n; i++ ) { | |
org.json.JSONObject streamJson = streamsJson.getJSONObject(i); | |
String title = streamJson.getString( "title" ); | |
streamsToLoad.add( streamJson.get("uuid") ); | |
} | |
api.get("streams/" + streamsToLoad.remove(0) ); | |
} | |
void streamLoaded ( org.json.JSONObject streamJson ) throws Exception | |
{ | |
float[][] frames3D = null; | |
int totalFrames; | |
String title = streamJson.getString("title"); | |
org.json.JSONArray frames = streamJson.getJSONArray( "frames" ); | |
if ( travelPath != null ) | |
{ | |
frames3D = new float[3][travelPath[0].length]; | |
System.arraycopy( travelPath, 0, frames3D, 0, travelPath.length ); | |
} | |
if ( frames3D == null ) { | |
totalFrames = frames.length()/2; // 50 fps to 25 fps … | |
frames3D = new float[3][totalFrames]; | |
} | |
int value = title.equals("x") ? 0 : ( title.equals("y") ? 1 : 2 ); | |
for ( int ii = 0, k = 0, ni = frames3D[value].length; ii < ni; ii++, k+=2 ) | |
{ | |
frames3D[value][ii] = (float)(frames.getDouble(k)); | |
} | |
if ( streamsToLoad.size() > 0 ) { | |
api.get("streams/" + streamsToLoad.remove(0) ); | |
} | |
travelPath = frames3D; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mode.id=processing.mode.java.JavaMode | |
mode=Java (2.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment