func groupV1(w http.ResponseWriter, req *http.Request) {
	api_response := ApiResponse{Result: true, Message: "", StatusCode: http.StatusOK}
	req.ParseForm()
	query_params := req.Form
	if req.Method == "GET" {
		name, name_exists := query_params["name"]
		state, state_exists := query_params["state"]

		if name_exists {
			group_id := group_name_presets(name[0])
			if state_exists {
				gg := groups.NewGroup(hue_hostname, username_api_key)
				group_state := light_state_presets(state[0])
				gg.SetGroupState(group_id, group_state)
			} else {
				api_response.Result = false
				api_response.Message = "Invalid state."
				api_response.StatusCode = http.StatusUnauthorized
			}
		} else {
			api_response.Result = false
			api_response.Message = "Invalid id or name."
			api_response.StatusCode = http.StatusUnauthorized
		}
	} else {
		api_response.Result = false
		api_response.Message = "Not an HTTP GET."
		api_response.StatusCode = http.StatusForbidden
	}
	w.Header().Set("content-type", "application/json")
	w.WriteHeader(api_response.StatusCode)
	json_data, _ := json.Marshal(&api_response)
	w.Write([]byte(json_data))
}
Exemple #2
0
import (
	"fmt"
	MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
	"github.com/heatxsink/go-hue/src/groups"
	"github.com/heatxsink/go-hue/src/lights"
)

// Some config
var (
	topic       = "/lights/set"
	hueIP       = "192.168.1.5"
	hueUsername = "******"

	l = lights.NewLights(hueIP, hueUsername)
	g = groups.NewGroup(hueIP, hueUsername)
)

// Handles incoming MQTT messages
var onMessage MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) {
	switch msg.Payload()[0] {
	default:
		fmt.Printf("Unknown payload: %s\n", msg.Payload())
	case '0':
		g.SetGroupState(0, lights.State{On: false})
	case '1':
		g.SetGroupState(0, lights.State{On: true, Bri: 255, Ct: 370})
	case '2':
		g.SetGroupState(0, lights.State{On: true, Bri: 144, Sat: 211, Hue: 13122})
	case '3':
		g.SetGroupState(0, lights.State{On: true, Bri: 40, Sat: 211, Hue: 13122})