public void ProcessRequest(HttpContext context)
 {
     try
     {
         if (uint.TryParse(MinimumAcceptedMatchScore, out _minimumAcceptedMatchScore) == false)
         {
             _minimumAcceptedMatchScore = 30;
         }
         Resource newResource = FHIRUtilities.StreamToFHIR(new StreamReader(context.Request.InputStream));
         _biometics = (Media)newResource;
         // TODO send to biometric match engine. If found, add patient reference to FHIR message.
         // convert FHIR fingerprint message (_biometics) to AFIS template class
         Template probe = ConvertFHIR.FHIRToTemplate(_biometics);
         dbMinutia = new FingerPrintMatchDatabase(_databaseDirectory, _backupDatabaseDirectory, _minimumAcceptedMatchScore);
         try
         {
             dbMinutia.LateralityCode = (FHIRUtilities.LateralitySnoMedCode)probe.NoID.LateralitySnoMedCode;
             dbMinutia.CaptureSite    = (FHIRUtilities.CaptureSiteSnoMedCode)probe.NoID.CaptureSiteSnoMedCode;
         }
         catch { }
         MinutiaResult minutiaResult = dbMinutia.SearchPatients(probe);
         if (minutiaResult != null)
         {
             if (minutiaResult.NoID != null && minutiaResult.NoID.Length > 0)
             {
                 // Fingerprint found in database
                 // check if patient is already pending.
                 MongoDBWrapper dbwrapper     = new MongoDBWrapper(NoIDMongoDBAddress, SparkMongoDBAddress);
                 string         currentStatus = dbwrapper.GetCurrentStatus(minutiaResult.NoID);
                 if (currentStatus.ToLower() != "pending")
                 {
                     _responseText = minutiaResult.NoID;  //TODO: for now, it returns the localNoID.  should return a FHIR response.
                 }
                 else
                 {
                     _responseText = "pending";
                 }
                 LogUtilities.LogEvent(_responseText);
             }
             else
             {
                 _responseText = "No local database match.";
                 LogUtilities.LogEvent(_responseText);
             }
         }
         else
         {
             _responseText = "No local database match.";
             LogUtilities.LogEvent(_responseText);
         }
         dbMinutia.Dispose();
         LogUtilities.LogEvent("After dbMinutia.Dispose();");
     }
     catch (Exception ex)
     {
         _exception    = ex;
         _responseText = ex.Message;
     }
     context.Response.Write(_responseText);
     context.Response.End();
 }
Exemple #2
0
        private void OnCaptured(CaptureResult captureResult)
        {
            // Check capture quality and throw an error if poor or incomplete capture.
            if (!biometricDevice.CheckCaptureResult(captureResult))
            {
                return;
            }


            Constants.CaptureQuality quality = captureResult.Quality;
            if ((int)quality != 0)
            {
                Quality = (int)quality;
                PoorCaputure(quality, new EventArgs());
                return;
            }

            currentCapture = new SourceAFIS.Simple.Person();

            Fingerprint newFingerPrint = new Fingerprint();

            foreach (Fid.Fiv fiv in captureResult.Data.Views)
            {
                newFingerPrint.AsBitmap = ImageUtilities.CreateBitmap(fiv.RawImage, fiv.Width, fiv.Height);
            }
            currentCapture.Fingerprints.Add(newFingerPrint);
            Afis.Extract(currentCapture);
            Template tmpCurrent = newFingerPrint.GetTemplate();

            if (FingerCaptured != null)
            {
                FingerCaptured(newFingerPrint, new EventArgs());
            }
            _capturedFingerprints.Add(newFingerPrint);

            if (_minutiaCaptureController.MatchFound == false)
            {
                if (_minutiaCaptureController.AddMinutiaTemplateProbe(tmpCurrent) == true)
                {
                    // Good pair found.
                    bestFingerprint1 = newFingerPrint;
                    bestFingerprint2 = _capturedFingerprints[_minutiaCaptureController.OtherBestFingerprintItem];
                    if (NewBestMatchFound != null)
                    {
                        NewBestMatchFound(bestFingerprint2, new EventArgs());
                    }
                }
                else
                {
                    // Good fingerprint pairs not found yet.  Try again.
                    return;
                }
            }
            // Lookup minitia pair in database.
            // If found, show NoID
            // If not found, save minutia pair in database with new NoID.
            // probe is set, search database for match.
            Template Template1 = bestFingerprint1.GetTemplate();
            Template Template2 = bestFingerprint2.GetTemplate();

            MinutiaResult idFound = IdentifyFinger(tmpCurrent);

            if ((idFound != null && idFound.NoID.Length > 0))
            {
                // Fingerprint found in database
                Score = idFound.Score;
                if (DatabaseMatchFound != null)
                {
                    DatabaseMatchFound(newFingerPrint, new EventArgs());
                }
                if (NoID.Length == 0)
                {
                    NoID = idFound.NoID;
                }
                else if (NoID.Length > 0 && NoID != idFound.NoID)
                {
                    //critical error! create false assert here.
                    NoID = idFound.NoID;
                    if (DatabaseMatchError != null)
                    {
                        DatabaseMatchError(newFingerPrint, new EventArgs());
                    }
                }
            }
            else
            {
                // Not found in database
                Score = 0;
                if (DoesNotMatch != null)
                {
                    DoesNotMatch(newFingerPrint, new EventArgs());
                }

                if (NoID.Length == 0)
                {
                    Template1.NoID.LocalNoID = Guid.NewGuid().ToString();
                    Template2.NoID           = Template1.NoID;
                    dbMinutia.AddTemplate(Template1);
                    dbMinutia.AddTemplate(Template2);
                    // trigger event NewPatientAdded
                }
            }
        }
        public FHIRMessageRouter(HttpContext context)
        {
            try
            {
                if (uint.TryParse(MinimumAcceptedMatchScore, out _minimumAcceptedMatchScore) == false)
                {
                    _minimumAcceptedMatchScore = 30;
                }

                Resource newResource = FHIRUtilities.StreamToFHIR(new StreamReader(context.Request.InputStream));
                switch (newResource.TypeName.ToLower())
                {
                case "patient":
                    //if new patient. TODO: check meta for NoID status
                    SessionQueue seq = new SessionQueue();


                    _patient = (Patient)newResource;
                    string sessionID = "";
                    if (_patient.Identifier.Count > 0)
                    {
                        foreach (Identifier id in _patient.Identifier)
                        {
                            if (id.System.ToString().ToLower().Contains("session") == true)
                            {
                                sessionID = id.Value.ToString();
                            }
                        }
                    }

                    Patient ptSaved = (Patient)SendPatientToSparkServer();
                    if (ptSaved == null)
                    {
                        _responseText = "Error sending Patient FHIR message to the Spark FHIR endpoint. " + ExceptionString;
                        return;
                    }

                    string LocalNoID = ptSaved.Id.ToString();
                    //TODO: make this an atomic transaction.
                    //          delete the FHIR message from Spark if there is an error in the minutia.

                    //TODO check for existing patient and expire old messages for the patient.
                    if (_patient.Photo.Count > 0)
                    {
                        dbMinutia = new FingerPrintMatchDatabase(_databaseDirectory, _backupDatabaseDirectory, _minimumAcceptedMatchScore);
                        foreach (var minutia in _patient.Photo)
                        {
                            byte[] byteMinutias = minutia.Data;
                            Stream stream       = new MemoryStream(byteMinutias);
                            Media  media        = (Media)FHIRUtilities.StreamToFHIR(new StreamReader(stream));
                            // Save minutias for matching.
                            Template fingerprintTemplate = ConvertFHIR.FHIRToTemplate(media);
                            fingerprintTemplate.NoID.LocalNoID = LocalNoID;
                            try
                            {
                                dbMinutia.LateralityCode = (FHIRUtilities.LateralitySnoMedCode)fingerprintTemplate.NoID.LateralitySnoMedCode;
                                dbMinutia.CaptureSite    = (FHIRUtilities.CaptureSiteSnoMedCode)fingerprintTemplate.NoID.CaptureSiteSnoMedCode;
                            }
                            catch { }
                            if (dbMinutia.AddTemplate(fingerprintTemplate) == false)
                            {
                                _responseText = "Error adding a fingerprint to the match database.";
                            }
                        }
                        dbMinutia.Dispose();
                        _responseText = "Successful.";
                    }
                    break;

                case "media":
                    _biometics = (Media)newResource;
                    // TODO send to biometric match engine. If found, add patient reference to FHIR message.
                    // convert FHIR fingerprint message (_biometics) to AFIS template class
                    Template probe = ConvertFHIR.FHIRToTemplate(_biometics);
                    dbMinutia = new FingerPrintMatchDatabase(_databaseDirectory, _backupDatabaseDirectory, _minimumAcceptedMatchScore);
                    try
                    {
                        dbMinutia.LateralityCode = (FHIRUtilities.LateralitySnoMedCode)probe.NoID.LateralitySnoMedCode;
                        dbMinutia.CaptureSite    = (FHIRUtilities.CaptureSiteSnoMedCode)probe.NoID.CaptureSiteSnoMedCode;
                    }
                    catch { }
                    MinutiaResult minutiaResult = dbMinutia.SearchPatients(probe);
                    if (minutiaResult != null)
                    {
                        if (minutiaResult.NoID != null && minutiaResult.NoID.Length > 0)
                        {
                            // Fingerprint found in database
                            _responseText = minutiaResult.NoID;      //TODO: for now, it returns the localNoID.  should return a FHIR response.
                        }
                        else
                        {
                            _responseText = "No local database match.";
                        }
                    }
                    else
                    {
                        _responseText = "No local database match.";
                    }
                    dbMinutia.Dispose();

                    if (!(SendBiometicsToSparkServer()))
                    {
                        _responseText = "Error sending Biometric Media FHIR message to the Spark FHIR endpoint. " + ExceptionString;
                    }
                    break;

                default:
                    _responseText = newResource.TypeName.ToLower() + " not supported.";
                    break;
                }
            }
            catch (Exception ex)
            {
                _responseText = "Error in FHIRMessageRouter::FHIRMessageRouter: " + ex.Message;
            }
        }
Exemple #4
0
        private void OnCaptured(CaptureResult captureResult)
        {
            // Check capture quality and throw an error if bad.
            if (!biometricDevice.CheckCaptureResult(captureResult))
            {
                return;
            }

            bool  match = false;
            float score = 0;

            Afis.Threshold = 70;

            currentCapture = new SourceAFIS.Simple.Person();

            Fingerprint newFingerPrint = new Fingerprint();

            foreach (Fid.Fiv fiv in captureResult.Data.Views)
            {
                newFingerPrint.AsBitmap = ImageUtilities.CreateBitmap(fiv.RawImage, fiv.Width, fiv.Height);
            }
            currentCapture.Fingerprints.Add(newFingerPrint);
            Afis.Extract(currentCapture);
            Template tmpCurrent = newFingerPrint.GetTemplate();

            if (!(bestCapture == null))
            {
                score = Afis.Verify(currentCapture, bestCapture);
                match = (score > Afis.Threshold);
            }
            else if ((bestCapture == null) && !(previousCapture == null))
            {
                score = Afis.Verify(currentCapture, previousCapture);
                match = (score > Afis.Threshold);
            }

            if (FingerCaptured != null)
            {
                FingerCaptured(tmpCurrent, new EventArgs());
            }

            if (match)
            {
                Template tmp;
                if (!(bestCapture == null))
                {
                    tmp = bestCapture.Fingerprints[0].GetTemplate();
                    if (tmpCurrent.Minutiae.Length >= tmp.Minutiae.Length)
                    {
                        bestCapture = currentCapture;
                    }
                }
                else if (!(previousCapture == null))
                {
                    tmp = previousCapture.Fingerprints[0].GetTemplate();
                    if (tmp.Minutiae.Length > tmpCurrent.Minutiae.Length)
                    {
                        bestCapture = previousCapture;
                    }
                    else
                    {
                        bestCapture = currentCapture;
                    }
                }

                tmp = bestCapture.Fingerprints[0].GetTemplate();


                MinutiaResult idFound = IdentifyFinger(tmp);
                if ((fGoodPairFound == false) && (patientNoID.NoID.Length == 0) && (idFound.NoID.Length == 0))
                {
                    tmp.NoID.LocalNoID = "NoID" + nextID;
                    dbMinutia.AddTemplate(tmp);
                    patientNoID = idFound;

                    nextID++;
                }
                else if ((fGoodPairFound == true) && (patientNoID.NoID.Length > 0) && (idFound.NoID.Length == 0))
                {
                    if (DatabaseMatchError != null)
                    {
                        DatabaseMatchError(tmpCurrent, new EventArgs());
                    }
                }
                else
                {
                    if (DatabaseMatchFound != null)
                    {
                        DatabaseMatchFound(tmpCurrent, new EventArgs());
                    }
                }

                if (fGoodPairFound == false)
                {
                    fGoodPairFound = true;
                    if (GoodPairFound != null)
                    {
                        GoodPairFound(tmp, new EventArgs());
                    }
                }
            }
            previousCapture = currentCapture;
        }