ZoneMinder / pyzm

Python API, Log, Event Server and Memory wrapper for ZoneMinder
GNU General Public License v2.0
31 stars 20 forks source link

Issue when retrieving events, 'to' and 'from' options seem to be parsed wrong? #21

Closed Cassio-4 closed 3 years ago

Cassio-4 commented 3 years ago

So, I am trying to retrieve events that happened 12 minutes ago, using this options dict (for example):

event_filter = {
    'from': '2020-10-28 17:58:29',
    'to': '2020-10-28 18:10:29',
    'object_only': False,
    'min_alarmed_frames': 1,
    'mid': 4
}

However, once I call the Monitor.events(options=event_filter) method I noticed this as a result: (I censored my token and URL, pay no mind to it)

Oct 28 2020 18:10:35.445790 [DBG 1] make_request called with url=http://myzoneminder/zm/api/events/index/StartTime >=:2020-10-28 18:10:29/EndTime <=:2020-10-28 18:10:29/MonitorId =:4/AlarmFrames >=:1.json payload={} type=get query={'sort': 'StartTime', 'direction': 'desc', 'page': 1, 'limit': 100, 'token':'mytoken'}

the StartTime and EndTime parts of the request are the same timestamp (specifically, they're the 'to' timestamp from my event_filter dict), hence why I am unable to receive any events.

While sorting through the code I couldn't really understand the line 51 of the Events Class. Shouldn't it be from_list = options.get('from').split(" to ", 1) instead of from_list = options.get('to').split(" to ", 1)

Thanks a lot, sorry about any formatting or grammatical errors

pdtwonotes commented 3 years ago

Also an error is encountered if 'to' is not specified. I think it should default to 'now'. This is using the verison that pip3 delivers, which is 0.1.30. Github has 0.2. I just tested with latest master and 'from' is now parsed correctly.

pliablepixels commented 3 years ago

I don't see a missing to causing any error.

import pyzm
import pyzm.api as zmapi
import getpass
import traceback
import pyzm.ZMMemory as zmmemory
import pyzm.helpers.utils as utils
from pyzm.helpers.Base import ConsoleLog

print ('Using pyzm version: {}'.format(pyzm.__version__))

logger = ConsoleLog()
logger.set_level(2)

conf = utils.read_config('/etc/zm/secrets.ini')
api_options  = {
    'apiurl': utils.get(key='ZM_API_PORTAL', section='secrets', conf=conf),
    'portalurl':utils.get(key='ZM_PORTAL', section='secrets', conf=conf),
    'user': utils.get(key='ZM_USER', section='secrets', conf=conf),
    'password': utils.get(key='ZM_PASSWORD', section='secrets', conf=conf),
    'logger': logger, # use none if you don't want to log to ZM,
    #'disable_ssl_cert_check': True
}

zmapi = zmapi.ZMApi(options=api_options)

event_filter = {
    'from': '9 am',
    'to': '7 pm',
    'object_only':False,
    'min_alarmed_frames': 0,
    'max_events':5,

}
cam_events = zmapi.events(event_filter)
print ('I got {} events'.format(len(cam_events.list())))
for e in cam_events.list():
    print ('Event:{} Cause:{} Notes:{}'.format(e.name(), e.cause(), e.notes()))