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 *TempChannel) init() error { log.Debugf("Initialising Temp channel of device %d", *c.device.deviceInfo.IeeeAddress) clusterID := ClusterIDTemp instantaneousDemandAttributeID := uint32(0x0400) minReportInterval := uint32(10) maxReportInterval := uint32(120) reportableChange := uint32(1) request := &gateway.GwSetAttributeReportingReq{ DstAddress: &gateway.GwAddressStructT{ AddressType: gateway.GwAddressTypeT_UNICAST.Enum(), IeeeAddr: c.device.deviceInfo.IeeeAddress, EndpointId: c.endpoint.EndpointId, }, ClusterId: &clusterID, AttributeReportList: []*gateway.GwAttributeReportT{{ AttributeId: &instantaneousDemandAttributeID, AttributeType: gateway.GwZclAttributeDataTypesT_ZCL_DATATYPE_INT24.Enum(), MinReportInterval: &minReportInterval, MaxReportInterval: &maxReportInterval, ReportableChange: &reportableChange, }}, } response := &gateway.GwSetAttributeReportingRspInd{} err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 20*time.Second) if err != nil { log.Errorf("Error enabling Temp reporting: %s", err) } else if response.Status.String() != "STATUS_SUCCESS" { log.Errorf("Failed to enable Temp reporting. status: %s", response.Status.String()) } c.channel = channels.NewTemperatureChannel(c) err = c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID) if err != nil { log.Fatalf("Failed to announce temperature channel: %s", err) } go func() { for { err := c.fetchState() if err != nil { log.Errorf("Failed to poll for Temperature %s", err) } time.Sleep(1 * time.Minute) } }() return nil }