func main() { if apiKey != "" { pp, err := portal.GetPortal() if err != nil { fmt.Println("portal.GetPortal() ERROR: ", err) os.Exit(1) } ll := lights.New(pp[0].InternalIPAddress, apiKey) allLights, err := ll.GetAllLights() if err != nil { fmt.Println("lights.GetAllLights() ERROR: ", err) os.Exit(1) } fmt.Println() fmt.Println("Lights") fmt.Println("------") for _, l := range allLights { fmt.Printf("ID: %d Name: %s\n", l.ID, l.Name) } gg := groups.New(pp[0].InternalIPAddress, apiKey) allGroups, err := gg.GetAllGroups() if err != nil { fmt.Println("groups.GetAllGroups() ERROR: ", err) os.Exit(1) } fmt.Println() fmt.Println("Groups") fmt.Println("------") for _, g := range allGroups { fmt.Printf("ID: %d Name: %s\n", g.ID, g.Name) for _, lll := range g.Lights { fmt.Println("\t", lll) } previousState := g.Action _, err := gg.SetGroupState(g.ID, blinkState) if err != nil { fmt.Println("groups.SetGroupState() ERROR: ", err) os.Exit(1) } time.Sleep(time.Second * time.Duration(10)) _, err = gg.SetGroupState(g.ID, previousState) if err != nil { fmt.Println("groups.SetGroupState() ERROR: ", err) os.Exit(1) } } } else { usage() } }
func groupV1(w http.ResponseWriter, req *http.Request) { apiResponse := ApiResponse{Result: true, Message: "", StatusCode: http.StatusOK} req.ParseForm() queryParams := req.Form if req.Method == "GET" { name, nameExists := queryParams["name"] state, stateExists := queryParams["state"] if nameExists { groupID := group_name_presets(name[0]) if stateExists { gg := groups.New(hueHostname, hueUsername) groupState := light_state_factory.GetLightState(state[0]) gg.SetGroupState(groupID, groupState) } else { apiResponse.Result = false apiResponse.Message = "Invalid state." apiResponse.StatusCode = http.StatusUnauthorized } } else { apiResponse.Result = false apiResponse.Message = "Invalid id or name." apiResponse.StatusCode = http.StatusUnauthorized } } else { apiResponse.Result = false apiResponse.Message = "Not an HTTP GET." apiResponse.StatusCode = http.StatusForbidden } w.Header().Set("content-type", "application/json") w.WriteHeader(apiResponse.StatusCode) jsonData, err := json.Marshal(&apiResponse) if err != nil { glog.Errorf("Error: %s\n", err.Error()) } w.Write([]byte(jsonData)) }