/
options.go
139 lines (114 loc) · 3.62 KB
/
options.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
package discovery
import (
"context"
"net"
"sync"
"time"
"github.com/hashicorp/mdns"
"tinygo.org/x/bluetooth"
)
type options struct {
bleAdapter *bluetooth.Adapter
bleLock sync.Mutex
enableBLEAdapter func() error
bleSearchEnabled bool
authCallback AuthCallback
now func() time.Time
mdnsInterface *net.Interface
mdnsZone string
mdnsService string
mdnsSearchEnabled bool
searchStrictTimeout bool
searchTimeout time.Duration
searchConfirm SearchConfirm
concurrency int
// deviceTTL is relevant for long-lived commands (like prometheus metrics server) when
// mixed with mDNS or other ephemeral discovery.
deviceTTL time.Duration
preferIPVersion string
mdnsQueryFunc func(context.Context, *mdns.QueryParam) error
}
// DiscovererOption provides optional parameters for the Discoverer.
type DiscovererOption func(*Discoverer)
// WithMDNSInterface configures a specific network interface to search.
func WithMDNSInterface(iface *net.Interface) DiscovererOption {
return func(d *Discoverer) {
d.mdnsInterface = iface
}
}
// WithMDNSZone configures the mDNS zone to search. This is commonly `.local`.
func WithMDNSZone(zone string) DiscovererOption {
return func(d *Discoverer) {
d.mdnsZone = zone
}
}
// WithMDNSService configures the mDNS service name to search. Shelly devices are commonly
// at `_shelly._tcp`.
func WithMDNSService(service string) DiscovererOption {
return func(d *Discoverer) {
d.mdnsService = service
}
}
// WithSearchTimeout specifies the duration to wait for mDNS responses and the initial probe.
func WithSearchTimeout(timeout time.Duration) DiscovererOption {
return func(d *Discoverer) {
d.searchTimeout = timeout
}
}
// WithConcurrency configures the number of concurrent device probes to evaluate.
func WithConcurrency(concurrency int) DiscovererOption {
return func(d *Discoverer) {
d.concurrency = concurrency
}
}
// WithIPVersion sets a required IP version for discovery. Values "4" or "6" are accepted.
func WithIPVersion(ipVersion string) DiscovererOption {
return func(d *Discoverer) {
d.preferIPVersion = ipVersion
}
}
// WithDeviceTTL configures a time-to-live for devices in long-lived commands like the
// prometheus server.
func WithDeviceTTL(ttl time.Duration) DiscovererOption {
return func(d *Discoverer) {
d.deviceTTL = ttl
}
}
// WithMDNSSearchEnabled allows enabling or disabling mDNS discovery.
func WithMDNSSearchEnabled(enabled bool) DiscovererOption {
return func(d *Discoverer) {
d.mdnsSearchEnabled = enabled
}
}
// WithBLEAdapter configures a BLE adapter for use in discovery.
func WithBLEAdapter(ble *bluetooth.Adapter) DiscovererOption {
return func(d *Discoverer) {
d.bleAdapter = ble
}
}
// WithMDNSSearchEnabled allows enabling or disabling BLE discovery.
func WithBLESearchEnabled(enabled bool) DiscovererOption {
return func(d *Discoverer) {
d.bleSearchEnabled = enabled
}
}
// WithSearchConfirm sets a callback to confirm search results.
func WithSearchConfirm(confirm SearchConfirm) DiscovererOption {
return func(d *Discoverer) {
d.searchConfirm = confirm
}
}
// WithAuthCallback sets a default callback for authenticating .
func WithAuthCallback(authCallback AuthCallback) DiscovererOption {
return func(d *Discoverer) {
d.authCallback = authCallback
}
}
// WithSearchStrictTimeout will force devices which have been discovered, but not resolved and added
// to finish within the search timeout or be cancelled.
func WithSearchStrictTimeout(strictTimeoutMode bool) DiscovererOption {
return func(d *Discoverer) {
d.searchStrictTimeout = strictTimeoutMode
}
}
type DeviceOption func(*Device)