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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
const ({{range .DeviceTypes}}
|
|
|
|
{{.Const}} = "{{.URN}}"
|
|
|
|
{{end}})
|
|
|
|
|
|
|
|
const ({{range .ServiceTypes}}
|
|
|
|
{{.Const}} = "{{.URN}}"
|
|
|
|
{{end}})
|
|
|
|
|
|
|
|
{{range .Services}}
|
|
|
|
{{$srv := .}}
|
|
|
|
{{$srvIdent := printf "%s%s" .Name .Version}}
|
|
|
|
|
|
|
|
// {{$srvIdent}} is a client for UPnP SOAP service with URN "{{.URN}}".
|
|
|
|
type {{$srvIdent}} struct {
|
|
|
|
SOAPClient soap.SOAPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
{{range .SCPD.Actions}}{{/* loops over *SCPDWithURN values */}}
|
|
|
|
|
|
|
|
{{$reqType := printf "_%s_%s_Request" $srvIdent .Name}}
|
|
|
|
{{$respType := printf "_%s_%s_Response" $srvIdent .Name}}
|
|
|
|
|
|
|
|
// {{$reqType}} is the XML structure for the input arguments for action {{.Name}}.
|
|
|
|
type {{$reqType}} struct {{"{"}}{{range .Arguments}}{{if .IsInput}}
|
2013-12-31 16:56:20 +00:00
|
|
|
{{.Name}} string
|
2013-10-06 21:06:47 +00:00
|
|
|
{{end}}{{end}}}
|
|
|
|
|
|
|
|
// {{$respType}} is the XML structure for the output arguments for action {{.Name}}.
|
|
|
|
type {{$respType}} struct {{"{"}}{{range .Arguments}}{{if .IsOutput}}
|
2013-12-31 16:56:20 +00:00
|
|
|
{{.Name}} string
|
2013-10-06 21:06:47 +00:00
|
|
|
{{end}}{{end}}}
|
|
|
|
|
2013-10-06 22:16:25 +00:00
|
|
|
// {{.Name}} action.
|
|
|
|
// Arguments:
|
|
|
|
//{{range .Arguments}}{{if .IsInput}}
|
|
|
|
// * {{.Name}}: {{$v := $srv.SCPD.GetStateVariable .RelatedStateVariable}}
|
|
|
|
{{if $v}}// (related state variable: {{$v.Name}})
|
|
|
|
// - {{if $v.AllowedValueRange}}allowed range: {{$v.AllowedValueRange.Minimum}} to {{$v.AllowedValueRange.Maximum}}{{end}}
|
|
|
|
// - {{if $v.AllowedValues}}allowed values:
|
|
|
|
// {{range $i, $val := $v.AllowedValues}}{{if $i}}|{{end}}{{$val}}{{end}}{{end}}
|
|
|
|
//{{else}}
|
|
|
|
// (unknown){{end}}
|
|
|
|
//{{end}}{{end}}
|
|
|
|
//
|
|
|
|
// Return values:
|
|
|
|
//{{range .Arguments}}{{if .IsOutput}}
|
|
|
|
// * {{.Name}}: {{$v := $srv.SCPD.GetStateVariable .RelatedStateVariable}}
|
|
|
|
{{if $v}}// (related state variable: {{$v.Name}})
|
|
|
|
// - {{if $v.AllowedValueRange}}allowed range: {{$v.AllowedValueRange.Minimum}} to {{$v.AllowedValueRange.Maximum}}{{end}}
|
|
|
|
// - {{if $v.AllowedValues}}allowed values:
|
|
|
|
// {{range $i, $val := $v.AllowedValues}}{{if $i}}|{{end}}{{$val}}{{end}}{{end}}
|
|
|
|
//{{else}}
|
|
|
|
// (unknown){{end}}
|
|
|
|
//{{end}}{{end}}
|
2013-10-06 21:06:47 +00:00
|
|
|
func (client *{{$srvIdent}}) {{.Name}}({{range .Arguments}}{{if .IsInput}}
|
2013-12-31 16:56:20 +00:00
|
|
|
{{$argWrap := $srv.Argument .}}{{$argWrap.AsParameter}},{{end}}{{end}}
|
|
|
|
) ({{range .Arguments}}{{if .IsOutput}}
|
|
|
|
{{$argWrap := $srv.Argument .}}{{$argWrap.AsParameter}},{{end}}{{end}}
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
var request {{$reqType}}
|
|
|
|
// BEGIN Marshal arguments into request.
|
|
|
|
{{range .Arguments}}{{if .IsInput}}{{$argWrap := $srv.Argument .}}
|
|
|
|
if request.{{.Name}}, err = {{$argWrap.Marshal}}; err != nil {
|
|
|
|
return
|
2013-10-06 21:06:47 +00:00
|
|
|
}
|
2013-12-31 16:56:20 +00:00
|
|
|
{{end}}{{end}}
|
|
|
|
// END Marshal arguments into request.
|
|
|
|
|
|
|
|
// Perform the SOAP call.
|
2013-10-06 21:06:47 +00:00
|
|
|
var response {{$respType}}
|
2013-12-31 16:56:20 +00:00
|
|
|
if err = client.SOAPClient.PerformAction({{$srv.URNParts.Const}}, "{{.Name}}", &request, &response); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// BEGIN Unmarshal arguments from response.
|
|
|
|
{{range .Arguments}}{{if .IsOutput}}{{$argWrap := $srv.Argument .}}
|
|
|
|
if {{.Name}}, err = {{$argWrap.Unmarshal "response"}}; err != nil {
|
2013-10-06 21:06:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
{{end}}{{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 */}}
|
|
|
|
`))
|