Last active
September 25, 2017 17:58
-
-
Save gr4y/f5dd33cd64f061e6b3b0 to your computer and use it in GitHub Desktop.
Toggle all WeMo switches. If it is on, it will turn off. If it is off, it will turn on. Just a quick little thing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
fmt.Println("Error while unmarshaling BinaryState: ", err) | |
return | |
} | |
fmt.Println("CurrentState: ", stateObj.BinaryState) | |
NewState, err := soap.MarshalBoolean(!CurrentState) | |
if err != nil { | |
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) | |
return | |
} | |
} | |
} | |
} | |
} else { | |
fmt.Println("No WeMo Switch Devices Found!") | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment