FalkTannhaeuser / python-onvif-zeep

ONVIF Client Implementation in Python 2+3 (using https://github.com/mvantellingen/python-zeep instead of suds as SOAP client)
MIT License
424 stars 138 forks source link

Blank Topic title in PullMessages #69

Open ashish1405 opened 3 years ago

ashish1405 commented 3 years ago

I'm trying to pull messages using pull point subscription as below.

mycam = ONVIFCamera("192.168.10.180", 80, 'admin', 'admin')
event_service = mycam.create_events_service()
pps = event_service.CreatePullPointSubscription()
plp = mycam.pullpoint.zeep_client.create_service('{http://www.onvif.org/ver10/events/wsdl}PullPointSubscriptionBinding', pps.SubscriptionReference.Address._value_1)
pullmess=plp.PullMessages(Timeout=timedelta(seconds=2), MessageLimit=10)
print(pullmess.NotificationMessage)

it prints

[{
    'SubscriptionReference': None,
    'Topic': {
        '_value_1': None,
        'Dialect': 'http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet',
        '_attr_1': {
    }
    },
    'ProducerReference': None,
    'Message': {
        '_value_1': <Element {http://www.onvif.org/ver10/schema}Message at 0x7fe221a2f348>
    }
}]

The topic value is clearly None, Where as my XML is as below. So how to get tns1:VideoSource/MotionAlarm value?

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
    xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsa5="http://www.w3.org/2005/08/addressing"
    xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
    xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc"
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
    xmlns:wsdd="http://schemas.xmlsoap.org/ws/2005/04/discovery"
    xmlns:chan="http://schemas.microsoft.com/ws/2005/02/duplex"
    xmlns:xmime="http://tempuri.org/xmime.xsd"
    xmlns:xop="http://www.w3.org/2004/08/xop/include"
    xmlns:ns2="http://www.onvif.org/ver10/pacs"
    xmlns:wsrfbf="http://docs.oasis-open.org/wsrf/bf-2"
    xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
    xmlns:tt="http://www.onvif.org/ver10/schema"
    xmlns:wsrfr="http://docs.oasis-open.org/wsrf/r-2"
    xmlns:ns1="http://www.onvif.org/ver10/accesscontrol/wsdl"
    xmlns:ns3="http://www.onvif.org/ver10/actionengine/wsdl"
    xmlns:ns4="http://www.onvif.org/ver10/advancedsecurity/wsdl"
    xmlns:tad="http://www.onvif.org/ver10/analyticsdevice/wsdl"
    xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
    xmlns:tdn="http://www.onvif.org/ver10/network/wsdl"
    xmlns:tds="http://www.onvif.org/ver10/device/wsdl"
    xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
    xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
    xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl"
    xmlns:tls="http://www.onvif.org/ver10/display/wsdl"
    xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl"
    xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"
    xmlns:trc="http://www.onvif.org/ver10/recording/wsdl"
    xmlns:trp="http://www.onvif.org/ver10/replay/wsdl"
    xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
    xmlns:trv="http://www.onvif.org/ver10/receiver/wsdl"
    xmlns:tse="http://www.onvif.org/ver10/search/wsdl"
    xmlns:ter="http://www.onvif.org/ver10/error"
    xmlns:tns1="http://www.onvif.org/ver10/topics">
    <SOAP-ENV:Header>
        <wsa5:Action SOAP-ENV:mustUnderstand="true">http://www.onvif.org/ver10/events/wsdl/PullPointSubscription/PullMessagesResponse</wsa5:Action>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <tev:PullMessagesResponse>
            <tev:CurrentTime>2020-08-05T05:14:07Z</tev:CurrentTime>
            <tev:TerminationTime>2020-08-05T05:14:16Z</tev:TerminationTime>
            <wsnt:NotificationMessage>
                <wsnt:Topic Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet">tns1:VideoSource/MotionAlarm</wsnt:Topic>
                <wsnt:Message>
                    <tt:Message UtcTime="2020-08-05T05:14:07Z" PropertyOperation="Initialized">
                        <tt:Source>
                            <tt:SimpleItem Name="Source" Value="VideoSourceToken" />
                        </tt:Source>
                        <tt:Data>
                            <tt:SimpleItem Name="State" Value="true" />
                        </tt:Data>
                    </tt:Message>
                </wsnt:Message>
            </wsnt:NotificationMessage>
        </tev:PullMessagesResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
ashish1405 commented 3 years ago

I think I found the issue. It seems to be related to a change in b-2.xsd.

The one with this repo contains

  <xsd:complexType name="TopicExpressionType" mixed="true">
    <xsd:sequence>
      <xsd:any minOccurs="0" maxOccurs="1" processContents="lax" />
    </xsd:sequence>
    <xsd:attribute name="Dialect" type="xsd:anyURI" use="required" />
    <xsd:anyAttribute/>
  </xsd:complexType>

where as one other repo contains

<xsd:complexType name="TopicExpressionType" mixed="true">
    <xsd:simpleContent>
      <xsd:extension base="xsd:string">
        <xsd:attribute name="Dialect" type="xsd:anyURI" use="required" />
        <xsd:anyAttribute/>
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

I also tried to use the latest wsdl files from ONVIF site, but that is giving different error.

barban-dev commented 3 years ago

I am the same issue but it seems to me that it is not related to an invalid b-2.xsd file since I can get valid result using the same set of WSDL file and a different SOAP library w.r.t. pythn-zeep. Please refers to the issue I just opened here

ashish1405 commented 3 years ago

@barban-dev yes it's possible. as if you see other elements related to TopicExpressionType, it also has the same structure as previous one. But as the second one was working for me, so I did that change. If at all this is solved in zeep library please inform here so that other people can get reference too.

yingchengpa commented 3 years ago
'_value_1': <Element {http://www.onvif.org/ver10/schema}Message at 0x7fe221a2f348> 

modify b-2.xsd , allmost ipc use the onvif tt:Message

<!-- ================== Message Helper Types  ===================== -->
  <xsd:complexType name="NotificationMessageHolderType" >
    <xsd:sequence>
      <xsd:element ref="wsnt:SubscriptionReference" 
                   minOccurs="0" maxOccurs="1" />
      <xsd:element ref="wsnt:Topic" 
                   minOccurs="0" maxOccurs="1" />
      <xsd:element ref="wsnt:ProducerReference" 
                   minOccurs="0" maxOccurs="1" />
      <xsd:element name="Message">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="tt:Message" 
                     minOccurs="1" maxOccurs="1" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>

    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="NotificationMessage"
               type="wsnt:NotificationMessageHolderType"/>

and add head msg

<xsd:schema 
  targetNamespace="http://docs.oasis-open.org/wsn/b-2"   
  xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
  xmlns:wsa="http://www.w3.org/2005/08/addressing"
  xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2"
  xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:tt="http://www.onvif.org/ver10/schema"
  elementFormDefault="qualified"  attributeFormDefault="unqualified">

<!-- ======================== Imports  ============================ -->

  <xsd:import namespace="http://www.w3.org/2005/08/addressing"
              schemaLocation="./ws-addr.xsd" 
  />

  <xsd:import namespace="http://docs.oasis-open.org/wsrf/bf-2"
              schemaLocation="./bf-2.xsd" 
  />
  <xsd:import namespace="http://docs.oasis-open.org/wsn/t-1"
              schemaLocation="./t-1.xsd" 
  />
  <xsd:import namespace="http://www.onvif.org/ver10/schema"
              schemaLocation="./onvif.xsd" 
  />
Victor-Mo commented 2 years ago

Patching the ' b-2.xsd' did work for me, but it did not feel right to patch a open standards file. The underlying problem is that the message field is defined as 'any', that means the field can be anything and because there is no schema for it, zeep cannot decode it. By patching the type to 'tt:message' zeep knows what to do with it. But it also means it does not support anything else than 'tt:message' type messages. I posted an alternative solution in a issue for the zeep project that will also work for this issue. But without having to patch the xsd file.