Skip to content

Instantly share code, notes, and snippets.

@gr4y
Last active September 25, 2017 17:58
Show Gist options
  • Select an option

  • Save gr4y/f5dd33cd64f061e6b3b0 to your computer and use it in GitHub Desktop.

Select an option

Save gr4y/f5dd33cd64f061e6b3b0 to your computer and use it in GitHub Desktop.

Revisions

  1. gr4y revised this gist Oct 25, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions wemo.go
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ import (
    func main() {
    devices, err := goupnp.DiscoverDevices("urn:Belkin:device:controllee")
    if err != nil {
    fmt.Println("Error while discovering devices.", err)
    fmt.Println("Error while discovering devices: ", err)
    return
    }
    fmt.Println("Devices: ", len(devices))
    @@ -24,24 +24,24 @@ func main() {
    BinaryState string
    }{}
    if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "GetBinaryState", stateObj, stateObj); err != nil {
    fmt.Println("Error while performing action. ", err)
    fmt.Println("Error while performing action: ", err)
    return
    }
    CurrentState, err := soap.UnmarshalBoolean(stateObj.BinaryState)
    if err != nil {
    panic(err)
    fmt.Println("Error while unmarshaling BinaryState: ", err)
    return
    }
    fmt.Println("CurrentState: ", CurrentState)
    fmt.Println("CurrentState: ", stateObj.BinaryState)
    NewState, err := soap.MarshalBoolean(!CurrentState)
    if err != nil {
    panic(err)
    fmt.Println("Error while marshaling BinaryState: ", err)
    return
    }
    fmt.Println("NewState: ", NewState)
    stateObj.BinaryState = NewState
    if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "SetBinaryState", stateObj, stateObj); err != nil {
    fmt.Println("Error while performing action. ", err)
    fmt.Println("Error while performing action: ", err)
    return
    }
    }
  2. gr4y created this gist Oct 25, 2015.
    54 changes: 54 additions & 0 deletions wemo.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    package main

    import (
    "fmt"
    "github.com/huin/goupnp"
    "github.com/huin/goupnp/soap"
    )

    func main() {
    devices, err := goupnp.DiscoverDevices("urn:Belkin:device:controllee")
    if err != nil {
    fmt.Println("Error while discovering devices.", err)
    return
    }
    fmt.Println("Devices: ", len(devices))
    if len(devices) > 0 {
    for _, device := range devices {
    services := device.Root.Device.FindService("urn:Belkin:service:basicevent:1")
    fmt.Println("Services: ", len(services))
    if len(services) > 0 {
    for _, service := range services {
    soapClient := service.NewSOAPClient()
    stateObj := &struct {
    BinaryState string
    }{}
    if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "GetBinaryState", stateObj, stateObj); err != nil {
    fmt.Println("Error while performing action. ", err)
    return
    }
    CurrentState, err := soap.UnmarshalBoolean(stateObj.BinaryState)
    if err != nil {
    panic(err)
    return
    }
    fmt.Println("CurrentState: ", CurrentState)
    NewState, err := soap.MarshalBoolean(!CurrentState)
    if err != nil {
    panic(err)
    return
    }
    fmt.Println("NewState: ", NewState)
    stateObj.BinaryState = NewState
    if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "SetBinaryState", stateObj, stateObj); err != nil {
    fmt.Println("Error while performing action. ", err)
    return
    }
    }
    }
    }
    } else {
    fmt.Println("No WeMo Switch Devices Found!")
    return
    }
    }