Skip to content

Commit

Permalink
Add wemo emulation via github.com/forfuncsake/smartswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
forfuncsake committed Aug 27, 2018
1 parent 969d9c8 commit 87b7713
Show file tree
Hide file tree
Showing 35 changed files with 3,775 additions and 27 deletions.
41 changes: 37 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions cmd/gdhk/garagedoor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type apiResponse struct {
// for the door (opened/closed), where the switch will always
// trigger the door button.
type GarageDoor struct {
Name string
URL string
User string
Password string
Expand All @@ -47,9 +48,17 @@ type GarageDoor struct {
guardDelay time.Duration
}

// NewGarageDoor returns a GarageDoor with the provided accessory info.
func NewGarageDoor(conf Config, info accessory.Info) *GarageDoor {
// NewGarageDoor returns a GarageDoor with the provided config.
func NewGarageDoor(conf Config) *GarageDoor {
info := accessory.Info{
Name: conf.Name,
SerialNumber: conf.Serial,
Manufacturer: "forfuncsake",
Model: "GDHK",
}

acc := GarageDoor{
Name: conf.Name,
URL: conf.URL,
User: conf.Username,
Password: conf.Password,
Expand Down
22 changes: 13 additions & 9 deletions cmd/gdhk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/kelseyhightower/envconfig"
)

Expand All @@ -30,6 +30,8 @@ type Config struct {
Username string `default:"admin"`
Password string `default:"password"`
Limit uint

Wemo bool
}

func main() {
Expand All @@ -50,12 +52,13 @@ func main() {
flag.StringVar(&conf.Username, "u", conf.Username, "`username` for requests to garage door API")
flag.StringVar(&conf.Password, "p", conf.Password, "`password` for requests to garage door API")
flag.UintVar(&conf.Limit, "limit", conf.Limit, "Limit probing the API to once every `n` seconds")
flag.BoolVar(&conf.Wemo, "wemo", conf.Wemo, "Also enable control as a simulated wemo plug")
e := flag.Bool("e", false, "show envconfig help and exit")
v := flag.Bool("version", false, "show version and exit")
flag.Parse()

if *v {
fmt.Printf("%s: %s", os.Args[0], version)
fmt.Printf("%s: %s\n", os.Args[0], version)
os.Exit(0)
}

Expand Down Expand Up @@ -86,13 +89,7 @@ func main() {
os.Exit(1)
}

info := accessory.Info{
Name: conf.Name,
SerialNumber: conf.Serial,
Manufacturer: "forfuncsake",
Model: "GDHK",
}
door := NewGarageDoor(conf, info)
door := NewGarageDoor(conf)

config := hc.Config{
Pin: conf.PIN,
Expand All @@ -116,6 +113,13 @@ func main() {
door.Opener.TargetDoorState.SetValue(door.getTargetState())
})

if conf.Wemo {
err = door.enableWemo()
if err != nil {
log.Printf("warning: could not start wemo emulation: %v", err)
}
}

timeout := 5 * time.Second
srv := http.Server{
Addr: fmt.Sprintf(":%d", conf.ProxyPort),
Expand Down
15 changes: 6 additions & 9 deletions cmd/gdhk/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import (
"fmt"
"testing"

"github.com/kelseyhightower/envconfig"

"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/kelseyhightower/envconfig"
)

func newDoor(port int) *GarageDoor {
conf := Config{}
envconfig.Process("gd_test", &conf)

conf.Name = "GarageDoorTest"
conf.Serial = "1234567890"
conf.URL = fmt.Sprintf("http://127.0.0.1:%d", port)
door := NewGarageDoor(conf, accessory.Info{
Name: "GarageDoorTest",
SerialNumber: "1234567890",
Manufacturer: "forfuncsake",
Model: "GDHK",
})

door := NewGarageDoor(conf)

return door
}
Expand Down
46 changes: 46 additions & 0 deletions cmd/gdhk/wemo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"log"

"github.com/brutella/hc/characteristic"
"github.com/forfuncsake/smartswitch"
)

// true == Switch On == Open Door
// false == Switch off == Close Door
func boolToTargetState(v bool) int {
if v {
return characteristic.TargetDoorStateOpen
}
return characteristic.TargetDoorStateClosed
}

// 1 == Door Closed == Switch Off == false
// Otherwise the door is fully or partially open, so the switch shows "on"
func stateToBool(i int) bool {
return i != characteristic.TargetDoorStateClosed
}

// Set changes the target state of the door
// satisfying the smartswitch.Switch interface
func (d *GarageDoor) Set(on bool) error {
d.setState(boolToTargetState(on))
return nil
}

// Status returns the status of the door (true=on=open, false=off=closed)
// satisfying the smartswitch.Switch interface
func (d *GarageDoor) Status() (bool, error) {
return stateToBool(d.getState()), nil
}

func (d *GarageDoor) enableWemo() error {
wemo := smartswitch.NewController(d.Name, d, smartswitch.WithMinissdpSocket("/var/run/minissdpd.sock"))
loc, err := wemo.Start()
if err != nil {
return err
}
log.Printf("Wemo handler listening on %s\n", loc)
return nil
}
18 changes: 18 additions & 0 deletions vendor/github.com/forfuncsake/minissdpc/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/forfuncsake/minissdpc/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions vendor/github.com/forfuncsake/minissdpc/Gopkg.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 87b7713

Please sign in to comment.