'Event handler for reception of an Iq with req - this is a request.
Verifies that
- all the requested nodes are available
- at least one of the requested fields is available from at least
one of the nodes
If the request passes verification, an accept response is sent, and
the readout process is started in a separate thread.
If the verification fails, a reject message is sent.'

def _handle_event_req(self, iq):

    seqnr = iq['req']['seqnr']
    error_msg = ''
    req_ok = True
    if ((len(self.test_authenticated_from) > 0) and (not (iq['from'] == self.test_authenticated_from))):
        req_ok = False
        error_msg = 'Access    denied'
    process_nodes = []
    if (len(iq['req']['nodes']) > 0):
        for n in iq['req']['nodes']:
            if (not (n['nodeId'] in self.nodes)):
                req_ok = False
                error_msg = ('Invalid    nodeId    ' + n['nodeId'])
        process_nodes = [n['nodeId'] for n in iq['req']['nodes']]
    else:
        process_nodes = self.nodes.keys()
    process_fields = []
    if (len(iq['req']['fields']) > 0):
        found = False
        for f in iq['req']['fields']:
            for node in self.nodes:
                if self.nodes[node]['device'].has_field(f['name']):
                    found = True
                    break
        if (not found):
            req_ok = False
            error_msg = ('Invalid    field    ' + f['name'])
        process_fields = [f['name'] for n in iq['req']['fields']]
    req_flags = iq['req']._get_flags()
    request_delay_sec = None
    if ('when' in req_flags):
        dt = None
        try:
            dt = datetime.datetime.strptime(req_flags['when'], '%Y-%m-%dT%H:%M:%S')
        except ValueError:
            req_ok = False
            error_msg = "Invalid    datetime    in    'when'    flag,    please    use    ISO    format    (i.e.    2013-04-05T15:00:03)."
        if (not (dt is None)):
            dtnow = datetime.datetime.now()
            dtdiff = (dt - dtnow)
            request_delay_sec = (dtdiff.seconds + ((dtdiff.days * 24) * 3600))
            if (request_delay_sec <= 0):
                req_ok = False
                error_msg = ("Invalid    datetime    in    'when'    flag,    cannot    set    a    time    in    the    past.    Current    time:    " + dtnow.isoformat())
    if req_ok:
        session = self._new_session()
        self.sessions[session] = {'from': iq['from'], 'to': iq['to'], 'seqnr': seqnr}
        self.sessions[session]['commTimers'] = {}
        self.sessions[session]['nodeDone'] = {}
        iq.reply()
        iq['accepted']['seqnr'] = seqnr
        if (not (request_delay_sec is None)):
            iq['accepted']['queued'] = 'true'
        iq.send(block=False)
        self.sessions[session]['node_list'] = process_nodes
        if (not (request_delay_sec is None)):
            timer = Timer(request_delay_sec, self._event_delayed_req, args=(session, process_fields, req_flags))
            self.sessions[session]['commTimers']['delaytimer'] = timer
            timer.start()
            return
        if self.threaded:
            tr_req = Thread(target=self._threaded_node_request, args=(session, process_fields, req_flags))
            tr_req.start()
        else:
            self._threaded_node_request(session, process_fields, req_flags)
    else:
        iq.reply()
        iq['type'] = 'error'
        iq['rejected']['seqnr'] = seqnr
        iq['rejected']['error'] = error_msg
        iq.send(block=False)
