Exemplo n.º 1
0
func setupHomeKit() {
	aInfo := accessory.Info{
		Name:         "Hive Bridge",
		Manufacturer: "British Gas PLC",
	}
	a := accessory.New(aInfo, accessory.TypeBridge)

	tInfo := accessory.Info{
		Name:         "Heating",
		Manufacturer: "British Gas PLC",
	}
	t := accessory.NewThermostat(tInfo, 20.0, hive.MinTemp, hive.MaxTemp, 0.5)
	t.Thermostat.TargetTemperature.OnValueRemoteUpdate(targetTempChangeRequest)
	thermostat = t

	bInfo := accessory.Info{
		Name:         "Heating Boost",
		Manufacturer: "British Gas PLC",
	}
	b := accessory.NewSwitch(bInfo)
	b.Switch.On.OnValueRemoteUpdate(heatingBoostStateChangeRequest)
	heatingBoostSwitch = b

	sInfo := accessory.Info{
		Name:         "Hot Water",
		Manufacturer: "British Gas PLC",
	}
	h := accessory.NewSwitch(sInfo)
	h.Switch.On.OnValueRemoteUpdate(hotWaterStateChangeRequest)
	hotWaterSwitch = h

	config := hap.Config{
		Pin: pin,
	}

	var err error
	transport, err = hap.NewIPTransport(config, a, t.Accessory, b.Accessory, h.Accessory)
	if err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 2
0
func getOutputDevice(name string, dev gogadgets.Message, g Gadget, accessories []*accessory.Accessory) []*accessory.Accessory {
	if dev.Info.Type == "thermostat" {
		return getThermostat(name, dev, g, accessories)
	}
	info := accessory.Info{
		Name:         name,
		Manufacturer: "gogadgets",
	}
	s := accessory.NewSwitch(info)
	connect(s, g, name)
	return append(accessories, s.Accessory)
}
func TestPutCharacteristic(t *testing.T) {
	info := accessory.Info{
		Name:         "My Switch",
		SerialNumber: "001",
		Manufacturer: "Google",
		Model:        "Bridge",
	}

	a := accessory.NewSwitch(info)
	a.Switch.On.SetValue(false)

	m := accessory.NewContainer()
	m.AddAccessory(a.Accessory)

	// find on characteristic with type TypeOn
	var cid int64
	for _, s := range a.Accessory.Services {
		for _, c := range s.Characteristics {
			if c.Type == characteristic.TypeOn {
				cid = c.ID
			}
		}
	}

	if cid == 0 {
		t.Fatal("characteristic not found")
	}

	char := data.Characteristic{AccessoryID: 1, CharacteristicID: cid, Value: true}
	var slice []data.Characteristic
	slice = append(slice, char)

	chars := data.Characteristics{Characteristics: slice}
	b, err := json.Marshal(chars)

	if err != nil {
		t.Fatal(err)
	}

	var buffer bytes.Buffer
	buffer.Write(b)

	controller := NewCharacteristicController(m)
	err = controller.HandleUpdateCharacteristics(&buffer, characteristic.TestConn)

	if err != nil {
		t.Fatal(err)
	}

	if is, want := a.Switch.On.GetValue(), true; is != want {
		t.Fatalf("is=%v want=%v", is, want)
	}
}
Exemplo n.º 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
}
Exemplo n.º 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()
}