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

Improve compatibility, documentation, and interface #7

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b596456
Add support for additional WeMo devices
andrewpurkett Jan 5, 2016
2c43625
Include Mac Address in output TODO -- Correct MacAddress offset
andrewpurkett Jan 5, 2016
b8cd997
Include missing parameter for cli calls
andrewpurkett Jan 5, 2016
f258fbc
Enable testing using go get in localized fork
andrewpurkett Jan 5, 2016
f086c92
Fixes for cli usage applied after testing in named fork
andrewpurkett Jan 5, 2016
44f658f
Enable testing using go get in localized fork
andrewpurkett Jan 5, 2016
b844a88
Merge branch 'forktest' of github.com:andrewpurkett/go.wemo into fork…
andrewpurkett Jan 5, 2016
c61e144
Enable shorthand notation
andrewpurkett Jan 5, 2016
8eecbfb
Revert "Enable shorthand notation"
andrewpurkett Jan 5, 2016
b0ab2b8
Discovery interface cleanup
andrewpurkett Jan 5, 2016
cc7d477
Add function to correct offset mac addresses and break test intention…
andrewpurkett Jan 5, 2016
303ec04
Fix intentionally failing test
andrewpurkett Jan 5, 2016
3043e1f
Show mac address column
andrewpurkett Jan 5, 2016
670b9ae
Update README to include new golang user guide
andrewpurkett Jan 5, 2016
f37d37d
Enable testing using go get in localized fork
andrewpurkett Jan 5, 2016
d2042d6
Merge branch 'forktest' of https://github.com/andrewpurkett/go.wemo i…
andrewpurkett Jan 5, 2016
d3d64a1
Update README.md
andrewpurkett Jan 5, 2016
00eacc5
Update README.md
andrewpurkett Jan 5, 2016
da21c62
Update README.md
andrewpurkett Jan 5, 2016
31347cd
Add example screenshot
andrewpurkett Jan 5, 2016
2824f72
Update README.md
andrewpurkett Jan 5, 2016
1ef0dee
Update README.md
andrewpurkett Jan 5, 2016
e88edab
Update README.md
andrewpurkett Jan 5, 2016
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
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
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

```
package main

import (
"fmt"
"github.com/savaki/go.wemo"
"github.com/andrewpurkett/go.wemo"
"time"
)

Expand All @@ -33,7 +72,7 @@ package main

import (
"fmt"
"github.com/savaki/go.wemo"
"github.com/andrewpurkett/go.wemo"
)

func main() {
Expand Down Expand Up @@ -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"
)

Expand Down
9 changes: 9 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"regexp"
"strconv"
"math/big"
)

type Device struct {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestParseResponseXML(t *testing.T) {
<serialNumber>221248K0102C92</serialNumber>
<UDN>uuid:Socket-1_0-221248K0102C92</UDN>
<UPC>123456789</UPC>
<macAddress>EC1A5974B1EC</macAddress>
<macAddress>EC1A5974B1EB</macAddress>
<firmwareVersion>WeMo_US_2.00.2769.PVT</firmwareVersion>
<iconVersion>0|49153</iconVersion>
<binaryState>1</binaryState>
Expand Down
13 changes: 10 additions & 3 deletions discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 24 additions & 12 deletions wemo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
Expand All @@ -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{}
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion wemo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 19 additions & 4 deletions wemo/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down