diff --git a/README.md b/README.md index 2cc582b..9c515e4 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,50 @@ go.wemo ======= -[![GoDoc](http://godoc.org/github.com/savaki/go.wemo?status.png)](http://godoc.org/github.com/savaki/go.wemo) -[![Build Status](https://snap-ci.com/savaki/go.wemo/branch/master/build_image)](https://snap-ci.com/savaki/go.wemo/branch/master) +[![GoDoc](http://godoc.org/github.com/andrewpurkett/go.wemo?status.png)](http://godoc.org/github.com/andrewpurkett/go.wemo) Simple package to interface with Belkin wemo devices. +## Standalone Usage Guide + +Install the go library and configure it as needed, ensuring `~/bin/go/src/` is in your `GOPATH` environment variable. + +Run the following command from `~/bin/go/src/`: + +`go get https://github.com/andrewpurkett/go.wemo` + +navigate into the new directory: + +`cd ~/bin/go/src/github.com/andrewpurkett/go.wemo` + +To use this fork, run `git checkout forktest`, now, to avoid any conflicts. + +Run `go get` to retrieve dependencies + +navigate into the example usage directory: + +`cd ~/bin/go/src/github.com/andrewpurkett/go.wemo/wemo` + +Run `go get` again to retrieve dependencies for the example usage directory + +Build the example usage tool: + +`go build` + +Then refer to the command line tool to see sample usage: + +`~/bin/go/src/github.com/andrewpurkett/go.wemo/wemo/wemo` + +`~/bin/go/src/github.com/andrewpurkett/go.wemo/wemo/wemo discover -h` + +![Example usage](https://i.imgur.com/UYI2E4F.png) + +If you were unable to build the CLI tool, run `go test` (in both `~/bin/go/src/github.com/andrewpurkett/go.wemo/wemo` and `~/bin/go/src/github.com/andrewpurkett/go.wemo/`), check your `GOPATH`, `GOROOT`, and repeat any other golang setup steps required. + +## Utilizing the library in projects + +Here is some example usage of the various functionality incorporated in this go repository: + ### Example - Device discovery ``` @@ -13,7 +52,7 @@ package main import ( "fmt" - "github.com/savaki/go.wemo" + "github.com/andrewpurkett/go.wemo" "time" ) @@ -33,7 +72,7 @@ package main import ( "fmt" - "github.com/savaki/go.wemo" + "github.com/andrewpurkett/go.wemo" ) func main() { @@ -61,7 +100,7 @@ As a convenience method, you can control lights through a more generic interface package main import ( - "github.com/savaki/go.wemo" + "github.com/andrewpurkett/go.wemo" "time" ) diff --git a/device.go b/device.go index 10cb4cf..a193acd 100644 --- a/device.go +++ b/device.go @@ -24,6 +24,7 @@ import ( "net/http" "regexp" "strconv" + "math/big" ) type Device struct { @@ -60,6 +61,7 @@ func unmarshalDeviceInfo(data []byte) (*DeviceInfo, error) { if err != nil { return nil, err } + resp.DeviceInfo.MacAddress = IncrementMacAddress(resp.DeviceInfo.MacAddress) return &resp.DeviceInfo, nil } @@ -82,6 +84,13 @@ func (d *Device) FetchDeviceInfo(ctx context.Context) (*DeviceInfo, error) { return deviceInfo, nil } +func IncrementMacAddress(inputMac string) (string) { + p1:=big.NewInt(0) + p1.SetString(inputMac, 16) + p1 = big.NewInt(0).Add(p1, big.NewInt(1)) + return fmt.Sprintf("%0X", p1) +} + func (d *Device) GetBinaryState() int { message := newGetBinaryStateMessage() response, err := post(d.Host, "GetBinaryState", message) diff --git a/device_test.go b/device_test.go index 77ccd19..22ce555 100644 --- a/device_test.go +++ b/device_test.go @@ -46,7 +46,7 @@ func TestParseResponseXML(t *testing.T) { 221248K0102C92 uuid:Socket-1_0-221248K0102C92 123456789 - EC1A5974B1EC + EC1A5974B1EB WeMo_US_2.00.2769.PVT 0|49153 1 diff --git a/discover.go b/discover.go index 016dbdb..f821ab3 100644 --- a/discover.go +++ b/discover.go @@ -27,9 +27,16 @@ type Wemo struct { func (self *Wemo) DiscoverAll(timeout time.Duration) ([]*Device, error) { urns := []string{ - "urn:Belkin:device:controllee:1", - "urn:Belkin:device:light:1", - "urn:Belkin:device:sensor:1", + // TODO: Include additional third-party WeMo devices from Holmes and Mr. Coffee + "urn:Belkin:device:bridge:1", // WeMo Link + "urn:Belkin:device:controllee:1", // WeMo Switch + "urn:Belkin:device:crockpot:1", // WeMo Crockpot + "urn:Belkin:device:insight:1", // WeMo Insight Switch + "urn:Belkin:device:lightswitch:1",// WeMo Light Switch + "urn:Belkin:device:Maker:1", // WeMo Maker + "urn:Belkin:device:netcam:1", // WeMo NetCam and NetCam HD+ + "urn:Belkin:device:sensor:1", // WeMo Motion Sensor + "urn:Belkin:device:wemo_baby:1", // WeMo Baby Monitor } var all []*Device diff --git a/wemo/discover.go b/wemo/discover.go index 45c099f..b7e892a 100644 --- a/wemo/discover.go +++ b/wemo/discover.go @@ -4,7 +4,7 @@ import ( "code.google.com/p/go.net/context" "fmt" "github.com/codegangsta/cli" - "github.com/savaki/go.wemo" + "github.com/andrewpurkett/go.wemo" "log" "sort" "time" @@ -15,9 +15,18 @@ var discoverCommand = cli.Command{ Usage: "find devices in the local network", Description: "search for devices in the local network", Flags: []cli.Flag{ - cli.StringFlag{"interface", "", "search by interface", ""}, - cli.StringFlag{"ip", "", "discovery wemo by ip", ""}, - cli.IntFlag{"timeout", 3, "timeout", ""}, + cli.StringFlag{ + Name: "interface", + Value: "", + Usage: "search by interface", + EnvVar: "WEMO_IFACE", + }, + cli.IntFlag{ + Name: "timeout", + Value: 3, + Usage: "timeout period in seconds", + EnvVar: "WEMO_TIMEOUT_DISCOVERY", + }, }, Action: commandAction, } @@ -36,18 +45,20 @@ func commandAction(c *cli.Context) { log.Fatal(err) } - format := "%-20s %-20s %-21s %-20s\n" + format := "%-22s %-18s %-15s %-26s %-35s\n" fmt.Printf(format, "Host", + "Mac Address", + "Serial Number", "Friendly Name", "Firmware Version", - "Serial Number", ) fmt.Printf(format, - "----------------", - "----------------", - "----------------", - "----------------", + "---------------------", + "-----------------", + "--------------", + "-------------------------", + "-----------------------------------", ) deviceInfos := wemo.DeviceInfos{} @@ -63,8 +74,9 @@ func commandAction(c *cli.Context) { for _, deviceInfo := range deviceInfos { fmt.Printf(format, deviceInfo.Device.Host, + fmt.Sprintf("%s:%s:%s:%s:%s:%s", deviceInfo.MacAddress[0:2], deviceInfo.MacAddress[2:4], deviceInfo.MacAddress[4:6], deviceInfo.MacAddress[6:8], deviceInfo.MacAddress[8:10], deviceInfo.MacAddress[10:12]), + deviceInfo.SerialNumber, deviceInfo.FriendlyName, - deviceInfo.FirmwareVersion, - deviceInfo.SerialNumber) + deviceInfo.FirmwareVersion) } } diff --git a/wemo/main.go b/wemo/main.go index ffbea44..9e4853b 100644 --- a/wemo/main.go +++ b/wemo/main.go @@ -22,7 +22,7 @@ func main() { app := cli.NewApp() app.Name = "wemo" app.Usage = "command line interface wemo" - app.Version = "0.1" + app.Version = "0.2" app.Commands = []cli.Command{ discoverCommand, onCommand, diff --git a/wemo/power.go b/wemo/power.go index 9227d40..531c4b4 100644 --- a/wemo/power.go +++ b/wemo/power.go @@ -2,13 +2,18 @@ package main import ( "github.com/codegangsta/cli" - "github.com/savaki/go.wemo" + "github.com/andrewpurkett/go.wemo" ) var onCommand = cli.Command{ Name: "on", Flags: []cli.Flag{ - cli.StringFlag{"host", "", "device host and ip e.g. 10.0.1.2:49128", ""}, + cli.StringFlag{ + Name: "host", + Value: "", + Usage: "device host and ip e.g. 10.0.1.2:49128", + EnvVar: "WEMO_POWER_HOST", + }, }, Action: onAction, } @@ -24,7 +29,12 @@ func onAction(c *cli.Context) { var offCommand = cli.Command{ Name: "off", Flags: []cli.Flag{ - cli.StringFlag{"host", "", "device host and ip e.g. 10.0.1.2:49128", ""}, + cli.StringFlag{ + Name: "host", + Value: "", + Usage: "device host and ip e.g. 10.0.1.2:49128", + EnvVar: "WEMO_POWER_HOST", + }, }, Action: offAction, } @@ -40,7 +50,12 @@ func offAction(c *cli.Context) { var toggleCommand = cli.Command{ Name: "toggle", Flags: []cli.Flag{ - cli.StringFlag{"host", "", "device host and ip e.g. 10.0.1.2:49128", ""}, + cli.StringFlag{ + Name: "host", + Value: "", + Usage: "device host and ip e.g. 10.0.1.2:49128", + EnvVar: "WEMO_POWER_HOST", + }, }, Action: toggleAction, }