diff options
Diffstat (limited to 'example.go')
-rw-r--r-- | example.go | 55 |
1 files changed, 0 insertions, 55 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 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "github.com/brutella/hc" | ||
5 | "github.com/brutella/hc/accessory" | ||
6 | |||
7 | "log" | ||
8 | "time" | ||
9 | ) | ||
10 | |||
11 | func 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 | } | ||