func NewLight(driver *Driver, D int, name string, port *arduino.Arduino) error { light, err := devices.CreateLightDevice(driver, &model.Device{ NaturalID: fmt.Sprintf("%s-%d", config.Serial(), D), NaturalIDType: "block-arduino", Name: &name, Signatures: &map[string]string{ "ninja:manufacturer": "Ninja Blocks Inc", "ninja:productType": "Light", "ninja:thingType": "light", }, }, driver.Conn) if err != nil { log.FatalError(err, "Could not create light device") } if err := light.EnableOnOffChannel(); err != nil { log.FatalError(err, "Could not enable hue on-off channel") } if err := light.EnableBrightnessChannel(); err != nil { log.FatalError(err, "Could not enable hue brightness channel") } if err := light.EnableColorChannel("temperature", "hue"); err != nil { log.FatalError(err, "Could not enable color channel") } port.OnDeviceData(func(data arduino.DeviceData) { if data.D == D { spew.Dump("Light Data!", data) } }) return nil }
func (d *LifxDriver) newLight(bulb *lifx.Bulb) (*devices.LightDevice, error) { //TODO cut this down! name := bulb.GetLabel() d.log.Infof("Making light with ID: %s Label: %s", bulb.GetLifxAddress(), name) light, err := devices.CreateLightDevice(d, &model.Device{ NaturalID: bulb.GetLifxAddress(), NaturalIDType: "lifx", Name: &name, Signatures: &map[string]string{ "ninja:manufacturer": "Lifx", "ninja:productName": "Lifx Bulb", "ninja:productType": "Light", "ninja:thingType": "light", }, }, d.conn) if err != nil { d.log.FatalError(err, "Could not create light device") } light.ApplyOnOff = func(state bool) error { var err error if state { err = d.client.LightOn(bulb) } else { err = d.client.LightOff(bulb) } if err != nil { return fmt.Errorf("Failed to set on-off state: %s", err) } return nil } light.ApplyLightState = func(state *devices.LightDeviceState) error { jsonState, _ := json.Marshal(state) d.log.Infof("Sending light state to lifx bulb: %s", jsonState) if state.OnOff != nil { err := light.ApplyOnOff(*state.OnOff) if err != nil { return err } } if state.Color != nil || state.Brightness != nil || state.Transition != nil { // pull out the origin state bulbState := bulb.GetState() if state.Brightness == nil { brightness := float64(bulbState.Brightness) / math.MaxUint16 state.Brightness = &brightness } if state.Color == nil { // save the brightness brightness := state.Brightness state = convertState(&bulbState) state.Brightness = brightness } if state.Transition == nil { state.Transition = &defaultTransition } /*if state.Color == nil { return fmt.Errorf("Color value missing from batch set") } if state.Brightness == nil { return fmt.Errorf("Brightness value missing from batch set") }*/ switch state.Color.Mode { case "hue": return d.client.LightColour( bulb, uint16(*state.Color.Hue*math.MaxUint16), uint16(*state.Color.Saturation*math.MaxUint16), uint16(*state.Brightness*math.MaxUint16), 0, uint32(*state.Transition), ) case "xy": //TODO: Lifx does not support XY color return fmt.Errorf("XY color mode is not yet supported in the Lifx driver") case "temperature": d.client.LightColour( bulb, 0, 0, uint16(*state.Brightness*math.MaxUint16), uint16(*state.Color.Temperature), uint32(*state.Transition), ) default: return fmt.Errorf("Unknown color mode %s", state.Color.Mode) } } return nil } bulb.SetStateHandler(buildStateHandler(d, bulb, light)) if err := light.EnableOnOffChannel(); err != nil { d.log.FatalError(err, "Could not enable lifx on-off channel") } if err := light.EnableBrightnessChannel(); err != nil { d.log.FatalError(err, "Could not enable lifx brightness channel") } if err := light.EnableColorChannel("temperature", "hue"); err != nil { d.log.FatalError(err, "Could not enable lifx color channel") } if err := light.EnableTransitionChannel(); err != nil { d.log.FatalError(err, "Could not enable lifx transition channel") } // extra channels for sensors illuminance := channels.NewIlluminanceChannel() if err := d.conn.ExportChannel(light, illuminance, "illuminance"); err != nil { d.log.FatalError(err, "Could not enable lifx illuminance channel") } d.log.Debugf("register illuminance channel for %s", bulb.GetLifxAddress()) d.devices[bulb.GetLifxAddress()] = &lifxDevice{illuminance} return light, nil }
func (d *HueDriver) newLight(bulb *hue.Light) (*HueLightContext, error) { //TODO cut this down! name := bulb.Name d.log.Infof("Making light on Bridge: %s ID: %s Label: %s", d.bridge.UniqueId, bulb.Id, name) attrs, _ := d.user.GetLightAttributes(bulb.Id) d.log.Debugf("Found hue bulb %s - %s", dump(*bulb), dump(attrs)) sigs := map[string]string{ "ninja:productType": "Light", "ninja:thingType": "light", "hue:bridge": d.user.Bridge.UniqueId, "hue:modelId": attrs.ModelId, "hue:type": attrs.Type, "hue:swversion": attrs.SoftwareVersion, } if attrs.ModelId != "ZLL Light" { sigs["ninja:manufacturer"] = "Phillips" sigs["ninja:productName"] = "Hue" } light, err := devices.CreateLightDevice(d, &model.Device{ NaturalID:/* SHOULD HAVE BRIDGE ID IN HERE!! d.user.Bridge.UniqueId + "-" +*/ bulb.Id, NaturalIDType: "hue", Name: &name, Signatures: &sigs, }, d.conn) if err != nil { d.log.FatalError(err, "Could not create light device") } //func NewLight(l *hue.Light, bus *ninja.DriverBus, bridge *hue.Bridge, user *hue.User) (*HueLightContext, error) { hl := &HueLightContext{ ID: bulb.Id, Name: bulb.Name, Bridge: d.bridge, User: d.user, Light: light, log: logger.GetLogger(fmt.Sprintf("huelight:%s", bulb.Id)), } if err := light.EnableOnOffChannel(); err != nil { d.log.FatalError(err, "Could not enable hue on-off channel") } if err := light.EnableBrightnessChannel(); err != nil { d.log.FatalError(err, "Could not enable hue brightness channel") } if attrs.State.ColorMode != "" { if err := light.EnableColorChannel("temperature", "hue"); err != nil { d.log.FatalError(err, "Could not enable hue color channel") } hl.colorEnabled = true } if err := light.EnableTransitionChannel(); err != nil { d.log.FatalError(err, "Could not enable hue transition channel") } light.ApplyIdentify = hl.ApplyIdentify if err := light.EnableIdentifyChannel(); err != nil { d.log.FatalError(err, "Could not enable identify channel") } light.ApplyLightState = hl.ApplyLightState return hl, hl.updateState() }