示例#1
0
func CreateSwitchDevice(driver ninja.Driver, info *model.Device, conn *ninja.Connection) (*SwitchDevice, error) {

	d := &SwitchDevice{
		baseDevice: baseDevice{
			conn:   conn,
			driver: driver,
			log:    logger.GetLogger("SwitchDevice - " + *info.Name),
			info:   info,
		},
	}

	err := conn.ExportDevice(d)
	if err != nil {
		d.log.Fatalf("Failed to export device %s: %s", *info.Name, err)
	}

	d.onOffChannel = channels.NewOnOffChannel(d)
	d.conn.ExportChannel(d, d.onOffChannel, "on-off")
	if err != nil {
		d.log.Fatalf("Failed to export on-off channel: %s", err)
	}

	d.log.Infof("Created")

	return d, nil
}
示例#2
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
}
示例#3
0
func (d *MediaPlayerDevice) EnableOnOffChannel(supportedEvents ...string) error {

	if d.ApplyOn == nil && d.ApplyOff == nil && d.ApplyToggleOnOff == nil {
		return fmt.Errorf("No on-off methods provided")
	}

	supportedMethods := []string{}
	if d.ApplyOn != nil || d.ApplyOff != nil {
		supportedMethods = append(supportedMethods, "set")

		if d.ApplyOn != nil {
			supportedMethods = append(supportedMethods, "turnOn")
		}

		if d.ApplyOff != nil {
			supportedMethods = append(supportedMethods, "turnOff")
		}
	}

	if d.ApplyToggleOnOff != nil {
		supportedMethods = append(supportedMethods, "toggle")
	}

	d.onOff = channels.NewOnOffChannel(d)
	return d.conn.ExportChannelWithSupported(d, d.onOff, "on-off", &supportedEvents, &supportedMethods)
}
示例#4
0
func (c *OnOffChannel) init() error {
	log.Debugf("Initialising on/off 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())
	}*/

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

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

	return nil

}
示例#5
0
func (d *LightDevice) EnableOnOffChannel() error {
	d.onOff = channels.NewOnOffChannel(d)
	return d.conn.ExportChannel(d, d.onOff, "on-off")
}