Quickstart - nico9889/AlexaHueBridge GitHub Wiki
Create a new bridge
To create a new bridge you need to build a Bridge object. Bridge object require a port for the web server necessary to handle HTTP API call. Note: if you want to use it with Echo devices, remember to set port 80 otherwise it won't work! Note: port 80 requires elevated privileges on some OSes
Bridge b = Bridge.createWithSelectNif(80);
Create your Callback function
This function is invoked every time the state changes.
The function must accept a Device as argument and return nothing. In this way, you can read/write your virtual device properties (access to this may changes in the near future) from the Callback.
public class Main(){
public static void hello(Device device){
System.out.println("Hello world!");
System.out.println("I'm " + device.name + " and my id is " + device.id);
}
}
If you want, Callback is a functional interface, so you can use a lambda function instead:
Callback c = device -> {
System.out.println("Hello world!");
System.out.println("I'm " + d.name + " and my id is " + d.id);
};
Create a new virtual device
You are ready to create a virtual device, you need to make this through the Bridge object you built before to the bridge can manage stuff for you to make it work correctly. You can set a device name, your own Callback through method reference or passing the Callback directly and the device Type. You can create up to 256 virtual devices.
Device a = b.addDevice("Custom name that you want", Main::hello, Type.ExtendedColor); // Method reference example
Device b = b.addDevice("Same ^", c, Type.WhiteSpectrum); // Callback example
b.addDevice
method will return a Device object so if you need you can access this even outside the Callback.
Starting the bridge
That's all, now you just need to start everything by calling
b.start();
this will make your application listen for SSDP incoming UDP packet and start the Web API server to handle the request in different threads.
Note: the first version of the bridge will ask you to choose in your standard output you the network interface and the local IP address that you want to use to manage the requests. Future version will remove this limitation.
Complete example code
import Bridge.Bridge;
import Devices.Callback;
import Devices.Device;
import Devices.Type;
public class Main(){
public static void hello(Device d){
System.out.println("Hello world!"); // This will print "Hello world" on device state change
}
public static void main(String[] args){
// Your code here
Bridge b = null;
try{
b = Bridge.createWithSelectNif(80); // This create a new Bridge listening on port 80/TCP
}catch(IOException io){
// Creating a new bridge may throw IOException, make sure to properly handle this!
io.printStackTrace();
System.exit(-1);
}
Callback c = device -> System.out.println("Lambda! Hello world!"); // Lambda example
Device d1 = b.addDevice("Method device", Main::hello, Type.ExtendedColor); // First device. Method reference callback
Device d2 = b.addDevice("Lambda device", c, Type.ExtendedColor); // Second device. Functional interface callback
b.start();
// Your code here
}
}