aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2022-08-04 17:42:14 -0700
committerMike Crute <mike@crute.us>2022-08-04 17:42:14 -0700
commit11b6cf3ce57b4e1706c8410ad7ccd499902fc4de (patch)
treecf6b37cc59f1e7b17e72cfb7917282f15dabbe4b
parent8fcbc57005d7a7195ee71d2ddc0b1ab70427ec3d (diff)
downloadgo-inform-11b6cf3ce57b4e1706c8410ad7ccd499902fc4de.tar.bz2
go-inform-11b6cf3ce57b4e1706c8410ad7ccd499902fc4de.tar.xz
go-inform-11b6cf3ce57b4e1706c8410ad7ccd499902fc4de.zip
Remove homekit demo junk
-rw-r--r--example.go55
-rw-r--r--example_hk.go125
2 files changed, 0 insertions, 180 deletions
diff --git a/example.go b/example.go
deleted file mode 100644
index 440b4ee..0000000
--- a/example.go
+++ /dev/null
@@ -1,55 +0,0 @@
1package main
2
3import (
4 "github.com/brutella/hc"
5 "github.com/brutella/hc/accessory"
6
7 "log"
8 "time"
9)
10
11func main() {
12 switchInfo := accessory.Info{
13 Name: "Lamp",
14 SerialNumber: "051AC-23AAM1",
15 Manufacturer: "Foobar",
16 Model: "AB",
17 }
18 acc := accessory.NewSwitch(switchInfo)
19
20 config := hc.Config{Pin: "12344321", Port: "12345", StoragePath: "./db"}
21 t, err := hc.NewIPTransport(config, acc.Accessory)
22
23 if err != nil {
24 log.Fatal(err)
25 }
26
27 // Log to console when client (e.g. iOS app) changes the value of the on characteristic
28 acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
29 if on == true {
30 log.Println("[INFO] Client changed switch to on")
31 } else {
32 log.Println("[INFO] Client changed switch to off")
33 }
34 })
35
36 // Periodically toggle the switch's on characteristic
37 go func() {
38 for {
39 on := !acc.Switch.On.GetValue()
40 if on == true {
41 log.Println("[INFO] Switch is on")
42 } else {
43 log.Println("[INFO] Switch is off")
44 }
45 acc.Switch.On.SetValue(on)
46 time.Sleep(5 * time.Second)
47 }
48 }()
49
50 hc.OnTermination(func() {
51 t.Stop()
52 })
53
54 t.Start()
55}
diff --git a/example_hk.go b/example_hk.go
deleted file mode 100644
index ee89281..0000000
--- a/example_hk.go
+++ /dev/null
@@ -1,125 +0,0 @@
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "github.com/brutella/hc"
7 "github.com/brutella/hc/accessory"
8 "github.com/mcrute/go-inform/inform"
9 "io/ioutil"
10 "log"
11 "os"
12)
13
14// Load devices into state
15// Gather current initial state from devices
16// Track state transitions
17// Inputs:
18// - Homekit
19// - Devices
20
21type Port struct {
22 Label string `json:"label"`
23 Port int `json:"port"`
24}
25
26type Device struct {
27 Key string `json:"key"`
28 Name string `json:"name"`
29 Model string `json:"model"`
30 Serial string `json:"serial"`
31 Ports []*Port `json:"ports"`
32}
33
34type DeviceMap map[string]*Device
35
36func LoadKeys(file string) (DeviceMap, error) {
37 var keys DeviceMap
38
39 kp, err := os.Open(file)
40 if err != nil {
41 return nil, err
42 }
43 defer kp.Close()
44
45 kd, err := ioutil.ReadAll(kp)
46 if err != nil {
47 return nil, err
48 }
49
50 err = json.Unmarshal(kd, &keys)
51 if err != nil {
52 return nil, err
53 }
54
55 return keys, nil
56}
57
58func main() {
59 devs, err := LoadKeys("data/device_keys.json")
60 if err != nil {
61 log.Println("Error loading key file")
62 log.Println(err.Error())
63 return
64 }
65
66 keys := make(map[string]string, len(devs))
67 for i, d := range devs {
68 keys[i] = d.Key
69 }
70
71 h := inform.NewInformHandler(&inform.Codec{keys})
72 s, _ := inform.NewServer(h)
73 as := make([]*accessory.Accessory, 0, len(devs)*3)
74
75 for i, d := range devs {
76 for _, p := range d.Ports {
77 a := accessory.NewSwitch(accessory.Info{
78 Name: p.Label,
79 SerialNumber: fmt.Sprintf("%s-%d", d.Serial, p.Port),
80 Manufacturer: "Ubiquiti",
81 Model: d.Model,
82 })
83
84 // Capture these for the closure, otherwise they're bound to the
85 // single loop variable and will only see the final value of that
86 // variable
87 dev, port := i, p.Port
88
89 a.Switch.On.OnValueRemoteUpdate(func(on bool) {
90 h.SetState(dev, port, on)
91 })
92
93 h.AddPort(dev, port)
94 as = append(as, a.Accessory)
95 }
96 }
97
98 // The root accessory is what gets used to name the bridge so let's make it
99 // an actual bridge
100 br := accessory.New(accessory.Info{
101 Name: "UnifiBridge",
102 Manufacturer: "Mike Crute",
103 Model: "0.1",
104 }, accessory.TypeBridge)
105
106 config := hc.Config{
107 Pin: "12344321",
108 Port: "12345",
109 StoragePath: "./db",
110 }
111
112 t, err := hc.NewIPTransport(config, br, as...)
113 if err != nil {
114 log.Fatal(err)
115 return
116 }
117
118 hc.OnTermination(func() {
119 t.Stop()
120 os.Exit(0) // Otherwise homekit doesn't actually stop
121 })
122
123 go t.Start()
124 s.ListenAndServe()
125}