-
Notifications
You must be signed in to change notification settings - Fork 7
/
Device.go
83 lines (65 loc) · 2.13 KB
/
Device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"regexp"
"time"
"github.com/ninjasphere/go-ninja/api"
"github.com/ninjasphere/go-ninja/model"
"github.com/ninjasphere/go-zigbee/gateway"
"github.com/ninjasphere/go-zigbee/nwkmgr"
)
type Device struct {
info *model.Device
sendEvent func(event string, payload interface{}) error
ManufacturerName string
ModelIdentifier string
driver *Driver
deviceInfo *nwkmgr.NwkDeviceInfoT
channels []Channel
}
var cleanStart, err = regexp.Compile(`(^[^\w -]+)`)
func cleanString(str []byte) string {
return string(cleanStart.ReplaceAll(str, []byte("")))
}
func (d *Device) getBasicInfo() error {
log.Debugf("Getting basic information from %X", *d.deviceInfo.IeeeAddress)
cluster := ClusterIDBasic
ManufacturerNameAttribute := uint32(0x004)
ModelIdentifierAttribute := uint32(0x005)
request := &gateway.GwReadDeviceAttributeReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: d.deviceInfo.IeeeAddress,
},
ClusterId: &cluster,
AttributeList: []uint32{ManufacturerNameAttribute, ModelIdentifierAttribute},
}
response := &gateway.GwReadDeviceAttributeRspInd{}
err := d.driver.gatewayConn.SendAsyncCommand(request, response, 10*time.Second)
if err != nil {
return fmt.Errorf("Error getting basic device information state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to get basic device information. status: %s", response.Status.String())
}
for _, attribute := range response.AttributeRecordList {
switch *attribute.AttributeId {
case ManufacturerNameAttribute:
d.ManufacturerName = cleanString(attribute.AttributeValue)
case ModelIdentifierAttribute:
d.ModelIdentifier = cleanString(attribute.AttributeValue)
default:
log.Debugf("Unknown attribute returned when finding basic info %s", *attribute.AttributeId)
}
}
return nil
}
func (d *Device) GetDeviceInfo() *model.Device {
return d.info
}
func (d *Device) GetDriver() ninja.Driver {
return d.driver
}
func (d *Device) SetEventHandler(sendEvent func(event string, payload interface{}) error) {
d.sendEvent = sendEvent
}