Example #1
0
        /// <summary>
        /// Called when camera is disconnected from PC.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CameraLost(object sender, ICImagingControl.DeviceLostEventArgs e)
        {
            try
            {
                cam.LiveStop();
            }
            catch (Exception inner)
            {
                string          errMsg = "Camera.CameraLost : Attempted to execute LiveStop after losing camera, exception thrown.";
                CameraException ex     = new CameraException(errMsg, inner);
                log.Error(ex);
            }


            isRunning           = false;
            cam.ImageAvailable -= new EventHandler <ICImagingControl.ImageAvailableEventArgs>(ImageAvailable);
            cam.DeviceLost     -= new EventHandler <ICImagingControl.DeviceLostEventArgs>(CameraLost);

            // generate exception to pass to any subscribers of the ThreadError event
            string               errMsg2 = "Camera.CameraLost : Camera disconnected from system.";
            CameraException      ex2     = new CameraException(errMsg2);
            ThreadErrorEventArgs er      = new ThreadErrorEventArgs(errMsg2, ex2, true);

            ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
        }
Example #2
0
 private void SaveQueueThreadError(object sender, ThreadErrorEventArgs e)
 {
     e.ShowErrorMsgBoxEx();
     if (e.StoppingThread)
     {
         // handle save queue shutdown accordingly
     }
 }
Example #3
0
 private void Camera1ThreadError(object sender, ThreadErrorEventArgs e)
 {
     e.ShowErrorMsgBoxEx();
     if (e.StoppingThread)
     {
         // handle camera 2 shutdown accordingly
     }
 }
Example #4
0
 private void ImageProcessor1ThreadError(object sender, ThreadErrorEventArgs e)
 {
     e.ShowErrorMsgBoxEx();
     if (e.StoppingThread)
     {
         // handle image processor 2 shutdown accordingly
     }
 }
Example #5
0
        private void Process()
        {
            // get a local copy of metadata singleton
            MetaData metadata = MetaData.Instance;

            // location of folders in saving directory structure
            string rootLocation      = metadata.SaveLocation;
            string cam1TestLocation  = "";
            string cam2TestLocation  = "";
            string cam1DebugLocation = "";
            string cam2DebugLocation = "";

            // list to keep image data in
            IPData[] cam1History            = new IPData[DEFAULT_TEST_HISTORY_SIZE];
            IPData[] cam2History            = new IPData[DEFAULT_TEST_HISTORY_SIZE];
            int      cam1HistoryInsertIndex = 0;
            int      cam2HistoryInsertIndex = 0;

            // tracks consecutive cracks in data and a flag to confirm a crack
            int  cam1ConsecutiveCrackedSampleCount = 0;
            int  cam2ConsecutiveCrackedSampleCount = 0;
            bool crackConfirmed = false;

            // timers for throttling thread, and updating save requests
            Stopwatch _threadTimer          = new Stopwatch();
            Stopwatch _debugSaveUpdateTimer = new Stopwatch();
            Stopwatch _testSaveUpdateTimer  = new Stopwatch();

            // flags to reset when timers trigger indicating corresponding data needs to be updated
            bool updateCam1DebugData = true;
            bool updateCam2DebugData = true;
            bool updateCam1TestData  = true;
            bool updateCam2TestData  = true;

            // attempt to create root directory and unique identify folder
            try
            {
                // updates the rootLocation to include the unique identifier folder generated inside function call
                rootLocation = initRootDirectory(rootLocation);
            }
            catch (Exception inner)
            {
                string errMsg = "ImageHistoryBuffer.Process : Unable to create root directory and unique identifier folder.";
                ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                log.Error(errMsg, ex);

                // shutdown thread here
                ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
            }

            // attempt to create directory structure for test data
            try
            {
                initTestDirectory(rootLocation, ref cam1TestLocation, ref cam2TestLocation);
            }
            catch (Exception inner)
            {
                string errMsg = "ImageHistoryBuffer.Process : Unable to create directory structure for test data.";
                ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                log.Error(errMsg, ex);

                // shutdown thread here
                ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
            }

            // attempt to create directory structure for debug saving if enabled
            if (metadata.EnableDebugSaving)
            {
                try
                {
                    initDebugDirectory(rootLocation, ref cam1DebugLocation, ref cam2DebugLocation);
                }
                catch (Exception inner)
                {
                    string errMsg = "ImageHistoryBuffer.Process : Unable to create directory structure for debug saving.";
                    ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                    log.Error(errMsg, ex);

                    // shutdown thread here
                    ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                    ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
                }
            }

            // start threading timers
            _threadTimer.Start();
            _debugSaveUpdateTimer.Start();
            _testSaveUpdateTimer.Start();

            while (isRunning)
            {
                // throttles the thread to prevent it from consuming 100% of processor it is running on
                int timeToSleep = threadThrottlePeriod - Convert.ToInt32(_threadTimer.ElapsedMilliseconds);
                if (timeToSleep > 0)
                {
                    Thread.Sleep(timeToSleep);
                }
                _threadTimer.Restart();

                // check if debug data needs to be updated
                if (_debugSaveUpdateTimer.ElapsedMilliseconds >= metadata.DebugSaveFrequency * 1000)
                {
                    updateCam1DebugData = true;
                    updateCam2DebugData = true;
                    _debugSaveUpdateTimer.Restart();
                }

                // check to see if test data needs to be updated
                if (_testSaveUpdateTimer.ElapsedMilliseconds >= 250)
                {
                    updateCam1TestData = true;
                    updateCam2TestData = true;
                    _testSaveUpdateTimer.Restart();
                }

                // pop everything off of savequeue
                List <QueueElement> imageElements = new List <QueueElement>();
                consumerQueue.popAll(ref imageElements);
                if (imageElements.Count > 0)
                {
                    for (int i = 0; i < imageElements.Count; i++)
                    {
                        //figure our where the iamge is coming from
                        IPData data = (IPData)imageElements[i].Data;
                        string type = imageElements[i].Type;
                        if (type.Contains("1"))
                        {
                            // see if image needs to be stored in recent history buffer 1
                            if (updateCam1TestData)
                            {
                                // requires a slot in buffer
                                cam1History[cam1HistoryInsertIndex] = data;
                                cam1HistoryInsertIndex = (cam1HistoryInsertIndex + 1) % DEFAULT_TEST_HISTORY_SIZE;
                                updateCam1TestData     = false;
                            }
                            if (metadata.EnableDebugSaving)
                            {
                                if (updateCam1DebugData)
                                {
                                    // need to save image to debug slot
                                    SaveIPData(cam1DebugLocation, ref data);
                                    updateCam1DebugData = false;
                                }
                            }
                            if (data.ContainsCrack)
                            {
                                cam1ConsecutiveCrackedSampleCount++;
                                if (cam1ConsecutiveCrackedSampleCount > CONSECUTIVE_CRACKS)
                                {
                                    crackConfirmed = true;
                                }
                            }
                            else
                            {
                                cam1ConsecutiveCrackedSampleCount = 0;
                                crackConfirmed = false;
                            }
                        }
                        else if (type.Contains("2"))
                        {
                            // see if image needs to be stored in recent history buffer 2
                            if (updateCam2TestData)
                            {
                                // requires a slot in buffer
                                cam2History[cam2HistoryInsertIndex] = data;
                                cam2HistoryInsertIndex = (cam2HistoryInsertIndex + 1) % DEFAULT_TEST_HISTORY_SIZE;
                                updateCam2TestData     = false;
                            }
                            if (metadata.EnableDebugSaving)
                            {
                                if (updateCam2DebugData)
                                {
                                    // need to save image to debug slot
                                    SaveIPData(cam2DebugLocation, ref data);
                                    updateCam2DebugData = false;
                                }
                            }
                            if (data.ContainsCrack)
                            {
                                cam2ConsecutiveCrackedSampleCount++;
                                if (cam2ConsecutiveCrackedSampleCount > CONSECUTIVE_CRACKS)
                                {
                                    crackConfirmed = true;
                                }
                            }
                            else
                            {
                                cam2ConsecutiveCrackedSampleCount = 0;
                                crackConfirmed = false;
                            }
                        }
                        else
                        {
                            // unknown data type
                            log.Info("ImageHistoryBuffer.Process : Received unknown data type.");
                        }
                        // check to see if we have confirmed a crack existing
                        if (crackConfirmed)
                        {
                            // trigger the USB Relay
                            USBRelayController usb_relay = USBRelayController.Instance;
                            if (usb_relay.IsOpen)
                            {
                                usb_relay.SetRelay0Status(true);
                                usb_relay.SetRelay1Status(true);
                            }

                            // save all images in history buffer
                            saveHistoryBuffer(cam1TestLocation, cam1History);
                            saveHistoryBuffer(cam2TestLocation, cam2History);

                            break;
                        }
                    }
                }
            }
            return;
        }