예제 #1
0
        /// <summary>
        /// Download the log file and return
        /// the Results of the file.
        ///
        /// It is assumed the connection is already made to the ADCP
        /// serial port.
        /// </summary>
        /// <param name="dir">Director to store the log file.</param>
        /// <param name="adcp">ADCP serial connection.</param>
        /// <returns>Content of the file.</returns>
        private string DownloadLog(string dir, AdcpSerialPort adcp)
        {
            // Result
            string result = "";

            // Directory to download the file
            _isDownloadComplete = false;
            bool resultDL = adcp.XModemDownload(dir, MAINT_FILE_NAME, false, true);

            adcp.DownloadCompleteEvent += delegate(string fileName, bool goodDownload)
            {
                // Set the flag when download is complete
                _isDownloadComplete = true;
            };

            // Wait for the download to be completed
            int timeout = 100;

            while (!_isDownloadComplete)
            {
                // Sleep to wait
                Thread.Sleep(AdcpSerialPort.WAIT_STATE * 2);

                // Give a timeout so it does not get stuck
                timeout--;
                if (timeout < 0)
                {
                    break;
                }
            }

            // Check if the download could be completed
            if (resultDL)
            {
                string filePath = dir + @"\" + MAINT_FILE_NAME;
                // If the file exist read in all the data
                if (File.Exists(filePath))
                {
                    try
                    {
                        result = File.ReadAllText(filePath);
                    }
                    catch (Exception e)
                    {
                        log.Error(string.Format("Error reading maintence file {0}", filePath), e);
                    }

                    // Remove the empty data in the file
                    // The file has a complete buffer written to it
                    // The buffer will contain empty data.  This
                    // will find the end of the JSON array.
                    // Add 1 to include the end of the array character
                    result = result.Substring(0, result.IndexOf(']') + 1);

                    try
                    {
                        // Write the cleaned up data to the file
                        File.WriteAllText(filePath, result);
                    }
                    catch (Exception e)
                    {
                        log.Error(string.Format("Error writing maintence file {0}", filePath), e);
                    }
                }
            }

            // Wait for the state to change
            System.Threading.Thread.Sleep(SerialConnection.WAIT_STATE);

            return(result);
        }