From f0e6c2e2e39572cd9044cabba2547ec7bd688216 Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Sat, 24 Oct 2020 20:31:19 +0200 Subject: [PATCH] add an example with an integrated web server a HomeKit device with an integrated web server which enables toggling via http --- _example/webexample/main.go | 84 +++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 _example/webexample/main.go diff --git a/_example/webexample/main.go b/_example/webexample/main.go new file mode 100644 index 00000000..424794b9 --- /dev/null +++ b/_example/webexample/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "github.com/brutella/hc" + "github.com/brutella/hc/accessory" + "github.com/brutella/hc/log" + + "fmt" + "net/http" +) + +var WebSwitchState bool +var accessoryDevice *accessory.Switch + +func toggleHandler(w http.ResponseWriter, r *http.Request) { + WebSwitchState = accessoryDevice.Switch.On.GetValue() + if WebSwitchState == true { + log.Debug.Println("Switch is on") + } else { + log.Debug.Println("Switch is off") + } + accessoryDevice.Switch.On.SetValue(!WebSwitchState) + fmt.Fprintf(w, "Hello World\nSwitch state is now: %v\n", !WebSwitchState) +} + +func turnOnHandler(w http.ResponseWriter, r *http.Request) { + accessoryDevice.Switch.On.SetValue(true) + fmt.Fprintf(w, "Hello World\nSwitch state is now: true\n") +} + +func turnOffHandler(w http.ResponseWriter, r *http.Request) { + accessoryDevice.Switch.On.SetValue(false) + fmt.Fprintf(w, "Hello World\nSwitch state is now: false\n") +} + +func main() { + log.Debug.Enable() + switchInfo := accessory.Info{ + Name: "WebSwitch", + //SerialNumber: "00001", + //Manufacturer: "", + //Model: "", + //FirmwareRevision: "0.0.1", + //ID: 123456, + } + accessoryDevice = accessory.NewSwitch(switchInfo) + + config := hc.Config{Pin: "12344321", Port: "12345", StoragePath: "./db"} + t, err := hc.NewIPTransport(config, accessoryDevice.Accessory) + + if err != nil { + log.Info.Panic(err) + } + + // Log to console when client (e.g. iOS app) changes the value of the on characteristic + accessoryDevice.Switch.On.OnValueRemoteUpdate(func(on bool) { + if on == true { + log.Debug.Println("Client changed switch to on") + } else { + log.Debug.Println("Client changed switch to off") + } + }) + + hc.OnTermination(func() { + <-t.Stop() + }) + + //http handler to toggle the switch + //send web request to http://localhost:8080/toggle to toggle the switch + http.HandleFunc("/toggle", toggleHandler) + + //http handler to turn on the switch + //send web request to http://localhost:8080/turnon to turn the switch on + http.HandleFunc("/turnon", turnOnHandler) + + //http handler to turn off the switch + //send web request to http://localhost:8080/turnoff to turn the switch off + http.HandleFunc("/turnoff", turnOffHandler) + + //start the webserver and listen on port 8080 + go http.ListenAndServe(":8080", nil) + + t.Start() +}