Ejemplo n.º 1
0
func NewFakeLight(driver ninja.Driver, id int) *FakeLight {
	name := fmt.Sprintf("Fancy Fake Light %d", id)

	light := &FakeLight{
		driver: driver,
		info: &model.Device{
			NaturalID:     fmt.Sprintf("light%d", id),
			NaturalIDType: "fake",
			Name:          &name,
			Signatures: &map[string]string{
				"ninja:manufacturer": "Fake Co.",
				"ninja:productName":  "FakeLight",
				"ninja:productType":  "Light",
				"ninja:thingType":    "light",
			},
		},
	}

	light.onOffChannel = channels.NewOnOffChannel(light)
	light.brightnessChannel = channels.NewBrightnessChannel(light)
	light.colorChannel = channels.NewColorChannel(light)
	light.temperatureChannel = channels.NewTemperatureChannel(light)

	go func() {

		var temp float64
		for {
			time.Sleep(5 * time.Second)
			temp += 0.5
			light.temperatureChannel.SendState(temp)
		}
	}()

	return light
}
Ejemplo n.º 2
0
func (d *LightDevice) EnableColorChannel(supportedModes ...string) error {
	if len(supportedModes) == 0 {
		log.Errorf("You must support at least one color mode")
	}
	if !containsString(supportedModes, "hue") {
		log.Errorf("You must support at least hue color values")
	}
	d.colorModes = supportedModes
	d.color = channels.NewColorChannel(d)
	return d.conn.ExportChannel(d, d.color, "color")
}
Ejemplo n.º 3
0
func (c *ColorChannel) init() error {
	log.Debugf("Initialising color channel of device %d", *c.device.deviceInfo.IeeeAddress)

	/*clusterID := uint32(0x06)
	  attributeID := uint32(0)
	  minReportInterval := uint32(1)
	  maxReportInterval := uint32(120)

	  request := &gateway.GwSetAttributeReportingReq{
	    DstAddress: &gateway.GwAddressStructT{
	      AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
	      IeeeAddr:    c.device.deviceInfo.IeeeAddress,
	    },
	    ClusterId: &clusterID,
	    AttributeReportList: []*gateway.GwAttributeReportT{{
	      AttributeId:       &attributeID,
	      AttributeType:     gateway.GwZclAttributeDataTypesT_ZCL_DATATYPE_BOOLEAN.Enum(),
	      MinReportInterval: &minReportInterval,
	      MaxReportInterval: &maxReportInterval,
	    }},
	  }

	  response := &gateway.GwSetAttributeReportingRspInd{}

	  err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 20*time.Second)
	  if err != nil {
	    log.Errorf("Error enabling on/off reporting: %s", err)
	  } else if response.Status.String() != "STATUS_SUCCESS" {
	    log.Errorf("Failed to enable on/off reporting. status: %s", response.Status.String())
	  }*/

	//mosquitto_pub -m '{"id":123, "params": [0.1],"jsonrpc": "2.0","method":"set","time":132123123}' -t '$device/26b4f71484/channel/11-8'

	c.channel = channels.NewColorChannel(c)
	err := c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
	if err != nil {
		log.Fatalf("Failed to announce color channel: %s", err)
	}

	go func() {
		for {
			log.Debugf("Polling for color")
			err := c.fetchState()
			if err != nil {
				log.Errorf("Failed to poll for color state %s", err)
			}
			time.Sleep(10 * time.Second)
		}
	}()

	return nil

}