Example #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
}
func (c *BrightnessChannel) init() error {
	log.Debugf("Initialising brightness 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.NewBrightnessChannel(c)
	err := c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
	if err != nil {
		log.Fatalf("Failed to announce brightness channel: %s", err)
	}

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

	return nil

}
Example #3
0
func (d *LightDevice) EnableBrightnessChannel() error {
	d.brightness = channels.NewBrightnessChannel(d)
	return d.conn.ExportChannel(d, d.brightness, "brightness")
}