diff --git a/Device.go b/Device.go index d5ae766..405af61 100644 --- a/Device.go +++ b/Device.go @@ -140,23 +140,32 @@ func GetAvailableDevicesAtSpecificEthernetInterface(interfaceName string) ([]Dev return nvtDevices, nil } -func (dev *Device) getSupportedServices(resp *http.Response) { +func (dev *Device) getSupportedServices(resp *http.Response) error { doc := etree.NewDocument() - data, _ := ioutil.ReadAll(resp.Body) + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + resp.Body.Close() if err := doc.ReadFromBytes(data); err != nil { //log.Println(err.Error()) - return + return err } + services := doc.FindElements("./Envelope/Body/GetCapabilitiesResponse/Capabilities/*/XAddr") for _, j := range services { dev.addEndpoint(j.Parent().Tag, j.Text()) } + extension_services := doc.FindElements("./Envelope/Body/GetCapabilitiesResponse/Capabilities/Extension/*/XAddr") for _, j := range extension_services { dev.addEndpoint(j.Parent().Tag, j.Text()) } + + return nil } //NewDevice function construct a ONVIF Device entity @@ -178,7 +187,11 @@ func NewDevice(params DeviceParams) (*Device, error) { return nil, errors.New("camera is not available at " + dev.params.Xaddr + " or it does not support ONVIF services") } - dev.getSupportedServices(resp) + err = dev.getSupportedServices(resp) + if err != nil { + return nil, err + } + return dev, nil } diff --git a/api/api.go b/api/api.go index df7f363..03a554c 100644 --- a/api/api.go +++ b/api/api.go @@ -153,6 +153,8 @@ func callNecessaryMethod(serviceName, methodName, acceptedData, username, passwo return "", errors.Annotate(err, "ReadAll") } + servResp.Body.Close() + return string(rsp), nil } diff --git a/event/operation.go b/event/operation.go index cbc967c..0dc4206 100644 --- a/event/operation.go +++ b/event/operation.go @@ -21,18 +21,18 @@ type SubscriptionPolicy struct { //tev http://www.onvif.org/ver10/events/wsdl //Subscribe action for subscribe event topic type Subscribe struct { //http://docs.oasis-open.org/wsn/b-2.xsd - XMLName struct{} `xml:"wsnt:Subscribe"` - ConsumerReference EndpointReferenceType `xml:"wsnt:ConsumerReference"` - Filter FilterType `xml:"wsnt:Filter"` - SubscriptionPolicy SubscriptionPolicy `xml:"wsnt:SubscriptionPolicy"` - InitialTerminationTime TerminationTime `xml:"wsnt:InitialTerminationTime"` + XMLName struct{} `xml:"wsnt:Subscribe"` + ConsumerReference EndpointReferenceType `xml:"wsnt:ConsumerReference"` + Filter FilterType `xml:"wsnt:Filter"` + SubscriptionPolicy SubscriptionPolicy `xml:"wsnt:SubscriptionPolicy"` + InitialTerminationTime AbsoluteOrRelativeTimeType `xml:"wsnt:InitialTerminationTime"` } //SubscribeResponse message for subscribe event topic type SubscribeResponse struct { //http://docs.oasis-open.org/wsn/b-2.xsd - SubscriptionReference EndpointReferenceType //`xml:"wsnt:subscriptionReference"` - CurrentTime CurrentTime //`xml:"wsnt:CurrentTime"` - TerminationTime TerminationTime //`xml:"wsnt:TerminationTime"` + SubscriptionReference EndpointReferenceType + CurrentTime CurrentTime + TerminationTime TerminationTime } //Renew action for refresh event topic subscription @@ -57,7 +57,6 @@ type UnsubscribeResponse struct { //http://docs.oasis-open.org/wsn/b-2.xsd } //CreatePullPointSubscription action -//BUG(r) Bad AbsoluteOrRelativeTimeType type type CreatePullPointSubscription struct { XMLName string `xml:"tev:CreatePullPointSubscription"` Filter FilterType `xml:"tev:Filter"` diff --git a/event/types.go b/event/types.go index 0d7afd3..1b088f3 100644 --- a/event/types.go +++ b/event/types.go @@ -2,6 +2,7 @@ package event import ( "github.com/use-go/onvif/xsd" + "github.com/use-go/onvif/xsd/onvif" ) //Address Alias @@ -9,8 +10,10 @@ type Address xsd.String //CurrentTime alias type CurrentTime xsd.DateTime //wsnt http://docs.oasis-open.org/wsn/b-2.xsd + //TerminationTime alias type TerminationTime xsd.DateTime //wsnt http://docs.oasis-open.org/wsn/b-2.xsd + //FixedTopicSet alias type FixedTopicSet xsd.Boolean //wsnt http://docs.oasis-open.org/wsn/b-2.xsd @@ -23,26 +26,32 @@ type TopicExpressionDialect xsd.AnyURI //Message alias type Message xsd.AnyType +//MessageNotification alias +type MessageNotification struct { + Message MessageNotificationHolderType +} + +type MessageNotificationHolderType struct { + UtcTime xsd.DateTime `xml:",attr"` + PropertyOperation xsd.String `xml:",attr"` + Source onvif.SimpleItem `xml:"Source>SimpleItem"` + Data onvif.SimpleItem `xml:"Data>SimpleItem"` +} + //ActionType for AttributedURIType type ActionType AttributedURIType //AttributedURIType in ws-addr type AttributedURIType xsd.AnyURI //wsa https://www.w3.org/2005/08/addressing/ws-addr.xsd -//AbsoluteOrRelativeTimeType -type AbsoluteOrRelativeTimeType struct { //wsnt http://docs.oasis-open.org/wsn/b-2.xsd - xsd.DateTime - xsd.Duration -} +// AbsoluteOrRelativeTimeType +type AbsoluteOrRelativeTimeType xsd.AnySimpleType //wsnt http://docs.oasis-open.org/wsn/b-2.xsd //EndpointReferenceType in ws-addr type EndpointReferenceType struct { //wsa http://www.w3.org/2005/08/addressing/ws-addr.xsd - // Address AttributedURIType `xml:"wsnt:Address"` - // ReferenceParameters ReferenceParametersType `xml:"wsnt:ReferenceParameters"` - // Metadata MetadataType `xml:"wsnt:Metadata"` - Address AttributedURIType `xml:"Address"` - ReferenceParameters ReferenceParametersType `xml:"wsnt:ReferenceParameters"` - Metadata MetadataType `xml:"wsnt:Metadata"` + Address AttributedURIType + ReferenceParameters ReferenceParametersType + Metadata MetadataType } // FilterType struct @@ -94,7 +103,7 @@ type NotificationMessageHolderType struct { SubscriptionReference SubscriptionReference //wsnt http://docs.oasis-open.org/wsn/b-2.xsd Topic Topic ProducerReference ProducerReference - Message Message + Message MessageNotification } //NotificationMessage Alias diff --git a/networking/networking.go b/networking/networking.go index eae388b..1e85a1b 100644 --- a/networking/networking.go +++ b/networking/networking.go @@ -2,12 +2,16 @@ package networking import ( "bytes" - "github.com/juju/errors" + "fmt" "net/http" + + "github.com/juju/errors" ) // SendSoap send soap message func SendSoap(httpClient *http.Client, endpoint, message string) (*http.Response, error) { + fmt.Printf("SendSoap message:%s\n", message) + resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message)) if err != nil { return resp, errors.Annotate(err, "Post") diff --git a/sdk/doc.go b/sdk/doc.go index afbd386..255af97 100644 --- a/sdk/doc.go +++ b/sdk/doc.go @@ -32,22 +32,25 @@ func ReadAndParse(ctx context.Context, httpReply *http.Response, reply interface Str("action", tag). Msg("RPC") // TODO(jfsmig): extract the deadline from ctx.Deadline() and apply it on the reply reading - if b, err := ioutil.ReadAll(httpReply.Body); err != nil { + b, err := ioutil.ReadAll(httpReply.Body) + if err != nil { return errors.Annotate(err, "read") - } else { - // my case - if tag == "Subscribe" || - tag == "CreatePullPointSubscription" || - tag == "GetStreamUri" || - tag == "GetSnapshotUri" || - tag == "GetEventProperties" || - tag == "GetServiceCapabilities" || - tag == "Unsubscribe" || - tag == "GetCapabilities" { - fmt.Printf("body received for %s:%s\n", tag, b) - } - - err = xml.Unmarshal(b, reply) - return errors.Annotate(err, "decode") } + + httpReply.Body.Close() + + // my case + if tag == "Subscribe" || + tag == "CreatePullPointSubscription" || + tag == "GetStreamUri" || + tag == "GetSnapshotUri" || + tag == "GetEventProperties" || + tag == "GetServiceCapabilities" || + tag == "Unsubscribe" || + tag == "GetCapabilities" { + fmt.Printf("body received for %s\n:%s\n", tag, b) + } + + err = xml.Unmarshal(b, reply) + return errors.Annotate(err, "decode") } diff --git a/xsd/built_in.go b/xsd/built_in.go index b2a015f..1bb79d1 100644 --- a/xsd/built_in.go +++ b/xsd/built_in.go @@ -304,9 +304,8 @@ type GYearMonth AnySimpleType /* Construct an instance of xsd GYearMonth type */ -func (tp GYearMonth) NewGYearMonth(time time.Time) GYearMonth { - return GYearMonth(fmt.Sprintf("", time.Year(), "-", time.Month())) - //return GYearMonth(time.Format("2004-04-05:00")) +func (tp GYearMonth) NewGYearMonth(t time.Time) GYearMonth { + return GYearMonth(fmt.Sprintf("%4d-%2d", t.Year(), t.Month())) } /* @@ -326,9 +325,8 @@ type GYear AnySimpleType /* Construct an instance of xsd GYear type */ -func (tp GYear) NewGYear(time time.Time) GYear { - return GYear(fmt.Sprintf("", time.Year())) - //return GYearMonth(time.Format("2004-04-05:00")) +func (tp GYear) NewGYear(t time.Time) GYear { + return GYear(fmt.Sprintf("%4d", t.Year())) } /* @@ -346,8 +344,8 @@ type GMonthDay AnySimpleType /* Construct an instance of xsd GMonthDay type */ -func (tp GMonthDay) NewGMonthDay(time time.Time) GMonthDay { - return GMonthDay(fmt.Sprintf("--", time.Month(), "-", time.Day())) +func (tp GMonthDay) NewGMonthDay(t time.Time) GMonthDay { + return GMonthDay(fmt.Sprintf("--%2d-%2d", t.Month(), t.Day())) } /* @@ -366,8 +364,8 @@ type GDay AnySimpleType /* Construct an instance of xsd GDay type */ -func (tp GDay) NewGDay(time time.Time) GDay { - return GDay(fmt.Sprintf("---", time.Day())) +func (tp GDay) NewGDay(t time.Time) GDay { + return GDay(fmt.Sprintf("---%2d", t.Day())) } /* @@ -384,8 +382,8 @@ func (tp GDay) NewGDay(time time.Time) GDay { */ type GMonth AnySimpleType -func (tp GMonth) NewGMonth(time time.Time) GMonth { - return GMonth(fmt.Sprintf("--", time.Month())) +func (tp GMonth) NewGMonth(t time.Time) GMonth { + return GMonth(fmt.Sprintf("--%2d", t.Month())) } /* diff --git a/xsd/onvif/onvif.go b/xsd/onvif/onvif.go index e0aca6e..1f63743 100644 --- a/xsd/onvif/onvif.go +++ b/xsd/onvif/onvif.go @@ -509,8 +509,8 @@ type ItemList struct { } type SimpleItem struct { - Name string `xml:"onvif:Name,attr"` - Value xsd.AnySimpleType `xml:"onvif:Value,attr"` + Name string `xml:"Name,attr"` + Value xsd.AnySimpleType `xml:"Value,attr"` } type ElementItem struct {