Beispiel #1
0
func main() {
	// Install HAP devices
	x10Accessories := X10Devices()
	particleAccessories := ParticleDevices()

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

	bridge := accessory.NewLightBulb(model.Info{
		Name:         "Bridge",
		Manufacturer: "Evan",
	})
	t, err := hap.NewIPTransport("10000000", 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")
}
Beispiel #2
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 := model.Info{
			Name:         device.Name,
			Manufacturer: "X10",
		}

		light := accessory.NewLightBulb(info)
		light.OnStateChanged(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.OnBrightnessChanged(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
}
Beispiel #3
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 := model.Info{
			Name:         device.Name,
			Manufacturer: "Particle",
		}

		light := accessory.NewLightBulb(info)
		light.OnStateChanged(func(on bool) {
			if on {
				callParticleFunction(device, "animate", "Rainbow,100,255")
			} else {
				callParticleFunction(device, "set_color", "0,0,0")
			}
		})
		light.OnBrightnessChanged(func(val int) {
			cl := colorful.Hsv(light.GetHue(), light.GetSaturation()/100, float64(light.GetBrightness())/100)
			callParticleFunction(device, "set_color", fmt.Sprintf("%v,%v,%v", int(cl.R*255), int(cl.G*255), int(cl.B*255)))
		})
		light.OnHueChanged(func(val float64) {
			cl := colorful.Hsv(light.GetHue(), light.GetSaturation()/100, float64(light.GetBrightness())/100)
			callParticleFunction(device, "set_color", fmt.Sprintf("%v,%v,%v", int(cl.R*255), int(cl.G*255), int(cl.B*255)))
		})
		light.OnSaturationChanged(func(val float64) {
			cl := colorful.Hsv(light.GetHue(), light.GetSaturation()/100, float64(light.GetBrightness())/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
}
Beispiel #4
0
func main() {
	info := model.Info{
		Name:         "Personal Light Bulb",
		Manufacturer: "Matthias",
	}

	light := accessory.NewLightBulb(info)
	light.OnStateChanged(func(on bool) {
		if on == true {
			turnLightOn()
		} else {
			turnLightOff()
		}
	})

	t, err := hap.NewIPTransport("32191123", light.Accessory)
	if err != nil {
		log.Fatal(err)
	}

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

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

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

	lightBulb := accessory.NewLightBulb(info)

	power, _ := light.GetPower()
	lightBulb.SetOn(power)

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

	lightBulb.SetBrightness(int(brightness))
	lightBulb.SetSaturation(saturation)
	lightBulb.SetHue(hue)

	transport, err := hap.NewIPTransport(pin, lightBulb.Accessory)
	if err != nil {
		log.Fatal(err)
	}

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

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

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

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

	lightBulb.OnStateChanged(func(power bool) {
		log.Printf("[INFO] Changed State for %s", label)
		light.SetPower(power)
	})

	updateColor := func(light common.Light) {
		// HAP: [0...360]
		// LIFX: [0...MAX_UINT16]
		hue := lightBulb.GetHue()

		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		saturation := lightBulb.GetSaturation()

		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		brightness := lightBulb.GetBrightness()

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

		lifxHue := math.MaxUint16 * float64(hue) / float64(characteristic.MaxHue)
		lifxSaturation := math.MaxUint16 * float64(saturation) / float64(characteristic.MaxSaturation)
		lifxBrightness := math.MaxUint16 * float64(brightness) / float64(characteristic.MaxBrightness)

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

		light.SetColor(color, 500*time.Millisecond)
	}

	lightBulb.OnHueChanged(func(value float64) {
		log.Printf("[INFO] Changed Hue for %s to %d", label, value)
		updateColor(light)
	})

	lightBulb.OnSaturationChanged(func(value float64) {
		log.Printf("[INFO] Changed Saturation for %s to %d", label, value)
		updateColor(light)
	})

	lightBulb.OnBrightnessChanged(func(value int) {
		log.Printf("[INFO] Changed Brightness for %s to %d", label, value)
		updateColor(light)
	})

	return hkLight
}
Beispiel #6
0
func lightForBulb(bulb *lifx.Bulb) *lifxLight {
	label := bulb.GetLabel()
	light, found := lights[label]
	if found == true {
		fmt.Println("Found")
		return light
	}

	fmt.Println("Create new switch for blub")

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

	light_bulb := accessory.NewLightBulb(info)
	light_bulb.OnIdentify(func() {
		timeout := 1 * time.Second
		toggleBulb(bulb)
		time.Sleep(timeout)
		toggleBulb(bulb)
		time.Sleep(timeout)
		toggleBulb(bulb)
		time.Sleep(timeout)
		toggleBulb(bulb)
	})

	light_bulb.OnStateChanged(func(on bool) {
		if on == true {
			client.LightOn(bulb)
			log.Println("Switch is on")
		} else {
			client.LightOff(bulb)
			log.Println("Switch is off")
		}
	})

	updateColors := func(client *lifx.Client, bulb *lifx.Bulb) {
		// TODO define max variables in Gohap

		// HAP: [0...360]
		// LIFX: [0...MAX_UINT16]
		hue := light_bulb.GetHue()

		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		saturation := light_bulb.GetSaturation()
		// HAP: [0...100]
		// LIFX: [0...MAX_UINT16]
		brightness := light_bulb.GetBrightness()
		// [2500..9000]
		kelvin := HSBKKelvinDefault

		lifx_brightness := math.MaxUint16 * float64(brightness) / 100
		lifx_saturation := math.MaxUint16 * float64(saturation) / 100
		lifx_hue := math.MaxUint16 * float64(hue) / 360

		log.Println("Brightness", lifx_brightness)
		log.Println("Hue", lifx_saturation)
		log.Println("Saturation", lifx_hue)
		client.LightColour(bulb, uint16(lifx_hue), uint16(lifx_saturation), uint16(lifx_brightness), uint16(kelvin), 0x0500)
	}

	light_bulb.OnBrightnessChanged(func(value int) {
		log.Println("Brightness", value)
		updateColors(client, bulb)
	})

	light_bulb.OnSaturationChanged(func(value float64) {
		log.Println("Saturation", value)
		updateColors(client, bulb)
	})

	light_bulb.OnHueChanged(func(value float64) {
		log.Println("Hue", value)
		updateColors(client, bulb)
	})

	transport, err := hap.NewIPTransport(pin, light_bulb.Accessory)
	if err != nil {
		log.Fatal(err)
	}

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

	light = &lifxLight{transport, light_bulb, light_bulb.Accessory}
	lights[label] = light

	return light
}
Beispiel #7
0
func handleNewLight(light common.Light) (err error) {
	id := light.ID()

	_, exists := lights[id]
	if exists {
		log.Debugf("A light with the ID '%s' has already been added", id)
		return nil
	}

	subscription, err := light.NewSubscription()
	if err != nil {
		log.WithField(`error`, err).Error(`Subscribing to light events`)
		return err
	}

	label, _ := light.GetLabel()

	log.Infof("Adding light [%s]", label)

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

	haLight := accessory.NewLightBulb(info)

	lights[id] = lightMeta{
		light:        light,
		subscription: subscription,
		haLight:      haLight,
	}

	// Get the initial state of the light and communicate it via HomeKit
	updateHaPowerState(light, haLight)
	updateHaColors(light, haLight)

	events := subscription.Events()
	go func() {
		for {
			select {
			case event := <-events:
				switch event := event.(type) {
				case common.EventUpdateColor:
					log.Infof("Light: %s, Event: Update Color", id)
					updateHaColors(light, haLight)
				case common.EventUpdateLabel:
					// TODO: Find out how to update the name of a homekit device
					log.Infof("Light: %s, Event: Update Label", id)
				case common.EventUpdatePower:
					log.Infof("Light: %s, Event: Update Power", id)
					updateHaPowerState(light, haLight)
				default:
					log.Debugf("Unhandled event on light: %+v", event)
					continue
				}
			}
		}
	}()

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

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

	haLight.OnStateChanged(func(on bool) {
		go func() {
			light.SetPower(on)
		}()
	})

	haLight.OnBrightnessChanged(func(value int) {
		go func() {
			updateLightColors(light, haLight)
		}()
	})

	haLight.OnSaturationChanged(func(value float64) {
		go func() {
			updateLightColors(light, haLight)
		}()
	})

	haLight.OnHueChanged(func(value float64) {
		go func() {
			updateLightColors(light, haLight)
		}()
	})

	transport, err := hap.NewIPTransport(pin, haLight.Accessory)
	if err != nil {
		log.Fatal(err)
	}

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

	return nil
}