-
Notifications
You must be signed in to change notification settings - Fork 20
/
objectlist.go
205 lines (180 loc) · 5.1 KB
/
objectlist.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gobacnet
import (
"fmt"
"github.com/alexbeltran/gobacnet/property"
bactype "github.com/alexbeltran/gobacnet/types"
)
func (c *Client) objectListLen(dev bactype.Device) (int, error) {
rp := bactype.ReadPropertyData{
Object: bactype.Object{
ID: dev.ID,
Properties: []bactype.Property{
bactype.Property{
Type: property.ObjectList,
ArrayIndex: 0,
},
},
},
}
resp, err := c.ReadProperty(dev, rp)
if err != nil {
return 0, fmt.Errorf("reading property failed: %v", err)
}
if len(resp.Object.Properties) == 0 {
return 0, fmt.Errorf("no data was returned")
}
data, ok := resp.Object.Properties[0].Data.(uint32)
if !ok {
return 0, fmt.Errorf("Unable to get object length")
}
return int(data), nil
}
func (c *Client) objectsRange(dev bactype.Device, start, end int) ([]bactype.Object, error) {
rpm := bactype.ReadMultipleProperty{
Objects: []bactype.Object{
bactype.Object{
ID: dev.ID,
},
},
}
for i := start; i <= end; i++ {
rpm.Objects[0].Properties = append(rpm.Objects[0].Properties, bactype.Property{
Type: property.ObjectList,
ArrayIndex: uint32(i),
})
}
resp, err := c.ReadMultiProperty(dev, rpm)
if err != nil {
return nil, fmt.Errorf("unable to read multiple properties: %v", err)
}
if len(resp.Objects) == 0 {
return nil, fmt.Errorf("no data was returned")
}
objs := make([]bactype.Object, len(resp.Objects[0].Properties))
for i, prop := range resp.Objects[0].Properties {
id, ok := prop.Data.(bactype.ObjectID)
if !ok {
return nil, fmt.Errorf("expected type Object ID, got %T", prop.Data)
}
objs[i].ID = id
}
return objs, nil
}
const readPropRequestSize = 20
func objectCopy(dest bactype.ObjectMap, src []bactype.Object) {
for _, o := range src {
if dest[o.ID.Type] == nil {
dest[o.ID.Type] = make(map[bactype.ObjectInstance]bactype.Object)
}
dest[o.ID.Type][o.ID.Instance] = o
}
}
func (c *Client) objectList(dev *bactype.Device) error {
dev.Objects = make(bactype.ObjectMap)
l, err := c.objectListLen(*dev)
if err != nil {
return fmt.Errorf("unable to get list length: %v", err)
}
// Scan size is broken
scanSize := int(dev.MaxApdu) / readPropRequestSize
i := 0
for i = 0; i < l/scanSize; i++ {
start := i*scanSize + 1
end := (i + 1) * scanSize
objs, err := c.objectsRange(*dev, start, end)
if err != nil {
return fmt.Errorf("unable to retrieve objects between %d and %d: %v", start, end, err)
}
objectCopy(dev.Objects, objs)
}
start := i*scanSize + 1
end := l
if start <= end {
objs, err := c.objectsRange(*dev, start, end)
if err != nil {
return fmt.Errorf("unable to retrieve objects between %d and %d: %v", start, end, err)
}
objectCopy(dev.Objects, objs)
}
return nil
}
func (c *Client) objectInformation(dev *bactype.Device, objs []bactype.Object) error {
// Often times the map will re arrange the order it spits out
// so we need to keep track since the response will be in the
// same order we issue the commands.
keys := make([]bactype.ObjectID, len(objs))
counter := 0
rpm := bactype.ReadMultipleProperty{
Objects: []bactype.Object{},
}
for _, o := range objs {
if o.ID.Type > maxStandardBacnetType {
continue
}
keys[counter] = o.ID
counter++
rpm.Objects = append(rpm.Objects, bactype.Object{
ID: o.ID,
Properties: []bactype.Property{
bactype.Property{
Type: property.ObjectName,
ArrayIndex: bactype.ArrayAll,
},
bactype.Property{
Type: property.Description,
ArrayIndex: bactype.ArrayAll,
},
},
})
}
resp, err := c.ReadMultiProperty(*dev, rpm)
if err != nil {
return fmt.Errorf("unable to read multiple property :%v", err)
}
var name, description string
var ok bool
for i, r := range resp.Objects {
name, ok = r.Properties[0].Data.(string)
if !ok {
return fmt.Errorf("expecting string got %T", r.Properties[0].Data)
}
description, ok = r.Properties[1].Data.(string)
if !ok {
return fmt.Errorf("expecting string got %T", r.Properties[1].Data)
}
obj := dev.Objects[keys[i].Type][keys[i].Instance]
obj.Name = name
obj.Description = description
dev.Objects[keys[i].Type][keys[i].Instance] = obj
}
return nil
}
func (c *Client) allObjectInformation(dev *bactype.Device) error {
objs := dev.ObjectSlice()
incrSize := 5
var err error
for i := 0; i < len(objs); i += incrSize {
subset := objs[i:min(i+incrSize, len(objs))]
err = c.objectInformation(dev, subset)
if err != nil {
return err
}
}
return nil
}
// Objects retrieves all of the objects within the given device and returns a
// device with these objects. Along with the list of objects, it will also
// gather additional information from the object such as the name and
// description of the objects. The device returned contains all of the name and
// description fields for all objects
func (c *Client) Objects(dev bactype.Device) (bactype.Device, error) {
err := c.objectList(&dev)
if err != nil {
return dev, fmt.Errorf("unable to get object list: %v", err)
}
err = c.allObjectInformation(&dev)
if err != nil {
return dev, fmt.Errorf("unable to get object's information: %v", err)
}
return dev, nil
}