18
18
package main
19
19
20
20
import (
21
+ "context"
21
22
"encoding/json"
22
- "fmt"
23
- "log"
24
23
25
- "github.com/oleksandr/bonjour "
24
+ "github.com/brutella/dnssd "
26
25
)
27
26
27
+ var service = "_arduino._tcp.local."
28
+
28
29
type syncOutputJSON struct {
29
30
EventType string `json:"eventType"`
30
31
Port * boardPortJSON `json:"port"`
@@ -43,32 +44,24 @@ func startSync() (chan<- bool, error) {
43
44
44
45
closeChan := make (chan bool )
45
46
46
- resolver , err := bonjour .NewResolver (nil )
47
- if err != nil {
48
- log .Println ("Failed to initialize resolver:" , err .Error ())
49
- return nil , err
47
+ addFn := func (srv dnssd.Service ) {
48
+ outputSyncMessage (& syncOutputJSON {
49
+ EventType : "add" ,
50
+ Port : newBoardPortJSON (& srv ),
51
+ })
50
52
}
51
53
52
- results := make (chan * bonjour.ServiceEntry )
53
-
54
- go func (results chan * bonjour.ServiceEntry , exitCh chan <- bool ) {
55
- for {
56
- for e := range results {
57
- log .Printf ("%+v" , e )
58
- if e .AddrIPv4 != nil {
59
- fmt .Println (e )
60
- }
61
- }
62
- }
63
- }(results , resolver .Exit )
54
+ remFn := func (srv dnssd.Service ) {
55
+ outputSyncMessage (& syncOutputJSON {
56
+ EventType : "remove" ,
57
+ Port : newBoardPortJSON (& srv ),
58
+ })
59
+ }
64
60
65
- // Sample output
66
- //2018/12/12 18:05:14 &{ServiceRecord:{Instance:Arduino Service:_arduino._tcp Domain:local serviceName: serviceInstanceName: serviceTypeName:} HostName:Arduino.local. Port:65280 Text:[ssh_upload=no tcp_check=no auth_upload=yes board=uno2018] TTL:120 AddrIPv4:10.130.22.247 AddrIPv6:<nil>}
67
- //&{{Arduino _arduino._tcp local } Arduino.local. 65280 [ssh_upload=no tcp_check=no auth_upload=yes board=uno2018] 120 10.130.22.247 <nil>}
61
+ ctx , cancel := context .WithCancel (context .Background ())
62
+ defer cancel ()
68
63
69
- err = resolver .Browse ("_arduino._tcp" , "" , results )
70
- if err != nil {
71
- log .Println ("Failed to browse:" , err .Error ())
64
+ if err := dnssd .LookupType (ctx , service , addFn , remFn ); err != nil {
72
65
return nil , err
73
66
}
74
67
0 commit comments