当前位置: 首页>>代码示例>>Golang>>正文


Golang Light.NewSubscription方法代码示例

本文整理汇总了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()
	}()
//.........这里部分代码省略.........
开发者ID:hsystr,项目名称:lifx-homekit,代码行数:101,代码来源:lifx-homekit.go


注:本文中的github.com/pdf/golifx/common.Light.NewSubscription方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。