Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an example with an integrated web server #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions _example/webexample/main.go
Original file line number Diff line number Diff line change
@@ -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()
}