Example #1
0
File: v2.go Project: pdf/golifx
func (p *V2) updateLocationGroup(dev device.GenericDevice) {
	groupID, err := dev.GetGroup()
	if err != nil {
		common.Log.Warnf("Error retrieving device group: %v", err)
		return
	}
	group, err := p.getGroup(groupID)
	if err != nil {
		common.Log.Warnf("Unknown group ID: %s", groupID)
		return
	}
	common.Log.Debugf("Adding device to group (%s): %v", groupID, dev.ID())
	if err = group.AddDevice(dev); err != nil {
		common.Log.Debugf("Error adding device to group: %v", err)
	}

	locationID, err := dev.GetLocation()
	if err != nil {
		common.Log.Warnf("Error retrieving device location: %v", err)
		return
	}
	p.RLock()
	location, ok := p.locations[locationID]
	p.RUnlock()
	if !ok {
		common.Log.Warnf("Unknown location ID: %v", locationID)
		return
	}
	common.Log.Debugf("Adding device to location (%s): %v", locationID, dev.ID())
	if err = location.AddDevice(dev); err != nil {
		common.Log.Debugf("Error adding device to location: %v", err)
	}
}
Example #2
0
File: v2.go Project: pdf/golifx
// classifyDevice either constructs a device.Light from the passed dev, or returns
// the dev untouched
func (p *V2) classifyDevice(dev device.GenericDevice) device.GenericDevice {
	common.Log.Debugf("Attempting to determine device type for: %d", dev.ID())
	vendor, err := dev.GetHardwareVendor()
	if err != nil {
		common.Log.Errorf("Error retrieving device hardware vendor: %v", err)
		return dev
	}
	product, err := dev.GetHardwareProduct()
	if err != nil {
		common.Log.Errorf("Error retrieving device hardware product: %v", err)
		return dev
	}

	defer dev.SetProvisional(false)

	switch vendor {
	case device.VendorLifx:
		switch product {
		case device.ProductLifxOriginal1000, device.ProductLifxColor650, device.ProductLifxWhite800LowVoltage, device.ProductLifxWhite800HighVoltage, device.ProductLifxWhite900BR30, device.ProductLifxColor1000BR30, device.ProductLifxColor1000:
			p.Lock()
			d := dev.(*device.Device)
			d.Lock()
			l := &device.Light{Device: d}
			common.Log.Debugf("Device is a light: %v", l.ID())
			// Replace the known dev with our constructed light
			p.devices[l.ID()] = l
			d.Unlock()
			p.Unlock()
			return l
		}
	}

	return dev
}
Example #3
0
File: v2.go Project: pdf/golifx
func (p *V2) addDevice(dev device.GenericDevice) {
	common.Log.Debugf("Attempting to add device: %d", dev.ID())
	d, err := p.getDevice(dev.ID())
	known := err == nil
	if known {
		dev = d
	} else {
		// We don't know this device, add it now and possibly overwrite it
		// later, this is necessary so that we can deliver responses to queries
		// for device type information
		p.Lock()
		p.devices[dev.ID()] = dev
		p.Unlock()
	}

	if dev.Provisional() {
		// Determine device type
		dev = p.classifyDevice(dev)
	}

	p.updateLocationGroup(dev)

	if known {
		common.Log.Debugf("Device already known: %d", dev.ID())
		return
	}

	sub, err := dev.NewSubscription()
	if err != nil {
		common.Log.Warnf("Error obtaining subscription from %d", dev.ID())
	} else {
		go p.broadcastLimiter(sub.Events())
	}

	common.Log.Debugf("Adding device to client: %d", dev.ID())
	if err := p.publish(common.EventNewDevice{Device: dev}); err != nil {
		common.Log.Errorf("Error adding device to client: %v", err)
		return
	}
	common.Log.Debugf("Added device to client: %d", dev.ID())
}