MissiaL / hikvision-client

Client for Hikvision devices
MIT License
209 stars 43 forks source link

Pass results method #9

Closed horendus closed 5 years ago

horendus commented 5 years ago

I am having absolutely no luck passing the results of response = cam.Event.notification.alertStream(method='get', type='stream') shown in your example.

I can print the results, looks like a JSON response but I cannot do any passing of it at all!

I have tried treating it as a String, List and Dict but I cannot perform anything such as string.find() or referring to Key Value pairs as if it was organized as a dict or list.

I have also tried passing it as if its JSON with json.loads(x) with no change in the ability to read any key value or search 'response.'

What is the data type? I cant event even perform a print (type(response)) on response

All I need to do is check the value of "eventDescription" or even just know is "VMD"is somewhere within the response!

Is there some other way I should be referencing the response elements?

rtorchia commented 5 years ago

If I remember correctly, the response is a dict of a list, and I've accessed the events like this in my projects: response = cam.Event.notification.alertStream(method='get', type='stream') print response[0]['eventNotificationAlert']['eventDescription']

MissiaL commented 5 years ago

If I remember correctly, the response is a dict of a list, and I've accessed the events like this in my projects: response = cam.Event.notification.alertStream(method='get', type='stream') print response[0]['eventNotificationAlert']['eventDescription']

I confirm @rtorchia response By default there should be a list with a dictionary inside. If this is not the case, could you provide the data type in your response. I'll see

horendus commented 5 years ago

Thanks for the response. Really appropriate the help!

Iv tried

response = cam.Event.notification.alertStream(method='get', type='stream') print response[0]['eventNotificationAlert']['eventDescription']

and im still not getting a print out of the eventDescription. Just no PRINT out at all

print response gives something like this

[{u'EventNotificationAlert': {u'@xmlns': u'http://www.std-cgi.com/ver20/XMLSchema', u'macAddress': u'44:47:cc:af:f1:2a', u'protocol': u'HTTP', u'eventDescription': u'Motion alarm', u'portNo': u'4801', u'@version': u'2.0', u'channelID': u'1', u'activePostCount': u'1', u'dateTime': u'2019-04-18T22:55:17+08:00', u'eventState': u'active', u'eventType': u'VMD', u'DetectionRegionList': None, u'channelName': u'Camera 01', u'ipAddress': u'192.168.0.221'}}]

Here is my current code minus some of the sensitive data

from hikvisionapi import Client

print ("Starting Connection")

Increase timeout if you want to wait for the event to be received

cam1 = Client('hikvisioncamera', 'hikvisioncamera', 'hikvisioncamera', timeout=30) cam2 = Client('hikvisioncamera', 'hikvisioncamera', 'hikvisioncamera', timeout=30)

while True: try: response = cam1.Event.notification.alertStream(method='get', type='stream') if response : print ("Camera 1 Event Received\n") print response[0]['eventNotificationAlert']['eventDescription'] print ("\n\n\n") except Exception: pass

try:
    response = cam2.Event.notification.alertStream(method='get', type='stream')
    if response:
        print ("Camera 2 Event Received\n")
        print response[0]['eventNotificationAlert']['eventDescription']
        print ("\n\n\n")
except Exception:
    pass
MissiaL commented 5 years ago

Increase count_events parameter Then you will receive all the events

cam2 = Client('hikvisioncamera', 'hikvisioncamera', 'hikvisioncamera', timeout=30)
cam2.count_events = 99
response = cam2.Event.notification.alertStream(method='get', type='stream')
rtorchia commented 5 years ago

Me bad...

response = cam.Event.notification.alertStream(method='get', type='stream') print response[0]['eventNotificationAlert']['eventDescription']`

should be EventNotificationAlert, capital letter on the first word. If not, it won't work.

[{u'EventNotificationAlert': {u'@xmlns': u'http://www.std-cgi.com/ver20/XMLSchema', u'macAddress': u'44:47:cc:af:f1:2a', u'protocol': u'HTTP', u'eventDescription': u'Motion alarm', u'portNo': u'4801', u'@version': u'2.0', u'channelID': u'1', u'activePostCount': u'1', u'dateTime': u'2019-04-18T22:55:17+08:00', u'eventState': u'active', u'eventType': u'VMD', u'DetectionRegionList': None, u'channelName': u'Camera 01', u'ipAddress': u'192.168.0.221'}}]

MissiaL commented 5 years ago

How do I understand your problem is now solved?