Beispiel #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()
}
Beispiel #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")
}
Beispiel #3
0
func (h *HomeKit) getDevices() {
	gadgets, err := GetGadgets(h.db)
	if err != nil {
		log.Fatal(err)
	}
	for _, g := range gadgets {
		p := path.Join(pth, g.Name)
		cfg := hc.Config{
			StoragePath: p,
			Pin:         h.key,
		}

		accessories := []*accessory.Accessory{}
		Register(g)
		if err := g.Fetch(); err != nil {
			log.Printf("not adding %s to homekit: %s\n", g.Name, err)
			continue
		}
		devices, err := g.Status()
		if err != nil {
			log.Printf("not adding %s to homekit: %s\n", g.Name, err)
			continue
		}
		for name, dev := range devices {
			if dev.Info.Direction == "output" {
				accessories = getOutputDevice(name, dev, g, accessories)
			} else if dev.Info.Direction == "input" {
				accessories = getInputDevice(name, dev, g, accessories)
			}
		}

		var t hc.Transport
		if len(accessories) == 1 {
			t, err = hc.NewIPTransport(cfg, accessories[0])
		} else if len(accessories) > 1 {
			t, err = hc.NewIPTransport(cfg, accessories[0], accessories[1:]...)
		} else {
			return
		}
		if err != nil {
			log.Println("not starting accesory", err)
		} else {
			go t.Start()
		}
	}
}
Beispiel #4
0
func GetHKSwitch(sensor *MPSensor) *HKSwitch {
	hkSwitch, found := switches[sensor.port]
	if found {
		return hkSwitch
	}

	label := fmt.Sprintf("mPower Port %d", sensor.port)
	log.Printf("[INFO] Creating New HKSwitch for %s", label)

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

	acc := accessory.NewSwitch(info)
	acc.Switch.On.SetValue(sensor.output)

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

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

	hkSwitch = &HKSwitch{acc, sensor, nil, transport}
	switches[sensor.port] = hkSwitch

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

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

	acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
		SetOutput(sensor, on)
	})

	return hkSwitch
}
Beispiel #5
0
func main() {
	switchInfo := accessory.Info{
		Name: "Lamp",
	}
	acc := accessory.NewSwitch(switchInfo)

	config := hc.Config{Pin: "12344321", Port: "12345", StoragePath: "./db"}
	t, err := hc.NewIPTransport(config, acc.Accessory)

	if err != nil {
		log.Info.Panic(err)
	}

	// Log to console when client (e.g. iOS app) changes the value of the on characteristic
	acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
		if on == true {
			log.Debug.Println("Client changed switch to on")
		} else {
			log.Debug.Println("Client changed switch to off")
		}
	})

	// Periodically toggle the switch's on characteristic
	go func() {
		for {
			on := !acc.Switch.On.GetValue()
			if on == true {
				log.Debug.Println("Switch is on")
			} else {
				log.Debug.Println("Switch is off")
			}
			acc.Switch.On.SetValue(on)
			time.Sleep(5 * time.Second)
		}
	}()

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

	t.Start()
}
Beispiel #6
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
}