This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
scanner.go
202 lines (186 loc) · 4.99 KB
/
scanner.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
// slow zeroconf on windows
// all XXX seconds, announcements are done automatically
// to forceTrigger the announcement, restart WLED
// as a timing reference for fast zeroconf on windows run:
// dns-sd -B _wled
// TODO serial device discovery, Art-Poll page 24 https://www.artisticlicence.com/WebSiteMaster/User%20Guides/art-net.pdf
package device
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/LedFx/ledfx/pkg/config"
"github.com/LedFx/ledfx/pkg/event"
"github.com/LedFx/ledfx/pkg/logger"
"github.com/grandcat/zeroconf"
)
var resolver *zeroconf.Resolver
var entries chan *zeroconf.ServiceEntry = make(chan *zeroconf.ServiceEntry)
var ctx context.Context
var cancel context.CancelFunc
var running bool
type wledInfo struct {
Ver string `json:"ver"`
Vid int `json:"vid"`
Leds struct {
Count int `json:"count"`
Lc byte `json:"lc"`
Pwr int `json:"pwr"`
Fps int `json:"fps"`
Maxpwr int `json:"maxpwr"`
Maxseg int `json:"maxseg"`
} `json:"leds"`
Str bool `json:"str"`
Name string `json:"name"`
Udpport int `json:"udpport"`
Live bool `json:"live"`
Lm string `json:"lm"`
Lip string `json:"lip"`
Ws int `json:"ws"`
Fxcount int `json:"fxcount"`
Palcount int `json:"palcount"`
Wifi struct {
Bssid string `json:"bssid"`
Rssi int `json:"rssi"`
Signal int `json:"signal"`
Channel int `json:"channel"`
} `json:"wifi"`
Fs struct {
U int `json:"u"`
T int `json:"t"`
Pmt int `json:"pmt"`
} `json:"fs"`
Ndc int `json:"ndc"`
Arch string `json:"arch"`
Core string `json:"core"`
Lwip int `json:"lwip"`
Freeheap int `json:"freeheap"`
Uptime int `json:"uptime"`
Opt int `json:"opt"`
Brand string `json:"brand"`
Product string `json:"product"`
Mac string `json:"mac"`
IP string `json:"ip"`
}
func init() {
var err error
resolver, err = zeroconf.NewResolver(nil)
ctx, cancel = context.WithCancel(context.Background())
if err != nil {
logger.Logger.WithField("context", "WLED Scanner").Fatal(err)
}
event.Subscribe(event.SettingsUpdate, func(e *event.Event) {
switch config.GetSettings().NoScan {
case false:
EnableScan()
case true:
DisableScan()
}
})
}
func EnableScan() error {
if running {
return nil
}
ctx, cancel = context.WithCancel(context.Background())
// handler for the scan results
go func(results <-chan *zeroconf.ServiceEntry) {
for entry := range results {
if err := handleEntry(entry); err != nil {
logger.Logger.WithField("context", "WLED Scanner").Error(err)
}
}
}(entries)
err := resolver.Browse(ctx, "_wled._tcp", "local", entries)
if err == nil {
logger.Logger.WithField("context", "WLED Scanner").Info("Enabled WLED Scanner")
running = true
} else {
logger.Logger.WithField("context", "WLED Scanner").Error("Failed to enable WLED scanner;", err)
cancel()
}
return err
}
func DisableScan() {
if !running {
return
}
logger.Logger.WithField("context", "WLED Scanner").Info("Disabled WLED Scanner")
cancel()
running = false
}
func handleEntry(entry *zeroconf.ServiceEntry) error {
// Discovered WLED service, now need to get additional info
logger.Logger.WithField("context", "WLED Scanner").Debugf("Found %s at %s", entry.ServiceRecord.Instance, entry.AddrIPv4[0])
// make request
url := fmt.Sprintf("http://%s/json/info", entry.AddrIPv4[0].String())
client := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
// read result
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
info := wledInfo{}
if err := json.Unmarshal(body, &info); err != nil {
return err
}
// Try to avoid duplication matching IP to other devices
for id, d := range deviceInstances {
switch pusher := d.pixelPusher.(type) {
case *UDP:
if pusher.config.IP == info.IP {
logger.Logger.WithField("context", "WLED Scanner").Debugf("Matches IP of %s - Ignoring.", id)
return nil
}
case *ArtNet:
if pusher.config.IP == info.IP {
logger.Logger.WithField("context", "WLED Scanner").Debugf("Matches IP of %s - Ignoring.", id)
return nil
}
case *E131:
for _, ip := range pusher.config.IPs {
if ip == info.IP {
logger.Logger.WithField("context", "WLED Scanner").Debugf("Matches IP of %s - Ignoring.", id)
return nil
}
}
}
}
// choose a suitable UDP protocol
var protocol Protocol
if info.Leds.Count <= 490 {
protocol = DRGB
} else {
protocol = DNRGB
}
// Create the device
logger.Logger.WithField("context", "WLED Scanner").Infof("Detected WLED %s", info.Name)
New("",
"udp_stream",
map[string]interface{}{
"name": info.Name,
"pixel_count": info.Leds.Count,
},
map[string]interface{}{
"ip": info.IP,
"port": info.Udpport,
"protocol": protocol,
"timeout": 3,
},
)
return nil
}