vrijdag 11 oktober 2013

Philips Hue gets Groovy

My first Groovy script when figuring out how to use the Philips Hue lights.
Details on the Philips Hue Api can be found here.


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
           }
}
}
}

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.