コード例 #1
0
ファイル: tasks.py プロジェクト: mmon8716/platipy
def move_task(task, endpoint, seriesUIDs, host, port, ae_title):
    """
    Background task that triggers the Dicom MOVE operation at the given endpoint
    for the given seriesUIDs
    """

    # For each series UID supplied, fetch the image series and run the algorithm
    total = len(seriesUIDs)
    count = 0

    dicom_connector = DicomConnector(host=host, port=port, ae_title=ae_title)

    task.update_state(
        state="PROGRESS",
        meta={
            "current": count,
            "total": total,
            "status": "Verifying dicom location"
        },
    )

    dicom_verify = dicom_connector.verify()

    if dicom_verify == None:
        return {
            "current": 100,
            "total": 100,
            "status": "Unable to connect to dicom location",
        }

    for suid in seriesUIDs:

        task.update_state(
            state="PROGRESS",
            meta={
                "current": count,
                "total": total,
                "status": "Moving series for UID: {0}".format(suid),
            },
        )

        logger.info("Moving Series with UID: {0}".format(suid))
        dicom_connector.move_series(suid)

        count = count + 1

    task.update_state(
        state="SUCCESS",
        meta={
            "current": total,
            "total": total,
            "status": "Move Complete"
        },
    )
コード例 #2
0
ファイル: api.py プロジェクト: pyplati/platipy
    def post(self):

        key = request.headers["API_KEY"]

        args = self.parser.parse_args()
        dataset_id = args["dataset"]

        # Get the dataset to which this data object should be added
        ds = Dataset.query.filter_by(owner_key=key, id=dataset_id).first()
        if not ds:
            return {"Error": "Dataset not found"}, 404

        # Get the parent dataset if one was given
        parent = None
        if args["parent"]:
            parent = DataObject.query.filter_by(dataset_id=ds.id,
                                                id=args["parent"]).first()

            if not parent:
                return {"Error": "Parent Data Object not found"}, 404

        meta_data = None
        if args["meta_data"]:
            meta_data = json.loads(args["meta_data"])

        # Create the DataObject
        do = DataObject(
            dataset=ds,
            is_input=True,
            type=args["type"],
            series_instance_uid=args["seriesUID"],
            meta_data=meta_data,
            parent=parent,
        )
        db.session.add(do)
        db.session.commit()

        if args["type"] == "DICOM":

            dicom_fetch = args["dicom_retrieve"]
            if not dicom_fetch:
                return (
                    {
                        "message": {
                            "dicom_retrieve":
                            "Set GET, MOVE or SEND to be able to retrieve Dicom objects."
                        }
                    },
                    400,
                )

            if not args["seriesUID"]:
                return (
                    {
                        "message": {
                            "seriesUID":
                            "SeriesUID is required to be able to retrieve DICOM objects"
                        }
                    },
                    400,
                )

            if dicom_fetch == "MOVE":

                if not ds.from_dicom_location:
                    return (
                        {
                            "message": {
                                "from_dicom_location":
                                "Dataset From Dicom Location not set, so unable to MOVE DICOM objects"
                            }
                        },
                        400,
                    )

                # Fetch Dicom data using MOVE
                # Check whether or not we are listening for for Dicom MOVE
                listening_connector = DicomConnector(
                    host="127.0.0.1",
                    port=app.dicom_listener_port,
                    ae_title=app.dicom_listener_aetitle,
                )

                if not listening_connector.verify():

                    # Verify Dicom Location is listening
                    timeout_seconds = 20
                    time_waited = 0

                    # We are not listening, wait for 20 seconds and abort if still not listening
                    while not listening_connector.verify():
                        logger.debug(
                            "Not listening for MOVE, sleeping for 1 second and will try again"
                        )
                        time.sleep(1)
                        time_waited += 1

                        if time_waited >= timeout_seconds:
                            msg = "Listener for MOVE timeout on port: {0}".format(
                                ds.from_dicom_location.move_port)
                            logger.error(msg)
                            return {
                                "message": {
                                    "from_dicom_location": msg
                                }
                            }, 400

                    logger.info("Listening for MOVE OK")

                # Trigger MOVE
                logger.info(
                    "Triggering MOVE at {0} for series UID: {1}",
                    app.dicom_listener_aetitle,
                    do.series_instance_uid,
                )
                dicom_connector = DicomConnector(
                    host=ds.from_dicom_location.host,
                    port=ds.from_dicom_location.port,
                    ae_title=ds.from_dicom_location.ae_title,
                )

                dicom_verify = dicom_connector.verify()

                if dicom_verify:
                    dicom_connector.move_series(
                        do.series_instance_uid,
                        move_aet=app.dicom_listener_aetitle)
                else:
                    msg = "Unable to connect to Dicom Location: {0} {1} {2}".format(
                        ds.from_dicom_location.host,
                        ds.from_dicom_location.port,
                        ds.from_dicom_location.ae_title,
                    )
                    logger.error(msg)
                    return {"message": {"from_dicom_location": msg}}, 400

            elif dicom_fetch == "GET":

                if not ds.from_dicom_location:
                    return (
                        {
                            "message": {
                                "from_dicom_location":
                                "Dataset From Dicom Location not set, so unable to GET DICOM objects"
                            }
                        },
                        400,
                    )

                # Fetch Dicom data using GET
                task = retrieve_task.apply_async([do.id])

            # If dicom_fetch is SEND we don't do anything here, just wait for the client
            # to send to our Dicom Listener.

        elif args["type"] == "FILE":

            if not args["file_name"]:
                return {"message": {"file_name": "Provide the file name"}}, 400

            if not args["file_data"]:
                return {"message": {"file_data": "Provide the file data"}}, 400

            # Save the file
            file_path = os.path.join(tempfile.mkdtemp(), args["file_name"])
            args["file_data"].save(file_path)
            do.is_fetched = True
            do.path = file_path

            db.session.add(do)
            db.session.commit()

        return do