How to use the pynetdicom.evt.trigger function in pynetdicom

To help you get started, we’ve selected a few pynetdicom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pydicom / pynetdicom / pynetdicom / acse.py View on Github external
def _check_sop_class_common_extended(self):
        """Check the user's response to a SOP Class Common Extended request.

        Returns
        -------
        dict
            The {SOP Class UID : SOPClassCommonExtendedNegotiation} items for
            the accepted SOP Class Common Extended negotiation items.
        """
        # pylint: disable=broad-except
        try:
            rsp = evt.trigger(
                self.assoc,
                evt.EVT_SOP_COMMON,
                {'items' : self.requestor.sop_class_common_extended}
            )
        except Exception as exc:
            LOGGER.error(
                "Exception raised in handler bound to 'evt.EVT_SOP_COMMON'"
            )
            LOGGER.exception(exc)
            return {}

        try:
            rsp = {
                uid:ii for uid, ii in rsp.items()
                if isinstance(ii, SOPClassCommonExtendedNegotiation)
            }
github pydicom / pynetdicom / pynetdicom / service_class.py View on Github external
LOGGER.debug('# DICOM Dataset')
            for elem in identifier.iterall():
                LOGGER.info(elem)
            LOGGER.info('')
        except Exception as ex:
            LOGGER.error("Failed to decode the request's Identifier dataset.")
            LOGGER.exception(ex)
            # Failure - Unable to Process - Failed to decode Identifier
            rsp.Status = 0xC310
            rsp.ErrorComment = 'Unable to decode the dataset'
            self.dimse.send_msg(rsp, context.context_id)
            return

        # Pass the C-FIND request to the user to handle
        try:
            handler = evt.trigger(
                self.assoc,
                evt.EVT_C_FIND,
                {
                    'request' : req,
                    'context' : context.as_tuple,
                    '_is_cancelled' : self.is_cancelled
                }
            )
        except Exception as exc:
            LOGGER.error("Exception in handler bound to 'evt.EVT_C_FIND'")
            LOGGER.exception(exc)
            rsp.Status = 0xC311
            self.dimse.send_msg(rsp, context.context_id)
            return

        # No matches and no yields
github pydicom / pynetdicom / pynetdicom / dul.py View on Github external
"""Decode a received PDU.

        Parameters
        ----------
        bytestream : bytearray
            The received PDU.

        Returns
        -------
        pdu.PDU subclass, str
            The PDU subclass corresponding to the PDU and the event string
            corresponding to receiving that PDU type.
        """
        # Trigger before data is decoded in case of exception in decoding
        bytestream = bytes(bytestream)
        evt.trigger(self.assoc, evt.EVT_DATA_RECV, {'data' : bytestream})

        pdu_types = {
            b'\x01' : (A_ASSOCIATE_RQ, 'Evt6'),
            b'\x02' : (A_ASSOCIATE_AC, 'Evt3'),
            b'\x03' : (A_ASSOCIATE_RJ, 'Evt4'),
            b'\x04' : (P_DATA_TF, 'Evt10'),
            b'\x05' : (A_RELEASE_RQ, 'Evt12'),
            b'\x06' : (A_RELEASE_RP, 'Evt13'),
            b'\x07' : (A_ABORT_RQ, 'Evt16')
        }

        pdu, event = pdu_types[bytestream[0:1]]
        pdu = pdu()
        pdu.decode(bytestream)

        evt.trigger(self.assoc, evt.EVT_PDU_RECV, {'pdu' : pdu})
github pydicom / pynetdicom / pynetdicom / acse.py View on Github external
self.assoc._accepted_cx = {
            cx.context_id:cx for cx in result if cx.result == 0x00
        }
        self.assoc._rejected_cx = [cx for cx in result if cx.result != 0x00]
        # pylint: enable=protected-access

        # Add any SCP/SCU Role Selection Negotiation response items
        for item in ac_roles:
            self.acceptor.add_negotiation_item(item)

        # Send the A-ASSOCIATE (accept) primitive
        LOGGER.info("Accepting Association")
        self.send_accept()

        # Callbacks/Logging
        evt.trigger(self.assoc, evt.EVT_ACCEPTED, {})

        # Assocation established OK
        self.assoc.is_established = True
        evt.trigger(self.assoc, evt.EVT_ESTABLISHED, {})
github pydicom / pynetdicom / pynetdicom / service_class.py View on Github external
LOGGER.debug('# DICOM Dataset')
            for elem in identifier.iterall():
                LOGGER.info(elem)
            LOGGER.info('')
        except Exception as ex:
            LOGGER.error("Failed to decode the request's Identifier dataset.")
            LOGGER.exception(ex)
            # Failure - Unable to Process - Failed to decode Identifier
            rsp.Status = 0xC310
            rsp.ErrorComment = 'Unable to decode the dataset'
            self.dimse.send_msg(rsp, context.context_id)
            return

        # Pass the C-FIND request to the user to handle
        try:
            handler = evt.trigger(
                self.assoc,
                evt.EVT_C_FIND,
                {
                    'request' : req,
                    'context' : context.as_tuple,
                    '_is_cancelled' : self.is_cancelled
                }
            )
        except Exception as exc:
            LOGGER.error("Exception in handler bound to 'evt.EVT_C_FIND'")
            LOGGER.exception(exc)
            rsp.Status = 0xC311
            self.dimse.send_msg(rsp, context.context_id)
            return

        # No matches and no yields
github pydicom / pynetdicom / pynetdicom / dimse.py View on Github external
The DIMSE message primitive to send to the peer.
        context_id : int
            The ID of the presentation context that the message is to be
            sent under.
        """
        if primitive.MessageIDBeingRespondedTo is None:
            dimse_msg = _RQ_TO_MESSAGE[primitive.__class__]()
        else:
            dimse_msg = _RSP_TO_MESSAGE[primitive.__class__]()

        # Convert DIMSE primitive to DIMSE Message
        dimse_msg.primitive_to_message(primitive)
        dimse_msg.context_id = context_id

        # Trigger event
        evt.trigger(
            self.assoc, evt.EVT_DIMSE_SENT, {'message' : dimse_msg}
        )

        # Split the full messages into P-DATA chunks,
        #   each below the max_pdu size
        for pdata in dimse_msg.encode_msg(context_id, self.maximum_pdu_size):
            self.dul.send_pdu(pdata)
github pydicom / pynetdicom / pynetdicom / acse.py View on Github external
.. currentmodule:: pynetdicom.pdu_primitives

        Returns
        -------
        pdu_primitives.AsynchronousOperationsWindowNegotiation or None
            If the ``evt.EVT_ASYNC_OPS`` handler hasn't been implemented
            then returns ``None``, otherwise returns an
            :class:`AsynchronousOperationsWindowNegotiation` item with the
            default values for the number of operations invoked/performed
            (1, 1).
        """
        # pylint: disable=broad-except
        try:
            # Response is always ignored as async ops is not supported
            inv, perf = self.requestor.asynchronous_operations
            _ = evt.trigger(
                self.assoc,
                evt.EVT_ASYNC_OPS,
                {'nr_invoked' : inv, 'nr_performed' : perf}
            )
        except NotImplementedError:
            return None
        except Exception as exc:
            LOGGER.error(
                "Exception raised in handler bound to 'evt.EVT_ASYNC_OPS'"
            )
            LOGGER.exception(exc)

        item = AsynchronousOperationsWindowNegotiation()
        item.maximum_number_operations_invoked = 1
        item.maximum_number_operations_performed = 1
github pydicom / pynetdicom / pynetdicom / fsm.py View on Github external
The DICOM Upper Layer Service instance for the local AE

    Returns
    -------
    str
        ``'Sta13'``, the next state of the state machine
    """
    primitive = A_P_ABORT()
    primitive.provider_reason = 0x02

    # Send A-ABORT PDU.
    pdu = A_ABORT_RQ()
    pdu.from_primitive(primitive)

    dul.socket.send(dul.pdu.encode())
    evt.trigger(dul.assoc, evt.EVT_PDU_SENT, {'pdu' : dul.pdu})

    return 'Sta13'