-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
192 lines (169 loc) Β· 4.49 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
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"github.com/haoguanguan/bluetooth_flow/device"
"github.com/o98k-ok/lazy/v2/alfred"
)
func connect(addr string) {
command := fmt.Sprintf("./blueutil --connect %s --info %s", addr, addr)
_, err := exec.Command("bash", "-c", command).CombinedOutput()
if err != nil {
fmt.Printf("connect bluetooth error %v\n", err)
}
}
func disconnect(addr string) {
command := fmt.Sprintf("./blueutil --disconnect %s --info %s", addr, addr)
_, err := exec.Command("bash", "-c", command).CombinedOutput()
if err != nil {
fmt.Printf("disconnect bluetooth error %v\n", err)
}
}
func bluetoothList() {
theme := os.Getenv("theme")
if theme == "" {
theme = "white"
}
groups := sync.WaitGroup{}
groups.Add(4)
var err error
var devices []device.Device1
var profileDevices device.Device2s
var ioregDevices device.Device3s
var plistDevices device.Device4s
go func() {
defer groups.Done()
devices, err = device.GetDeviceListByBlueutil()
if err != nil {
alfred.Log("get device list by blueutil error: " + err.Error())
return
}
}()
go func() {
defer groups.Done()
profileDevices, err = device.GetDeviceListBySystemProfiler()
if err != nil {
alfred.Log("get device list by system profiler error: " + err.Error())
return
}
}()
go func() {
defer groups.Done()
ioregDevices, err = device.GetDeviceListByIoreg()
if err != nil {
alfred.Log("get device list by ioreg error: " + err.Error())
return
}
}()
go func() {
defer groups.Done()
plistDevices, err = device.GetDeviceListByPlist()
if err != nil {
alfred.Log("get device list by plist error: " + err.Error())
return
}
}()
groups.Wait()
var blueDevices []device.DeviceInterface = []device.DeviceInterface{device.NewMac()}
for _, dd := range devices {
profileDevice := profileDevices.Get(dd.Address)
ioregDevice := ioregDevices.Get(dd.Address)
plistDevice := plistDevices.Get(dd.Address)
switch {
case profileDevice == nil:
v := device.NewNormal(&dd, profileDevice, ioregDevice, plistDevice)
blueDevices = append(blueDevices, v)
case profileDevice.MinorType == device.DeviceTypeAirpods:
v := device.NewAirPod(&dd, profileDevice, ioregDevice, plistDevice)
blueDevices = append(blueDevices, v)
case profileDevice.MinorType == device.DeviceTypeTrackpad:
v := device.NewTrackpad(&dd, profileDevice, ioregDevice, plistDevice)
blueDevices = append(blueDevices, v)
case profileDevice.MinorType == device.DeviceTypeKeyboard:
v := device.NewKeyboard(&dd, profileDevice, ioregDevice, plistDevice)
blueDevices = append(blueDevices, v)
default:
v := device.NewNormal(&dd, profileDevice, ioregDevice, plistDevice)
blueDevices = append(blueDevices, v)
}
}
items := alfred.NewItems()
for _, device := range blueDevices {
item := alfred.NewItem(device.GetName(), subTitle(device), device.GetAddress())
item.Icon = &alfred.Icon{
Path: getIcon(device, theme),
}
items.Append(item)
}
fmt.Println(items.Encode())
}
func getIcon(d device.DeviceInterface, theme string) string {
format := "./icons/%s/%s_1_%s_%s.png"
typ, _ := d.GetDeviceType()
if typ == device.DeviceTypeUnknown {
typ = "bluetooth"
}
ring := "0"
bat, _ := d.GetBatteryLevel()
if d.IsConnected() {
switch {
case float64(bat) > 95:
ring = "100"
case float64(bat) >= 87.5:
ring = "87.5"
case float64(bat) >= 75:
ring = "75"
case float64(bat) >= 62.5:
ring = "62.5"
case float64(bat) >= 50:
ring = "50"
case float64(bat) >= 37.5:
ring = "37.5"
case float64(bat) >= 25:
ring = "25"
case float64(bat) >= 12.5:
ring = "12.5"
default:
ring = "0"
}
}
return fmt.Sprintf(format,
typ, strings.ToLower(typ),
theme, ring)
}
func subTitle(device device.DeviceInterface) string {
connectIcon := "π΅"
if device.IsConnected() {
connectIcon = "π’"
}
return fmt.Sprintf("%s %s", connectIcon, device.GetBatteryTextView())
}
func main() {
app := alfred.NewApp("bluetooth")
app.Bind("list", func(s []string) { bluetoothList() })
app.Bind("connect", func(s []string) {
if len(s) != 1 {
alfred.Log("connect error: not enough arguments")
return
}
devices, err := device.GetDeviceListByBlueutil()
if err != nil {
alfred.Log("get device list by blueutil error: " + err.Error())
return
}
device := devices.Get(s[0])
if device == nil {
alfred.Log("device not found: " + s[0])
return
}
if device.Connected {
disconnect(device.Address)
} else {
connect(device.Address)
}
})
app.Run(os.Args)
}