2013-10-06 21:06:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var packageTmpl = template.Must(template.New("package").Parse(`package {{.Name}}
|
|
|
|
|
2013-12-31 16:56:20 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2013-12-31 20:01:17 +00:00
|
|
|
"github.com/huin/goupnp"
|
2013-12-31 16:56:20 +00:00
|
|
|
"github.com/huin/goupnp/soap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hack to avoid Go complaining if time isn't used.
|
|
|
|
var _ time.Time
|
2013-10-06 21:06:47 +00:00
|
|
|
|
2013-12-31 18:04:59 +00:00
|
|
|
// Device URNs:
|
2013-10-06 21:06:47 +00:00
|
|
|
const ({{range .DeviceTypes}}
|
2013-12-31 18:04:59 +00:00
|
|
|
{{.Const}} = "{{.URN}}"{{end}}
|
|
|
|
)
|
2013-10-06 21:06:47 +00:00
|
|
|
|
2013-12-31 18:04:59 +00:00
|
|
|
// Service URNs:
|
2013-10-06 21:06:47 +00:00
|
|
|
const ({{range .ServiceTypes}}
|
2013-12-31 18:04:59 +00:00
|
|
|
{{.Const}} = "{{.URN}}"{{end}}
|
|
|
|
)
|
2013-10-06 21:06:47 +00:00
|
|
|
|
|
|
|
{{range .Services}}
|
|
|
|
{{$srv := .}}
|
|
|
|
{{$srvIdent := printf "%s%s" .Name .Version}}
|
|
|
|
|
2013-12-31 20:01:17 +00:00
|
|
|
// {{$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.
|
2013-10-06 21:06:47 +00:00
|
|
|
type {{$srvIdent}} struct {
|
2013-12-31 20:01:17 +00:00
|
|
|
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
|
2013-10-06 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{{range .SCPD.Actions}}{{/* loops over *SCPDWithURN values */}}
|
|
|
|
|
2013-12-31 17:52:43 +00:00
|
|
|
{{$inargs := .InputArguments}}{{$outargs := .OutputArguments}}
|
|
|
|
// {{if $inargs}}Arguments:{{range $inargs}}{{$argWrap := $srv.WrapArgument .}}
|
2013-12-31 18:04:59 +00:00
|
|
|
//
|
|
|
|
// * {{.Name}}: {{$argWrap.Document}}{{end}}{{end}}
|
|
|
|
//
|
2013-12-31 17:52:43 +00:00
|
|
|
// {{if $outargs}}Return values:{{range $outargs}}{{$argWrap := $srv.WrapArgument .}}
|
2013-12-31 18:04:59 +00:00
|
|
|
//
|
|
|
|
// * {{.Name}}: {{$argWrap.Document}}{{end}}{{end}}
|
2013-12-31 17:52:43 +00:00
|
|
|
func (client *{{$srvIdent}}) {{.Name}}({{range $inargs}}{{/*
|
|
|
|
*/}}{{$argWrap := $srv.WrapArgument .}}{{$argWrap.AsParameter}}, {{end}}{{/*
|
|
|
|
*/}}) ({{range $outargs}}{{/*
|
|
|
|
*/}}{{$argWrap := $srv.WrapArgument .}}{{$argWrap.AsParameter}}, {{end}} err error) {
|
2013-12-31 17:06:15 +00:00
|
|
|
// Request structure.
|
2014-01-06 19:47:57 +00:00
|
|
|
request := {{if $inargs}}&{{template "argstruct" $inargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}}
|
2013-12-31 16:56:20 +00:00
|
|
|
// BEGIN Marshal arguments into request.
|
2013-12-31 17:52:43 +00:00
|
|
|
{{range $inargs}}{{$argWrap := $srv.WrapArgument .}}
|
2013-12-31 16:56:20 +00:00
|
|
|
if request.{{.Name}}, err = {{$argWrap.Marshal}}; err != nil {
|
|
|
|
return
|
2013-12-31 17:52:43 +00:00
|
|
|
}{{end}}
|
2013-12-31 16:56:20 +00:00
|
|
|
// END Marshal arguments into request.
|
|
|
|
|
2013-12-31 17:06:15 +00:00
|
|
|
// Response structure.
|
2014-01-06 19:47:57 +00:00
|
|
|
response := {{if $outargs}}&{{template "argstruct" $outargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}}
|
2013-12-31 17:06:15 +00:00
|
|
|
|
2013-12-31 16:56:20 +00:00
|
|
|
// Perform the SOAP call.
|
2014-01-06 19:47:57 +00:00
|
|
|
if err = client.SOAPClient.PerformAction({{$srv.URNParts.Const}}, "{{.Name}}", request, response); err != nil {
|
2013-12-31 16:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// BEGIN Unmarshal arguments from response.
|
2013-12-31 17:52:43 +00:00
|
|
|
{{range $outargs}}{{$argWrap := $srv.WrapArgument .}}
|
2013-12-31 16:56:20 +00:00
|
|
|
if {{.Name}}, err = {{$argWrap.Unmarshal "response"}}; err != nil {
|
2013-10-06 21:06:47 +00:00
|
|
|
return
|
2013-12-31 17:52:43 +00:00
|
|
|
}{{end}}
|
2013-12-31 16:56:20 +00:00
|
|
|
// END Unmarshal arguments from response.
|
2013-10-06 21:06:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
{{end}}{{/* range .SCPD.Actions */}}
|
|
|
|
{{end}}{{/* range .Services */}}
|
2014-01-06 19:47:57 +00:00
|
|
|
|
|
|
|
{{define "argstruct"}}struct {{"{"}}{{range .}}
|
|
|
|
{{.Name}} string
|
|
|
|
{{end}}{{"}"}}{{end}}
|
2013-10-06 21:06:47 +00:00
|
|
|
`))
|