Beispiel #1
0
// GetAllTimezones GET on /api/<username>/info/timezones
func GetAllTimezones(connection *common.Connection) ([]string, *common.ErrorHUE, error) {
	timezones := []string{}
	path := fmt.Sprintf("/api/" + connection.Username + "/info/timezones")
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return timezones, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return timezones, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &timezones)
	if err != nil {
		log.Errorf("Error with unmarshalling GetAllTimezones: %s", err.Error())
		return timezones, nil, err
	}
	return timezones, nil, nil
}
Beispiel #2
0
// GetLight GET on /api/<username>/lights/<id>
func GetLight(connection *common.Connection, id string) (*Light, *common.ErrorHUE, error) {
	light := &Light{}
	path := fmt.Sprintf("/api/" + connection.Username + "/lights/" + id)
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return light, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return light, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &light)
	if err != nil {
		log.Errorf("Error with unmarshalling GetLight: %s", err.Error())
		return light, nil, err
	}
	return light, nil, nil
}
Beispiel #3
0
// GetGroup GET on /api/<username>/groups/<id>
func GetGroup(connection *common.Connection, id string) (*Group, *common.ErrorHUE, error) {
	group := &Group{}
	path := fmt.Sprintf("/api/" + connection.Username + "/groups/" + id)
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return group, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return group, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &group)
	if err != nil {
		log.Errorf("Error with unmarshalling GetGroup: %s", err.Error())
		return group, nil, err
	}
	return group, nil, nil
}
Beispiel #4
0
// GetAllSensors GET on /api/<username>/sensors
func GetAllSensors(connection *common.Connection) (map[string]*Sensor, *common.ErrorHUE, error) {
	sensors := map[string]*Sensor{}
	path := fmt.Sprintf("/api/" + connection.Username + "/sensors")
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return sensors, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return sensors, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &sensors)
	if err != nil {
		log.Errorf("Error with unmarshalling GetAllSensors: %s", err.Error())
		return sensors, nil, err
	}
	return sensors, nil, nil
}
Beispiel #5
0
// GetScene GET on /api/<username>/scenes/<id>
func GetScene(connection *common.Connection, id string) (*Scene, *common.ErrorHUE, error) {
	scene := &Scene{}
	path := fmt.Sprintf("/api/" + connection.Username + "/scenes/" + id)
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return scene, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return scene, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &scene)
	if err != nil {
		log.Errorf("Error with unmarshalling GetScene: %s", err.Error())
		return scene, nil, err
	}
	return scene, nil, nil
}
Beispiel #6
0
// Get GET on /api/<username>/config
// see http://www.developers.meethue.com/documentation/configuration-api#72_get_configuration
func Get(connection *common.Connection) (*Config, *common.ErrorHUE, error) {
	var config Config
	path := fmt.Sprintf("/api/" + connection.Username + "/config")
	bodyResponse, errHUE, err := internal.Request(connection, "GET", http.StatusOK, path, nil)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return &config, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return &config, errHUE, err
	}
	err = json.Unmarshal(bodyResponse, &config)
	if err != nil {
		log.Errorf("Error with unmarshalling GetAllLights: %s", err.Error())
		return &config, nil, err
	}
	return &config, nil, nil
}
Beispiel #7
0
// CreateAPI POST on /api to create a new user
func CreateAPI(connection *common.Connection, create *Create) (*CreateResult, *common.ErrorHUE, error) {
	bodyRequest, err := json.Marshal(create)
	if err != nil {
		log.Errorf("Error with marshalling create: %s", err.Error())
		return &CreateResult{}, nil, err
	}
	bodyResponse, errHUE, err := internal.Request(connection, "POST", http.StatusOK, "/api/", bodyRequest)
	if errHUE != nil {
		log.Errorf("Error with requesting POST on /api (create a new user), HUE Error: %s", errHUE.Error.Description)
		return &CreateResult{}, errHUE, err
	}
	if err != nil {
		log.Errorf("Error with requesting POST on /api (create a new user): %s", err.Error())
		return &CreateResult{}, errHUE, err
	}
	var creates []CreateResult
	err = json.Unmarshal(bodyResponse, &creates)
	if err != nil {
		log.Errorf("Error with unmarshalling POST on /api (create a new user): %s", err.Error())
		return &CreateResult{}, nil, err
	}
	return &creates[0], nil, nil

}
Beispiel #8
0
// SetState PUT on /api/<username>/lights/<idLight>
func SetState(connection *common.Connection, id string, setState *SetStateValues) ([]interface{}, *common.ErrorHUE, error) {

	var ret []interface{}
	requestBody := make(map[string]interface{})

	if setState.Alert != "" {
		requestBody["alert"] = setState.Alert
	}
	if setState.Effect != "" {
		requestBody["effect"] = setState.Effect
	}
	if v, err := strconv.ParseBool(setState.On); err == nil {
		requestBody["on"] = v
	}

	toInt("bri", setState.Bri, requestBody)
	toInt("hue", setState.Hue, requestBody)
	toInt("sat", setState.Sat, requestBody)
	//toInt("xy", setState.XY, requestBody)
	toInt("ct", setState.Ct, requestBody)
	toInt("transitiontime", setState.TransitionTime, requestBody)
	toInt("bri_inc", setState.BriInc, requestBody)
	toInt("sat_inc", setState.SatInc, requestBody)
	toInt("hue_inc", setState.HueInc, requestBody)
	toInt("ct_inc", setState.CtInc, requestBody)
	toInt("xy_inc", setState.XYInc, requestBody)
	if setState.XY != "" {
		tuple := strings.Split(setState.XY, ",")
		if len(tuple) == 2 {
			x, e1 := strconv.ParseFloat(tuple[0], 64)
			y, e2 := strconv.ParseFloat(tuple[0], 64)
			if e1 == nil && e2 == nil {
				requestBody["xy"] = []float64{x, y}
			} else {
				return ret, nil, fmt.Errorf("Invalid value for xy")
			}
		}
	}

	bodyRequest, err := json.Marshal(requestBody)
	if err != nil {
		log.Errorf("Error with marshalling state: %s", err.Error())
		return ret, nil, err
	}

	path := fmt.Sprintf("/api/" + connection.Username + "/lights/" + id + "/state")
	bodyResponse, errHUE, err := internal.Request(connection, "PUT", http.StatusOK, path, bodyRequest)
	if errHUE != nil {
		log.Errorf("HUE Error: %s", errHUE.Error.Description)
		return ret, errHUE, err
	}
	if err != nil {
		log.Errorf("Error: %s", err.Error())
		return ret, errHUE, err
	}

	err = json.Unmarshal(bodyResponse, &ret)
	if err != nil {
		log.Errorf("Error with unmarshalling SetState: %s", err.Error())
		return ret, nil, err
	}
	return ret, nil, nil
}