<commit_before>var Future = Npm.require('fibers/future');

DIMSE = {};

var conn = new Connection({
    vr: {
        split: false
    }
});

var getInstanceRetrievalParams = function(studyInstanceUID, seriesInstanceUID) {
    return {
        0x0020000D: studyInstanceUID ? studyInstanceUID : '',
        0x0020000E: (studyInstanceUID && seriesInstanceUID) ? seriesInstanceUID : '',
        0x00080005: '',
        0x00080020: '',
        0x00080030: '',
        0x00080090: '',
        0x00100010: '',
        0x00100020: '',
        0x00200010: '',
        0x0008103E: '',
        0x00200011: '',
        0x00080016: '', // sopClassUid
        0x00080018: '', // sopInstanceUid
        0x00080060: '', // modality
        0x00200013: '', // instanceNumber
        0x00280010: '', // rows
        0x00280011: '', // columns
        0x00280100: '', // bitsAllocated
        0x00280101: '', // bitsStored
        0x00280102: '', // highBit
        0x00280103: '', // pixelRepresentation
        0x00280004: '', // photometricInterpretation
        0x00280008: '', // numFrames
        0x00181063: '', // frameTime
        0x00281052: '', // rescaleIntercept
        0x00281053: '', // rescaleSlope
        0x00280002: '', // samplesPerPixel
        0x00201041: '', // sliceLocation
        0x00281050: '', // windowCenter
        0x00281051: '', // windowWidth
        0x00280030: '', // pixelSpacing
        0x00200062: '', // laterality
        0x00185101: '', // viewPosition
        0x00080008: '', // imageType
        0x00200032: '', // imagePositionPatient
        0x00200037: '', // imageOrientationPatient
        0x00200052: '', // frameOfReferenceUID

        // Orthanc has a bug here so we can't retrieve sequences at the moment
        // https://groups.google.com/forum/#!topic/orthanc-users/ghKJfvtnK8Y
        //0x00082112: ''  // sourceImageSequence
    };
};

Meteor.startup(function() {
    if (!Meteor.settings.servers.dimse || !Meteor.settings.servers.dimse.length) {
        return;
    }

    // TODO: [custom-servers] use active server and check if type is DIMSE
    var peers = Meteor.settings.servers.dimse[0].peers;
    console.log('Adding DIMSE peers');
    if (peers && peers.length) {
        peers.forEach(function(peer) {
            conn.addPeer(peer);
        });
    }
});

DIMSE.associate = function(contexts, callback, options) {
    var defaults = {
        contexts: contexts
    };
    options = Object.assign(defaults, options);

    conn.associate(options, function(pdu) {
        // associated
        console.log('==Associated');
        callback.call(this, pdu);
    });
};

DIMSE.retrievePatients = function(params, options) {
    //var start = new Date();
    var future = new Future;
    DIMSE.associate([C.SOP_PATIENT_ROOT_FIND], function(pdu) {
        var defaultParams = {
            0x00100010: '',
            0x00100020: '',
            0x00100030: '',
            0x00100040: '',
            0x00101010: '',
            0x00101040: ''
        };

        var result = this.findPatients(Object.assign(defaultParams, params)),
            o = this;

        var patients = [];
        result.on('result', function(msg) {
            patients.push(msg);
        });<commit_msg>Add Meteor.Errors to DIMSE startup for easier configuration debugging<commit_after>var Future = Npm.require('fibers/future');

DIMSE = {};

var conn = new Connection({
    vr: {
        split: false
    }
});

var getInstanceRetrievalParams = function(studyInstanceUID, seriesInstanceUID) {
    return {
        0x0020000D: studyInstanceUID ? studyInstanceUID : '',
        0x0020000E: (studyInstanceUID && seriesInstanceUID) ? seriesInstanceUID : '',
        0x00080005: '',
        0x00080020: '',
        0x00080030: '',
        0x00080090: '',
        0x00100010: '',
        0x00100020: '',
        0x00200010: '',
        0x0008103E: '',
        0x00200011: '',
        0x00080016: '', // sopClassUid
        0x00080018: '', // sopInstanceUid
        0x00080060: '', // modality
        0x00200013: '', // instanceNumber
        0x00280010: '', // rows
        0x00280011: '', // columns
        0x00280100: '', // bitsAllocated
        0x00280101: '', // bitsStored
        0x00280102: '', // highBit
        0x00280103: '', // pixelRepresentation
        0x00280004: '', // photometricInterpretation
        0x00280008: '', // numFrames
        0x00181063: '', // frameTime
        0x00281052: '', // rescaleIntercept
        0x00281053: '', // rescaleSlope
        0x00280002: '', // samplesPerPixel
        0x00201041: '', // sliceLocation
        0x00281050: '', // windowCenter
        0x00281051: '', // windowWidth
        0x00280030: '', // pixelSpacing
        0x00200062: '', // laterality
        0x00185101: '', // viewPosition
        0x00080008: '', // imageType
        0x00200032: '', // imagePositionPatient
        0x00200037: '', // imageOrientationPatient
        0x00200052: '', // frameOfReferenceUID

        // Orthanc has a bug here so we can't retrieve sequences at the moment
        // https://groups.google.com/forum/#!topic/orthanc-users/ghKJfvtnK8Y
        //0x00082112: ''  // sourceImageSequence
    };
};

Meteor.startup(function() {
    if (!Meteor.settings.servers.dimse || !Meteor.settings.servers.dimse.length) {
        return;
    }

    // TODO: [custom-servers] use active server and check if type is DIMSE
    var peers = Meteor.settings.servers.dimse[0].peers;
    if (!peers || !peers.length) {
        throw new Meteor.Error('dimse-config', 'No DIMSE Peers provided.');
    }

    console.log('Adding DIMSE peers');
    try {
        peers.forEach(function(peer) {
            conn.addPeer(peer);
        });
    } catch(error) {
        console.warn('dimse-addPeers: ' + error);
        throw new Meteor.Error('dimse-addPeers', error);
    }
});

DIMSE.associate = function(contexts, callback, options) {
    var defaults = {
        contexts: contexts
    };
    options = Object.assign(defaults, options);

    try {
        conn.associate(options, function(pdu) {
            // associated
            console.log('==Associated');
            callback.call(this, pdu);
        });
    } catch(error) {
        console.warn('dimse-associate: ' + error);
        throw new Meteor.Error('dimse-associate', error);
    }
};

DIMSE.retrievePatients = function(params, options) {
    //var start = new Date();
    var future = new Future;
    DIMSE.associate([C.SOP_PATIENT_ROOT_FIND], function(pdu) {
        var defaultParams = {
            0x00100010: '',
            0x00100020: '',
            0x00100030: '',
            0x00100040: '',
            0x00101010: '',
            0x00101040: ''
        };

        var result = this.findPatients(Object.assign(defaultParams, params)),
            o = this;

        var patients = [];
        result.on('result', function(msg) {
            patients.push(msg);
        });
{'fraction_non_alphanumeric': 0.1401071066724562, 'fraction_numerical': 0.11434360978433927, 'mean_word_length': 2.1252826775214837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5433748', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2133, 'n_words': 383}