本文整理汇总了Golang中github.com/pdf/golifx/common.Light.NewSubscription方法的典型用法代码示例。如果您正苦于以下问题:Golang Light.NewSubscription方法的具体用法?Golang Light.NewSubscription怎么用?Golang Light.NewSubscription使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pdf/golifx/common.Light
的用法示例。
在下文中一共展示了Light.NewSubscription方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleNewLight
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()
}()
//.........这里部分代码省略.........