-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (158 loc) · 3.1 KB
/
main.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
package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"time"
"io/fs"
"path/filepath"
"github.com/fatih/color"
"tinygo.org/x/bluetooth"
)
type StoredDevice struct {
UID string
LastSeen time.Time
LocalName string
CustomName string
Seen int
}
type DeviceTuple struct {
K string
V *StoredDevice
}
func (app *App) checkSeen() {
for {
time.Sleep(5 * time.Second)
app.RLock()
list := []*DeviceTuple{}
for k, sd := range app.seen {
if sd.Seen != 0 {
color.Green("SEEN %s %v", k, sd)
} else {
color.Yellow("SEEN %s %v", k, sd)
}
list = append(
list,
&DeviceTuple{
K: k,
V: sd,
},
)
}
sort.Slice(
list,
func(j, k int) bool {
return list[k].V.LastSeen.After(list[j].V.LastSeen)
},
)
color.Red("%d Total AWOL", len(app.notSeen))
for k, sd := range app.notSeen {
if sd.Seen > 1 && (time.Since(sd.LastSeen) > (3 * time.Minute)) {
color.Red("NOT SEEN %s %v", k, sd)
}
}
app.RUnlock()
}
}
func (app *App) loadManufacturers() {
b, err := os.ReadFile("./manufacturers.csv")
if err != nil {
panic(err)
}
var p int
for _, bb := range bytes.Split(b, []byte("\n")) {
x := bytes.Split(bb, []byte("\t"))
if len(x) < 2 {
continue
}
addr := string(x[0])
name := string(x[1])
var descr string
if len(x) > 2 {
descr = string(x[2])
}
addrParts := strings.Split(addr, ":")
if len(addrParts) > 3 {
addr = strings.Join(addrParts[:3], ":")
}
entry := fmt.Sprintf("%s: %s", name, descr)
println(p, addr, entry)
app.db[addr] = append(
app.db[addr],
entry,
)
p++
}
}
func (app *App) loadSeen(m map[string]struct{}) {
if err := filepath.WalkDir(
"./seen",
func(path string, info fs.DirEntry, err error) error {
println(path)
if path == "./seen" {
return nil
}
b, err := os.ReadFile(path)
if err != nil {
return err
}
sd := &StoredDevice{}
if err := app.Unmarshal(b, sd); err != nil {
return err
}
uid := strings.Split(path, "/")[1]
if _, ok := m[uid]; ok {
return nil
}
if time.Since(sd.LastSeen) > (2 * time.Minute) {
app.unsee(uid, sd)
} else {
app.see(uid, sd)
}
return nil
},
); err != nil {
panic(err)
}
}
func main() {
os.Mkdir("seen", 0777)
os.Mkdir("skipped", 0777)
app := &App{
adapter: bluetooth.DefaultAdapter,
discovered: make(chan bluetooth.ScanResult, 10),
db: map[string][]string{},
seen: map[string]*StoredDevice{},
notSeen: map[string]*StoredDevice{},
}
app.loadManufacturers()
// Enable BLE interface.
if err := app.adapter.Enable(); err != nil {
panic(err)
}
go app.checkSeen()
go app.logic()
for {
m := map[string]struct{}{}
// Start scanning.
app.loadSeen(m)
go func() {
time.Sleep(time.Minute)
app.adapter.StopScan()
}()
println("scanning...")
if err := app.adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
uid := device.Address.String()
if _, ok := m[uid]; ok {
return
}
m[uid] = struct{}{}
app.discovered <- device
}); err != nil {
panic(err)
}
println("clean scan exit")
}
}