JJAppConnector for Processing is actually very simple to use. The general setup is:
- Create an instance of AppConnector with your key
- Specify your publications and subscriptions
- Kick things off by using start()
- Publish your variables and access your subscriptions
- Have fun!
## Constructor
Construct an instance of the AppConnector-class by passing as an argument the key which you got after registering an app:
String appKey = "1234567890abcdefg";
AppConnector app = new AppConnector(appKey);
Checks whether your application is connected to the server, i.e.
if (app.isConnected()) {
System.out.print("I am connected!");
}
Checks whether an app with the specified appTitle is currently online and running, i.e.
if (app.isConnected("david")) {
System.out.print("David's app is connected");
}
Kick off things by:
app.start()
Please remember that by now you have to specify your publications and subscriptions before using start
Define a publication for your application. (By now you have to use this function before kicking things off with start() ) As first parameter, specify a name for the publication. As second parameter, specify a description which will be displayed within the app list (starting with the returnType in brackets). Let's say we want to register a publication that is a random integer between 1 and 100:
app.addPublication("random", "(int) a random value between 1 and 100");
To publish a value, use publish()
## subscribeTo(_appTitle.varName_[, _shortcut_ ])If you want to have access to the value of an app's publication, you need to subscribe to it by using app.subscribeTo("appName.variableName")
. For example, if you want to subscribe to David's random-publication, use
app.subscribeTo("david.random");
You could now access the value of the subscription by using app.get("david.random").getValue()
or the shortcut app.get("random").getValue()
.
Please note that you can define your own shortcuts by passing in a shortcut argument. This is especially useful if multiple apps are using the same publication names. For example I could subscribe to David's random-publication this way
app.subscribeTo("david.random", "d-random");
and then access the value by app.get("d-random")
.
Publish a value and push it to the server. Pass in the publication name and the value you want to publish. Using the "random" example from above:
app.publish("random", floor(random(1, 101)));
Please note that you can only publish to publications which you have specified! See addPublication() for more information.
## get(_appTitle.varName_ || _shortcut_).getValue()Access the current value of one of your subscriptions by either appTitle.varName
or a shortcut
. Using the example above this could look like
app.get("david.random").getValue();
or
app.get("random").getValue();
or (if the shortcut has been specified)
app.get("d-random").getValue();
Please note that the method getValue()
returns the current value as an instance of java.lang.Object.
There are, though, several helper methods which let you directly cast the value.
Let's take a look at an example.
Instead of writing
Object randomObj = app.get("david.random").getValue();
int i = (Integer) randomObj; // this could throw a ClassCastExeption or NullPointerException
you can write
int randInt = app.get("david.random").toInt();
Following methods are currently available:
Casts to String
. If Object is null, an empty String will be returned.
Casts to boolean
. If Object is null, false
will be returned. If the Object is numeric and greater than 0, true
will be returned, else false
.
Casts to int
. If Object is null, 0 will be returned. If the String-value of the Object equals "true", 1 will be returned.
Casts to double
. See toInt()
above.
Casts to float
. See toInt()
above.
Tries to convert the data to an instance of PImage
(see PImage Processing reference). If Object is null, a new instance of PImage will be returned.
For more information on subscriptions and shortcuts, see subscribeTo().
Prints value
to the console if debug mode is on.
Enable or disable debug mode. If disabled, no messages will be logged.
Returns true
if debug mode is on, else false
.
Returns the version of AppConnector you are using as a float
-value.
System.print.out("Version is " + app.version()); // prints out "Version is 0.1"