當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。