Example #1
0
func main() {
	info := accessory.Info{
		Name:         "Personal Light Bulb",
		Manufacturer: "Matthias",
	}

	acc := accessory.NewLightbulb(info)

	acc.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
		if on == true {
			turnLightOn()
		} else {
			turnLightOff()
		}
	})

	t, err := hc.NewIPTransport(hc.Config{Pin: "32191123"}, acc.Accessory)
	if err != nil {
		log.Fatal(err)
	}

	hc.OnTermination(func() {
		t.Stop()
	})

	t.Start()
}
Example #2
0
func main() {
	// Install HAP devices
	x10Accessories := X10Devices()
	particleAccessories := ParticleDevices()

	accessories := append(x10Accessories, particleAccessories...)

	bridge := accessory.NewLightbulb(accessory.Info{
		Name:         "Bridge",
		Manufacturer: "Evan",
	})
	config := hc.Config{Pin: "10000000"}
	t, err := hc.NewIPTransport(config, bridge.Accessory, accessories...)
	if err != nil {
		log.Fatal(err)
	}

	t.Start()

	// Setup REST API
	m := martini.Classic()
	m.Get("/", func() string {
		return "Hello world!"
	})
	m.RunOnAddr(":5591")
}
Example #3
0
func X10Devices() []*accessory.Accessory {
	file, err := ioutil.ReadFile("./config/x10.json")
	if err != nil {
		log.Fatalln("Error opening X10 config file", err.Error())
	}

	var bulbs []Device
	if err = json.Unmarshal(file, &bulbs); err != nil {
		log.Fatalln("Error parsing X10 config file", err.Error())
	}

	lightBulbs := []*accessory.Accessory{}

	for _, b := range bulbs {
		var device Device = b
		log.Printf("Creating X10 accessory \"%v\"...\n", device.Name)

		info := accessory.Info{
			Name:         device.Name,
			Manufacturer: "X10",
		}

		light := accessory.NewLightbulb(info)
		light.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
			var action string
			if on {
				action = "on"
			} else {
				action = "off"
			}
			var method string
			if device.Dimmable {
				method = "pl"
			} else {
				method = "rf"
			}

			cmd := fmt.Sprintf("%s %s%v %s", method, device.HouseCode, device.DeviceID, action)
			writeCommand(cmd)
		})
		light.Lightbulb.Brightness.OnValueRemoteUpdate(func(val int) {
			if device.Dimmable {
				dimVal := int((float64(val) / 100) * 70)
				cmd := fmt.Sprintf("pl %s%v xdim %v", device.HouseCode, device.DeviceID, dimVal)
				writeCommand(cmd)
			}
		})

		lightBulbs = append(lightBulbs, light.Accessory)
	}

	return lightBulbs
}
Example #4
0
func ParticleDevices() []*accessory.Accessory {
	file, err := ioutil.ReadFile("./config/particle.json")
	if err != nil {
		log.Fatalln("Error opening Particle config file", err.Error())
	}

	var cores []Core
	if err = json.Unmarshal(file, &cores); err != nil {
		log.Fatalln("Error parsing Particle config file", err.Error())
	}

	lightBulbs := []interface{}{}

	for _, b := range cores {
		var device Core = b
		log.Printf("Creating Particle accessory \"%v\"...\n", device.Name)

		info := accessory.Info{
			Name:         device.Name,
			Manufacturer: "Particle",
		}

		light := accessory.NewLightbulb(info)
		light.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
			if on {
				callParticleFunction(device, "animate", "Rainbow,100,255")
			} else {
				callParticleFunction(device, "set_color", "0,0,0")
			}
		})
		light.Lightbulb.Brightness.OnValueRemoteUpdate(func(val int) {
			cl := colorful.Hsv(light.Lightbulb.Hue.GetValue(), light.Lightbulb.Saturation.GetValue()/100, float64(light.Lightbulb.Brightness.GetValue()/100))
			callParticleFunction(device, "set_color", fmt.Sprintf("%v,%v,%v", int(cl.R*255), int(cl.G*255), int(cl.B*255)))
		})
		light.Lightbulb.Hue.OnValueRemoteUpdate(func(val float64) {
			cl := colorful.Hsv(light.Lightbulb.Hue.GetValue(), light.Lightbulb.Saturation.GetValue()/100, float64(light.Lightbulb.Brightness.GetValue()/100))
			callParticleFunction(device, "set_color", fmt.Sprintf("%v,%v,%v", int(cl.R*255), int(cl.G*255), int(cl.B*255)))
		})
		light.Lightbulb.Saturation.OnValueRemoteUpdate(func(val float64) {
			cl := colorful.Hsv(light.Lightbulb.Hue.GetValue(), light.Lightbulb.Saturation.GetValue()/100, float64(light.Lightbulb.Brightness.GetValue()/100))
			callParticleFunction(device, "set_color", fmt.Sprintf("%v,%v,%v", int(cl.R*255), int(cl.G*255), int(cl.B*255)))
		})

		lightBulbs = append(lightBulbs, light.Accessory)
	}

	accessories := make([]*accessory.Accessory, len(lightBulbs))
	for i, bulb := range lightBulbs {
		accessories[i] = bulb.(*accessory.Accessory)
	}

	return accessories
}
Example #5
0
func GetHKLight(light common.Light) *HKLight {
	hkLight, found := lights[light.ID()]
	if found {
		return hkLight
	}

	label, _ := light.GetLabel()
	log.Debug.Printf("Creating New HKLight for %s", label)

	info := accessory.Info{
		Name:         label,
		Manufacturer: "LIFX",
	}

	acc := accessory.NewLightbulb(info)

	power, _ := light.GetPower()
	acc.Lightbulb.On.SetValue(power)

	color, _ := light.GetColor()
	hue, saturation, brightness := ConvertLIFXColor(color)

	acc.Lightbulb.Brightness.SetValue(int(brightness))
	acc.Lightbulb.Saturation.SetValue(saturation)
	acc.Lightbulb.Hue.SetValue(hue)

	config := hc.Config{Pin: pin}
	transport, err := hc.NewIPTransport(config, acc.Accessory)
	if err != nil {
		log.Info.Panic(err)
	}

	go func() {
		transport.Start()
	}()

	hkLight = &HKLight{acc, transport, nil}
	lights[light.ID()] = hkLight

	acc.OnIdentify(func() {
		timeout := 1 * time.Second

		for i := 0; i < 4; i++ {
			ToggleLight(light)
			time.Sleep(timeout)
		}
	})

	acc.Lightbulb.On.OnValueRemoteUpdate(func(power bool) {
		log.Debug.Printf("Changed State for %s", label)
		light.SetPowerDuration(power, transitionDuration)
	})

	updateColor := func(light common.Light) {
		currentPower, _ := light.GetPower()

		// HAP: [0...360]
		// LIFX: [0...MAX_UINT16]
		hue := acc.Lightbulb.Hue.GetValue()

		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		saturation := acc.Lightbulb.Saturation.GetValue()

		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		brightness := acc.Lightbulb.Brightness.GetValue()

		// [HSBKKelvinMin..HSBKKelvinMax]
		kelvin := HSBKKelvinDefault

		lifxHue := math.MaxUint16 * float64(hue) / float64(HueMax)
		lifxSaturation := math.MaxUint16 * float64(saturation) / float64(SaturationMax)
		lifxBrightness := math.MaxUint16 * float64(brightness) / float64(BrightnessMax)

		color := common.Color{
			uint16(lifxHue),
			uint16(lifxSaturation),
			uint16(lifxBrightness),
			kelvin,
		}

		light.SetColor(color, transitionDuration)

		if brightness > 0 && !currentPower {
			log.Debug.Printf("Color changed for %s, turning on power.", label)
			light.SetPowerDuration(true, transitionDuration)
		} else if brightness == 0 && currentPower {
			log.Debug.Printf("Color changed for %s, but brightness = 0 turning off power.", label)
			light.SetPower(false)
		}
	}

	acc.Lightbulb.Hue.OnValueRemoteUpdate(func(value float64) {
		log.Debug.Printf("Changed Hue for %s to %f", label, value)
		updateColor(light)
	})

	acc.Lightbulb.Saturation.OnValueRemoteUpdate(func(value float64) {
		log.Debug.Printf("Changed Saturation for %s to %f", label, value)
		updateColor(light)
	})

	acc.Lightbulb.Brightness.OnValueRemoteUpdate(func(value int) {
		log.Debug.Printf("Changed Brightness for %s to %d", label, value)
		updateColor(light)
	})

	return hkLight
}