-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (65 loc) · 1.56 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
package main
import (
"fmt"
"log"
"math"
"math/rand"
"os/exec"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
)
func main() {
pin, formattedPin := generatePin()
info := accessory.Info{
Name: "USB Smart Switch",
Manufacturer: "Dustin Mowcomber",
}
log.Printf("starting HomeKit device %q", info.Name)
log.Printf("pin: %s", formattedPin)
// add hc lightbulb
acc := accessory.NewLightbulb(info)
acc.Lightbulb.On.OnValueRemoteUpdate(func(isOn bool) {
if isOn {
on()
} else {
off()
}
})
// default to on state
acc.Lightbulb.On.SetValue(true)
// run hc server
t, err := hc.NewIPTransport(hc.Config{Pin: pin}, acc.Accessory)
if err != nil {
log.Fatal(err)
}
hc.OnTermination(func() {
<-t.Stop()
})
t.Start()
}
func off() {
if err := exec.Command("uhubctl", "-a", "off", "-l", "1-1").Run(); err != nil {
log.Printf("failed to turn off usb: %s", err.Error())
}
}
func on() {
if err := exec.Command("uhubctl", "-a", "on", "-l", "1-1").Run(); err != nil {
log.Printf("failed to turn on usb: %s", err.Error())
}
}
func generatePin() (string, string) {
rand.Seed(time.Now().UnixNano())
// generate an 8 digit number
num := randNDigits(8)
pin := fmt.Sprintf("%d", num)
// ignoring the error as I've tested the above pin generation to always be 8 digits
formattedPin, _ := hc.ValidatePin(pin)
return pin, formattedPin
}
func randNDigits(digits int) int {
rand.Seed(time.Now().UnixNano())
low := int(math.Pow(10, float64(digits)-1))
high := int(math.Pow(10, float64(digits)))
return low + rand.Intn(high-low)
}