Example #1
0
func setupClient(c *cobra.Command, args []string) {
	var err error

	client, err = golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		logger.WithField(`error`, err).Fatalln(`Failed initializing client`)
	}
}
Example #2
0
// Instantiating a new client with protocol V2, with default options
func ExampleNewClient_v2() common.Light {
	client, err := golifx.NewClient(&protocol.V2{})
	if err != nil {
		panic(err)
	}
	light, err := client.GetLightByLabel(`lightLabel`)
	if err != nil {
		panic(err)
	}
	return light
}
Example #3
0
func initClient() error {
	var err error

	client, err = golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		log.WithField(`error`, err).Error(`Creating LIFX client`)
		return err
	}

	return nil
}
Example #4
0
// When using protocol V2, it's possible to enable reliable operations.  This is
// highly recommended, otherwise sends operate in fire-and-forget mode, meaning
// we don't know if they actually arrived at the target or not.  Whilst this is
// faster and generates less network traffic, in my experience LIFX devices
// aren't the most reliable at accepting the packets they're sent, so sometimes
// we need to retry.
func ExampleNewClient_v2reliable() {
	client, err := golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		panic(err)
	}
	light, err := client.GetLightByLabel(`lightLabel`)
	if err != nil {
		panic(err)
	}

	fmt.Println(light.ID())
}
Example #5
0
// When using protocol V2, it's possible to choose an alternative client port.
// This is not recommended unless you need to use multiple client instances at
// the same time.
func ExampleNewClient_v2port() {
	client, err := golifx.NewClient(&protocol.V2{Port: 56701})
	if err != nil {
		panic(err)
	}
	light, err := client.GetLightByLabel(`lightLabel`)
	if err != nil {
		panic(err)
	}

	fmt.Println(light.ID())
}
Example #6
0
func initClient() error {
	var err error

	client, err = golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		log.WithField(`error`, err).Error(`Creating LIFX client`)
		return err
	}

	subscription, err = client.NewSubscription()
	if err != nil {
		log.WithField(`error`, err).Error(`Subscribing to events`)
		return err
	}

	events := subscription.Events()
	go func() {
		for {
			select {
			case event := <-events:
				switch event := event.(type) {
				case common.EventNewDevice:
					// TODO: In the future it might not be a light ...
					light, err := client.GetLightByID(event.Device.ID())
					if err == nil {
						handleNewLight(light)
					}
				case common.EventExpiredDevice:
					// TODO: In the future it might not be a light ...
					light, err := client.GetLightByID(event.Device.ID())
					if err == nil {
						handleExpiredLight(light)
					}
				default:
					log.Debugf("Unhandled event on client: %+v", event)
					continue
				}
			}
		}
	}()

	return nil
}
Example #7
0
func newLight() (*light, error) {
	// Get debug output for LIFX device
	//logger := logrus.New()
	//logger.Out = os.Stderr
	//logger.Level = logrus.DebugLevel
	//golifx.SetLogger(logger)

	client, err := golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		return nil, err
	}
	client.SetDiscoveryInterval(5 * time.Minute)

	device, err := client.GetLightByLabel("BuildBulb")
	if err != nil {
		util.Log.Error("Could not find any lamp with label 'BuildBulb'")
		return nil, err
	}
	return &light{client, device}, nil
}
Example #8
0
func Connect() {
	client, err := golifx.NewClient(&protocol.V2{Reliable: true})
	if err != nil {
		log.Fatalf("[ERR] Failed to initiliaze the client: %s", err)
	}

	client.SetDiscoveryInterval(30 * time.Second)

	sub, _ := client.NewSubscription()
	for {
		event := <-sub.Events()
		switch event.(type) {
		case common.EventNewLocation:
			log.Printf("[INFO] Discovered Location %s", event.(common.EventNewLocation).Location.GetLabel())
		case common.EventNewGroup:
			log.Printf("[INFO] Discovered Group %s", event.(common.EventNewGroup).Group.GetLabel())
		case common.EventNewDevice:
			label, _ := event.(common.EventNewDevice).Device.GetLabel()
			log.Printf("[INFO] Discovered Device %s", label)

			go NewDevice(event.(common.EventNewDevice).Device)

		case common.EventExpiredLocation:
			log.Printf("[INFO] Expired Location %s", event.(common.EventExpiredLocation).Location.GetLabel())
		case common.EventExpiredGroup:
			log.Printf("[INFO] Expired Group %s", event.(common.EventExpiredGroup).Group.GetLabel())
		case common.EventExpiredDevice:
			label, _ := event.(common.EventExpiredDevice).Device.GetLabel()
			log.Printf("[INFO] Expired Device %s", label)

			ExpireDevice(event.(common.EventExpiredDevice).Device)

		default:
			log.Printf("[INFO] Unknown Client Event: %T", event)
		}
	}
}