Example #1
0
        private List <BenRecord> GetFileList()
        {
            List <BenRecord> downloadList = new List <BenRecord>();
            string           dirFile      = m_tempDirectoryName + "bendir.txt";

            try
            {
                //delete the existing dir file if one exists.
                if (FileSystem.FileExists(dirFile))
                {
                    FileSystem.DeleteFile(dirFile);
                }

                //build new dir files.
                BuildBenLinkDirINI();

                ExecBenCommand();

                // build list of records to download
                if (FileSystem.FileExists(dirFile))
                {
                    TextFieldParser dirReader = FileSystem.OpenTextFieldParser(dirFile, new string[] { "\t" });
                    while (!dirReader.EndOfData)
                    {
                        string[] curRow = dirReader.ReadFields();

                        if (Convert.ToInt32(curRow[2]) < BENMAXFILESIZE)
                        {
                            BenRecord curRecord = new BenRecord(Convert.ToInt32(curRow[0]), Convert.ToDateTime(curRow[1]), Convert.ToInt32(curRow[2]));

                            if (curRecord.DateTime > DateTime.UtcNow.AddDays(-30) && curRecord.DateTime > m_lastFileDownloaded.DateTime)
                            {
                                downloadList.Add(curRecord);
                            }
                        }
                        else
                        {
                            Program.Log("File too large Error: " + m_siteName + " - " + Convert.ToString(curRow[0]), m_tempDirectoryName);
                        }
                    }
                    dirReader.Close();
                }
                else
                {
                    throw new Exception("GetFileList Error: " + m_siteName + " - dir file does not exist.");
                }
            }
            catch (Exception ex)
            {
                Program.Log("GetFileList Error: " + m_siteName + " - " + ex.ToString(), m_tempDirectoryName);
                throw new Exception("GetFileList Error: " + m_siteName + " - " + ex.ToString());
            }
            return(downloadList);
        }
Example #2
0
        private List <BenRecord> GetFileList()
        {
            List <BenRecord> downloadList = new List <BenRecord>();
            string           dirFile      = localPath + "\\bendir.txt";

            try
            {
                //delete the existing dir file if one exists.
                //if (FileSystem.FileExists(dirFile))
                //    FileSystem.DeleteFile(dirFile);

                //build new dir files.
                BuildBenLinkDirINI();

                ExecBenCommand();

                // build list of records to download
                // todo: build an algroithm for rollover of record numbers
                if (FileSystem.FileExists(dirFile))
                {
                    TextFieldParser dirReader = FileSystem.OpenTextFieldParser(dirFile, new string[] { "\t" });
                    string[]        curRow;
                    while (!dirReader.EndOfData)
                    {
                        curRow = dirReader.ReadFields();

                        if (Convert.ToInt32(curRow[2]) < BENMAXFILESIZE)
                        {
                            //Program.Log("DateTime from GetFile List for "+Convert.ToInt32(curRow[0]).ToString() +": " + Convert.ToDateTime(curRow[1]).ToString());
                            BenRecord curRecord = new BenRecord(Convert.ToInt32(curRow[0]), Convert.ToDateTime(curRow[1]), Convert.ToInt32(curRow[2]));
                            downloadList.Add(curRecord);
                        }
                        else
                        {
                            SendFileTooLargeEmailNotification("Record id: " + curRow[0]);
                        }
                    }
                    dirReader.Close();
                }
                else
                {
                    throw new Exception("GetFileList Error: " + siteName + " - dir file does not exist.");
                }
            }
            catch (Exception ex)
            {
                Program.Log("GetFileList Error: " + siteName + " - " + ex.ToString());

                throw new Exception("GetFileList Error: " + siteName + " - " + ex.ToString());
            }
            return(downloadList);
        }
Example #3
0
        public BenRunner(int deviceId, int taskId)
        {
            m_adoDataConnection = new AdoDataConnection(Program.OpenMiConfigurationFile.Settings["systemSettings"]["ConnectionString"].Value, Program.OpenMiConfigurationFile.Settings["systemSettings"]["DataProviderString"].Value);

            try
            {
                string taskSettingsString = m_adoDataConnection.ExecuteScalar <string>("Select Settings From ConnectionProfileTask WHERE ID = {0}", taskId);
                m_connectionProfileTaskSettings = taskSettingsString.ParseKeyValuePairs();
                m_deviceRecord = m_adoDataConnection.RetrieveRow("Select * From Device WHERE ID = {0}", deviceId);
                Dictionary <string, string> deviceConnection = m_deviceRecord["connectionString"].ToString().ParseKeyValuePairs();

                m_connectionProfile = m_adoDataConnection.RetrieveRow("SELECT * FROM connectionProfile WHERE ID = {0}", deviceConnection["connectionProfileID"]);
                m_folder            = m_deviceRecord["OriginalSource"].ToString();
                m_ipAddress         = deviceConnection["connectionUserName"].Split('&')[0];
                m_localPath         = m_connectionProfileTaskSettings["localPath"];
                m_siteName          = m_deviceRecord["Name"].ToString();
                m_serialNumber      = deviceConnection["connectionUserName"].Split('&')[1];

                string tempDirectory = System.IO.Path.GetTempPath();
                System.IO.Directory.CreateDirectory(tempDirectory + "\\BenDownloader\\" + m_siteName);
                m_tempDirectoryName = tempDirectory + "BenDownloader\\" + m_siteName + "\\";
                //Console.WriteLine(m_tempDirectoryName);

                m_fileWatcher = new SafeFileWatcher(m_tempDirectoryName);
                m_fileWatcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName;
                m_fileWatcher.Changed     += m_fileWatcher_Changed;
                m_fileWatcher.Created     += m_fileWatcher_Changed;

                m_activityMonitor           = new System.Timers.Timer(5000.0D);
                m_activityMonitor.Elapsed  += m_activityMonitor_Elapsed;
                m_activityMonitor.AutoReset = true;

                m_lastFileDownloaded            = GetLastDownloadedFile();
                m_lastFileDownloadedThisSession = "";
            }
            catch (Exception ex)
            {
                Program.Log(ex.ToString(), m_tempDirectoryName);
            }
        }
Example #4
0
        public BenRunner(string localPath, string siteName, string ip, string serialNumber)
        {
            m_localPath    = localPath;
            m_siteName     = siteName;
            m_ipAddress    = ip;
            m_serialNumber = serialNumber;

            Directory.CreateDirectory(m_localPath);
            m_lastFileDownloaded    = GetLastDownloadedFile();
            m_maxRetriesOnFileInUse = ConfigurationFile.Current.Settings["systemSettings"]["MaxRetriesOnFileInUse"]?.ValueAsInt32() ?? 8;

            m_tempDirectoryPath = Path.Combine(Path.GetTempPath(), "BenDownloader", m_siteName);

            if (Directory.Exists(m_tempDirectoryPath))
            {
                Directory.Delete(m_tempDirectoryPath, true);
            }

            Directory.CreateDirectory(m_tempDirectoryPath);

            m_saveRequestsOnError = true;
        }
Example #5
0
        private BenRecord GetLastDownloadedFile()
        {
            string[]  files;
            BenRecord lastFile = new BenRecord(0, DateTime.MinValue, 0);

            try
            {
                files = System.IO.Directory.GetFiles(m_localPath + "\\" + m_folder);
                foreach (string fileName in files)
                {
                    if (fileName.EndsWith("cfg"))
                    {
                        System.IO.FileInfo file = new System.IO.FileInfo(fileName);

                        try
                        {
                            if (file.LastWriteTime > lastFile.DateTime)
                            {
                                string[] dateFromFileName = System.IO.Path.GetFileNameWithoutExtension(file.Name).Split(',');
                                lastFile.DateTime = DateTime.ParseExact(dateFromFileName[0] + ',' + dateFromFileName[1], "yyMMdd,HHmmssfff", null);
                                lastFile.Id       = int.Parse(dateFromFileName[dateFromFileName.Length - 1]);
                            }
                        }
                        catch (Exception ex)
                        {
                            Program.Log("Retrieving Last Downloaded File error: " + file.Name + '-' + ex.ToString(), m_tempDirectoryName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Log("Get Last Downloaded File - Get Files \n\n" + ex.ToString(), m_tempDirectoryName);
            }


            return(lastFile);
        }
Example #6
0
        private List <BenRecord> GetFileList()
        {
            List <BenRecord> downloadList     = new List <BenRecord>();
            string           workingDirectory = Path.Combine(m_tempDirectoryPath, "bendir.req");
            string           dirFilePath      = Path.Combine(workingDirectory, BenDirFileName);
            string           logFilePath      = Path.Combine(workingDirectory, BenLogFileName);

            // Delete the existing dir file if one exists
            if (File.Exists(dirFilePath))
            {
                File.Delete(dirFilePath);
            }

            // Build new dir file
            Program.Log("Requesting list of files to be downloaded from the device...");
            BuildBenLinkDirINI(workingDirectory);

            int retries = 0;

            while (true)
            {
                ExecBenCommand(workingDirectory);

                if (File.Exists(dirFilePath))
                {
                    break;
                }

                string lastLogEntry = File.ReadAllLines(logFilePath).LastOrDefault() ?? string.Empty;
                string errorMessage = Regex.Match(lastLogEntry, "ERROR.*", RegexOptions.IgnoreCase).Value;
                bool   fileInUse    = Regex.IsMatch(errorMessage, "FILE ALREADY IN USE", RegexOptions.IgnoreCase);

                if (!fileInUse)
                {
                    throw new Exception($"Error received from benlink: {errorMessage}");
                }

                if (m_maxRetriesOnFileInUse >= 0 && retries++ > m_maxRetriesOnFileInUse)
                {
                    m_saveRequestsOnError = false;
                    throw new Exception("Exceeded maximum number of benlink retries");
                }

                Program.Log($"Encountered FILE IN USE error; initiating retry attempt {retries}");
            }

            // Build list of records to download
            using (TextReader dirFileReader = File.OpenText(dirFilePath))
            {
                string line;

                while ((line = dirFileReader.ReadLine()) != null)
                {
                    string[] curRow = line.Split('\t');

                    if (Convert.ToInt32(curRow[2]) >= BENMAXFILESIZE)
                    {
                        Program.Log("File too large Error: " + m_siteName + " - " + Convert.ToString(curRow[0]), true);
                        continue;
                    }

                    BenRecord record = new BenRecord(Convert.ToInt32(curRow[0]), Convert.ToDateTime(curRow[1]), Convert.ToInt32(curRow[2]), Get232Fn(Convert.ToDateTime(curRow[1]), curRow[0].Trim()));

                    if (record.DateTime > DateTime.UtcNow.AddDays(-30) && record.DateTime > m_lastFileDownloaded.DateTime && !File.Exists(Path.Combine(m_localPath, record.Name)))
                    {
                        downloadList.Add(record);
                    }
                }
            }

            return(downloadList);
        }