Add New*Clients functions to the generated code to simplify their use.
This commit is contained in:
@ -5,40 +5,32 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/huin/goupnp"
|
||||
"github.com/huin/goupnp/dcps/internetgateway1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
results, err := goupnp.DiscoverDevices(internetgateway1.URN_WANPPPConnection_1)
|
||||
clients, errors, err := internetgateway1.NewWANPPPConnection1Clients()
|
||||
if err != nil {
|
||||
fmt.Println("Error discovering InternetGatewayDevice with UPnP: ", err)
|
||||
fmt.Println("Error discovering service with UPnP: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Discovered %d InternetGatewayDevices:\n", len(results))
|
||||
for _, maybeRootDevice := range results {
|
||||
if maybeRootDevice.Err != nil {
|
||||
fmt.Println(maybeRootDevice.Err)
|
||||
continue
|
||||
if len(errors) > 0 {
|
||||
fmt.Printf("Error discovering %d services:\n", len(errors))
|
||||
for _, err := range errors {
|
||||
fmt.Println(" ", err)
|
||||
}
|
||||
}
|
||||
|
||||
device := &maybeRootDevice.Root.Device
|
||||
fmt.Printf("Successfully discovered %d services:\n", len(clients))
|
||||
for _, client := range clients {
|
||||
device := &client.RootDevice.Device
|
||||
|
||||
fmt.Println("Device ", device.FriendlyName)
|
||||
wanPPPSrvs := device.FindService(internetgateway1.URN_WANPPPConnection_1)
|
||||
if len(wanPPPSrvs) < 1 {
|
||||
fmt.Printf("Could not find expected service on device %s\n", device.FriendlyName)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, srv := range wanPPPSrvs {
|
||||
client := internetgateway1.WANPPPConnection1{srv.NewSOAPClient()}
|
||||
if addr, err := client.GetExternalIPAddress(); err != nil {
|
||||
fmt.Printf("Failed to get external IP address: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("External IP address: %v\n", addr)
|
||||
}
|
||||
fmt.Println(" Device:", device.FriendlyName)
|
||||
if addr, err := client.GetExternalIPAddress(); err != nil {
|
||||
fmt.Printf(" Failed to get external IP address: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf(" External IP address: %v\n", addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ var packageTmpl = template.Must(template.New("package").Parse(`package {{.Name}}
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/huin/goupnp"
|
||||
"github.com/huin/goupnp/soap"
|
||||
)
|
||||
|
||||
@ -29,9 +30,29 @@ const ({{range .ServiceTypes}}
|
||||
{{$srv := .}}
|
||||
{{$srvIdent := printf "%s%s" .Name .Version}}
|
||||
|
||||
// {{$srvIdent}} is a client for UPnP SOAP service with URN "{{.URN}}".
|
||||
// {{$srvIdent}} is a client for UPnP SOAP service with URN "{{.URN}}". See
|
||||
// goupnp.ServiceClient, which contains RootDevice and Service attributes which
|
||||
// are provided for informational value.
|
||||
type {{$srvIdent}} struct {
|
||||
SOAPClient *soap.SOAPClient
|
||||
goupnp.ServiceClient
|
||||
}
|
||||
|
||||
// New{{$srvIdent}}Clients discovers instances of the service on the network,
|
||||
// and returns clients to any that are found. errors will contain an error for
|
||||
// any devices that replied but which could not be queried, and err will be set
|
||||
// if the discovery process failed outright.
|
||||
//
|
||||
// This is a typical entry calling point into this package.
|
||||
func New{{$srvIdent}}Clients() (clients []*{{$srvIdent}}, errors []error, err error) {
|
||||
var genericClients []goupnp.ServiceClient
|
||||
if genericClients, errors, err = goupnp.NewServiceClients({{$srv.Const}}); err != nil {
|
||||
return
|
||||
}
|
||||
clients = make([]*{{$srvIdent}}, len(genericClients))
|
||||
for i := range genericClients {
|
||||
clients[i] = &{{$srvIdent}}{genericClients[i]}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
{{range .SCPD.Actions}}{{/* loops over *SCPDWithURN values */}}
|
||||
|
@ -176,6 +176,7 @@ func (dcp *DCP) writePackage(outDir string) error {
|
||||
defer packageFile.Close()
|
||||
gofmt := exec.Command("gofmt")
|
||||
gofmt.Stdout = packageFile
|
||||
gofmt.Stderr = os.Stderr
|
||||
gofmtWriter, err := gofmt.StdinPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user