-
Notifications
You must be signed in to change notification settings - Fork 5
/
whoisrouter.go
57 lines (52 loc) · 1.32 KB
/
whoisrouter.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
package bacnet
import (
"fmt"
"github.com/NubeDev/bacnet/btypes"
"github.com/NubeDev/bacnet/btypes/ndpu"
"github.com/NubeDev/bacnet/encoding"
)
/*
Is in beta
*/
func (c *client) WhoIsRouterToNetwork() (resp *[]btypes.Address) {
var err error
dest := *c.dataLink.GetBroadcastAddress()
enc := encoding.NewEncoder()
npdu := &btypes.NPDU{
Version: btypes.ProtocolVersion,
Destination: &dest,
Source: c.dataLink.GetMyAddress(),
IsNetworkLayerMessage: true,
NetworkLayerMessageType: ndpu.WhoIsRouterToNetwork,
// We are not expecting a direct reply from a single destination
ExpectingReply: false,
Priority: btypes.Normal,
HopCount: btypes.DefaultHopCount,
}
enc.NPDU(npdu)
// Run in parallel
errChan := make(chan error)
broadcast := &SetBroadcastType{Set: true, BacFunc: btypes.BacFuncBroadcast}
go func() {
_, err = c.Send(dest, npdu, enc.Bytes(), broadcast)
errChan <- err
}()
values, err := c.utsm.Subscribe(1, 65534) //65534 is the max number a network can be
if err != nil {
fmt.Println(`err`, err)
}
err = <-errChan
if err != nil {
}
var list []btypes.Address
for _, addresses := range values {
r, ok := addresses.([]btypes.Address)
if !ok {
continue
}
for _, addr := range r {
list = append(list, addr)
}
}
return &list
}