SparkJ: Control the ParticleCloud w/ Java
Disclaimer
I built this as a naive developer, it is not a good example of code and it is not a good idea to ever hard code your password as suggested in the documents. A proper program would use a token (not password) provided at runtime. I am only leaving this code here because it works and someone might find it usefull for a little weekend experiment.
Usage
SparkJ is a maven project and is available from the Maven Central repository. To use SparkJ add this to your pom.xml:
<dependency>
<groupId>com.github.grantwest.sparkj</groupId>
<artifactId>sparkj</artifactId>
<version>0.0.3</version>
</dependency>
Call functions and read variables:
SparkDevice device = new SparkDevice("50df6b0651675496402a02b7", "username", "password");
int funcResult = device.callFunction("functionName", "argsHere");
String varValue = device.readVariable("varName");
Subscribe to a device's event stream:
SparkDevice device = new SparkDevice("50df6b0651675496402a02b7", "username", "password");
SparkEventStream stream = device.eventStream((event) -> System.out.println(event.toString()));
//You will need to wait here because the event stream is asynchronous
It is also possible to get other event streams by using the myEvents() and publicEvents() methods of the SparkEventStream class.
Lastly, if you are instantiating many devices, this method will result in fewer HTTP requests:
SparkSession session = new SparkSession("username", "password");
SparkDevice dev1 = new SparkDevice("50df6b0651675496402a02b7", session);
SparkDevice dev2 = new SparkDevice("5496402a02b000df6b06516c", session);
Documentation
Full ParticleCloud documentation and examples on how to write your Particle device code can be found here: https://docs.particle.io/reference/api/