import groovyx.net.http.*
@Grab(group='org.codehaus.groovy.modules.http-builder',
module='http-builder', version='0.5.2' )
class HueLamp {
int id
HueLamp(int id = 1) {
this.id = id
}
def status(boolean on, int hue) {
def base = "http://192.168.2.32/"
def api = "api/newdeveloper/lights/$id/state"
def url = base + api
def http = new HTTPBuilder( url)
http.request( Method.PUT, ContentType.JSON ) { req ->
body = ["on" : on, "transitiontime" : 1,
"hue" : hue, "sat" : 255, "bri" : 255 ]
response.success = { resp, json ->
println json
}
response.failure = { resp ->
println resp
}
}
}
}
class HueLamp {
int id
HueLamp(int id = 1) {
this.id = id
}
def status(boolean on, int hue) {
def base = "http://192.168.2.32/"
def api = "api/newdeveloper/lights/$id/state"
def url = base + api
def http = new HTTPBuilder( url)
http.request( Method.PUT, ContentType.JSON ) { req ->
body = ["on" : on, "transitiontime" : 1,
"hue" : hue, "sat" : 255, "bri" : 255 ]
response.success = { resp, json ->
println json
}
response.failure = { resp ->
println resp
}
}
}
}
Details on how to use the Groovy Http builder are found here.
The status method updates the lamp hue. Below an example of looping over different colors:
def hueMax = 65535
def stepSize = (int)hueMax/nrSteps
def hues = (0..25).collect{ it*stepSize }
def lamp = new HueLamp(1)
hues.each{ hue ->
lamp.status(true, hue)
Thread.sleep(100)
}
Interested an need more code? Check out this java/groovy project on Github from Stephan Jaetzold.
Enjoy.