Ejemplo n.º 1
0
func TestOnBrightness(t *testing.T) {
	action := actions.Action{
		On: true, Bri: maybe.NewUint8(135)}
	expected := []request{
		{L: 0, Bri: maybe.NewUint8(135), On: maybe.NewBool(true), D: 0}}
	verifyAction(t, expected, action)
}
Ejemplo n.º 2
0
Archivo: hue.go Proyecto: keep94/gohue
// Get gets the properties of a light. lightId is the ID of the light.
// properties is the returned properties.
// response is the raw response from the hue bridge or nil if communication
// failed. This function may return both a non-nil response and an error
// if the response from the hue bridge indicates an error. For most
// applications, it is enough just to look at properties and err.
func (c *Context) Get(lightId int) (
	properties *LightProperties, response []byte, err error) {
	request := &http.Request{
		Method: "GET",
		URL:    c.getLightUrl(lightId),
	}
	client := c.client
	var resp *http.Response
	if resp, err = client.Do(request); err != nil {
		return
	}
	defer resp.Body.Close()
	var respBuffer bytes.Buffer
	if _, err = respBuffer.ReadFrom(resp.Body); err != nil {
		return
	}
	response = respBuffer.Bytes()
	var jsonProps json_structs.LightState
	if err = json.Unmarshal(response, &jsonProps); err != nil {
		err = toError(response)
		return
	}
	if jsonProps.State != nil && len(jsonProps.State.XY) == 2 {
		state := jsonProps.State
		jsonColor := state.XY
		properties = &LightProperties{
			C:   NewMaybeColor(NewColor(jsonColor[0], jsonColor[1])),
			Bri: maybe.NewUint8(state.Bri),
			On:  maybe.NewBool(state.On)}
	} else {
		err = GeneralError
	}
	return
}
Ejemplo n.º 3
0
func maybeBlendBrightness(
	first, second maybe.Uint8, ratio float64) maybe.Uint8 {
	if first.Valid && second.Valid {
		return maybe.NewUint8(
			uint8((1.0-ratio)*float64(first.Value) + ratio*float64(second.Value) + 0.5))
	}
	return first
}
Ejemplo n.º 4
0
func TestMaybeUint8(t *testing.T) {
	var m, c maybe.Uint8
	var v uint8 = 7
	m.Set(v)
	if m != maybe.NewUint8(v) {
		t.Error("Uint8 Set broken.")
	}
	verifyString(t, "Just 7", m.String())
	m.Clear()
	if m != c {
		t.Error("Uint8 Clear broken.")
	}
	verifyString(t, "Nothing", m.String())
}
Ejemplo n.º 5
0
func TestGradient3(t *testing.T) {
	action := actions.Action{
		G: &actions.Gradient{
			Cds: []actions.ColorDuration{
				{Bri: maybe.NewUint8(gohue.Bright), D: 0},
				{Bri: maybe.NewUint8(gohue.Bright),
					C: gohue.NewMaybeColor(gohue.Red),
					D: 1000},
				{C: gohue.NewMaybeColor(gohue.Red), D: 2000},
				{Bri: maybe.NewUint8(gohue.Dim), D: 3000},
				{Bri: maybe.NewUint8(gohue.Dim), D: 4000}},
			Refresh: 500}}
	expected := []request{
		{L: 0, Bri: maybe.NewUint8(gohue.Bright), D: 0},
		{L: 0, Bri: maybe.NewUint8(gohue.Bright), D: 500},
		{L: 0,
			Bri: maybe.NewUint8(gohue.Bright),
			C:   gohue.NewMaybeColor(gohue.Red),
			D:   1000},
		{L: 0,
			Bri: maybe.NewUint8(gohue.Bright),
			C:   gohue.NewMaybeColor(gohue.Red),
			D:   1500},
		{L: 0, C: gohue.NewMaybeColor(gohue.Red), D: 2000},
		{L: 0, C: gohue.NewMaybeColor(gohue.Red), D: 2500},
		{L: 0, Bri: maybe.NewUint8(gohue.Dim), D: 3000},
		{L: 0, Bri: maybe.NewUint8(gohue.Dim), D: 3500},
		{L: 0, Bri: maybe.NewUint8(gohue.Dim), D: 4000}}
	verifyAction(t, expected, action)
}
Ejemplo n.º 6
0
func TestGradient(t *testing.T) {
	action := actions.Action{
		Lights: []int{2},
		G: &actions.Gradient{
			Cds: []actions.ColorDuration{
				{C: gohue.NewMaybeColor(gohue.NewColor(0.2, 0.1)),
					Bri: maybe.NewUint8(0), D: 0},
				{C: gohue.NewMaybeColor(gohue.NewColor(0.3, 0.3)),
					Bri: maybe.NewUint8(30), D: 1000},
				{C: gohue.NewMaybeColor(gohue.NewColor(0.9, 0.9)),
					Bri: maybe.NewUint8(100), D: 1000},
				{C: gohue.NewMaybeColor(gohue.NewColor(0.8, 0.7)),
					Bri: maybe.NewUint8(100), D: 1000},
				{C: gohue.NewMaybeColor(gohue.NewColor(0.2, 0.4)),
					Bri: maybe.NewUint8(10), D: 1750},
				{C: gohue.NewMaybeColor(gohue.NewColor(0.29, 0.46)),
					Bri: maybe.NewUint8(22), D: 2500}},
			Refresh: 500},
		On: true}
	expected := []request{
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.2, 0.1)),
			Bri: maybe.NewUint8(0),
			On:  maybe.NewBool(true),
			D:   0},
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.25, 0.2)),
			Bri: maybe.NewUint8(15),
			D:   500},
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.8, 0.7)),
			Bri: maybe.NewUint8(100),
			D:   1000},
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.4, 0.5)),
			Bri: maybe.NewUint8(40),
			D:   1500},
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.23, 0.42)),
			Bri: maybe.NewUint8(14),
			D:   2000},
		{L: 2,
			C:   gohue.NewMaybeColor(gohue.NewColor(0.29, 0.46)),
			Bri: maybe.NewUint8(22),
			D:   2500}}
	verifyAction(t, expected, action)
}