/// <summary> /// Download programs and/or platforms /// </summary> /// <param name="handler">An exception handler to allow processing additional items despite an exception. /// If the handler is null, processing will stop on first exception. /// The handler can throw it's own exception to halt further processing</param> /// <param name="pi">A project investigator. /// If provided, only the programs/platforms for that PI will be downloaded, /// otherwise all programs/platforms will be downloaded</param> /// <param name="daysToRetrieve">The number of days to download. If it is null, then the database is consulted /// to get the number of days since the last successful download. If the number is less than MinDays then nothing is /// downloaded (the server is not contacted at all). If the number is truncated to MaxDays. /// MinDays and MaxDays are set in the config file of the calling application.</param> public static void DownloadAll(Action<Exception, ArgosProgram, ArgosPlatform> handler = null, ProjectInvestigator pi = null, int? daysToRetrieve = null) { var db = new AnimalMovementDataContext(); foreach (var program in db.ArgosPrograms.Where(p => (pi == null || pi == p.ProjectInvestigator) && p.Active.HasValue && p.Active.Value)) { try { DownloadArgosProgram(program, daysToRetrieve); } catch (Exception ex) { if (handler == null) throw; handler(ex, program, null); } } foreach (var platform in db.ArgosPlatforms.Where(p => (pi == null || pi == p.ArgosProgram.ProjectInvestigator) && p.Active && !p.ArgosProgram.Active.HasValue)) { try { DownloadArgosPlatform(platform, daysToRetrieve); } catch (Exception ex) { if (handler == null) throw; handler(ex, null, platform); } } }
/// <summary> /// Retrieves data for an Argos Platform from the Argos Web Server /// </summary> /// <param name="platform">The Argos Platform to retrieve from the server</param> /// <param name="daysToRetrieve">The number of most recent days to retrieve from the server</param> public static void DownloadArgosPlatform(ArgosPlatform platform, int? daysToRetrieve = null) { int daysSinceLastDownload; if (daysToRetrieve.HasValue) daysSinceLastDownload = daysToRetrieve.Value; else { var database = new AnimalMovementDataContext(); var dateOfLastDownload = (from log in database.ArgosDownloads where log.PlatformId == platform.PlatformId && log.FileId != null orderby log.TimeStamp descending select log.TimeStamp).FirstOrDefault(); daysSinceLastDownload = (DateTime.Now - dateOfLastDownload).Days; } var days = Math.Min(ArgosWebSite.MaxDays, daysSinceLastDownload); if (days < ArgosWebSite.MinDays) return; var program = platform.ArgosProgram; string errors; var results = ArgosWebSite.GetCollar(program.UserName, program.Password, platform.PlatformId, days, out errors); CollarFile file = FileLoader.LoadPlatfrom(platform, days, results, errors); if (file == null) return; FileProcessor.ProcessFile(file); }
private void EnableForm() { Cursor.Current = Cursors.WaitCursor; Application.DoEvents(); // Make sure the wait cursor is shown; var db = new AnimalMovementDataContext(); try { _investigator = db.ProjectInvestigators.FirstOrDefault(pi => pi.Login == _currentUser); } catch (Exception ex) { Cursor.Current = Cursors.Default; MyProfileButton.Text = "Connection Failed"; MyProfileButton.Enabled = false; MessageBox.Show(Environment.NewLine + ex.Message + Environment.NewLine + "Connection String:" + Environment.NewLine + db.Connection.ConnectionString, "Unable to connect to the database", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Cursor.Current = Cursors.Default; MyProfileButton.Visible = _investigator != null; MyProfileButton.Text = "Project Investigator Details"; ProjectsButton.Enabled = true; InvestigatorsButton.Enabled = true; UploadButton.Enabled = true; GenerateMapButton.Enabled = true; QuickStartWizardButton.Enabled = true; }
private static Project GetProject(string projectId) { projectId = projectId.Normalize(); if (projectId.StartsWith("/p:", StringComparison.OrdinalIgnoreCase)) projectId = projectId.Substring(3); if (projectId.StartsWith("/project:", StringComparison.OrdinalIgnoreCase)) projectId = projectId.Substring(9); var database = new AnimalMovementDataContext(); return database.Projects.FirstOrDefault(p => p.ProjectId == projectId); }
private static ProjectInvestigator GetOwner(string owner) { owner = owner.Normalize(); if (owner.StartsWith("/m:", StringComparison.OrdinalIgnoreCase)) owner = owner.Substring(3); if (owner.StartsWith("/manager:", StringComparison.OrdinalIgnoreCase)) owner = owner.Substring(9); if (owner.StartsWith("/o:", StringComparison.OrdinalIgnoreCase)) owner = owner.Substring(3); if (owner.StartsWith("/owner:", StringComparison.OrdinalIgnoreCase)) owner = owner.Substring(7); var database = new AnimalMovementDataContext(); return database.ProjectInvestigators.FirstOrDefault(p => p.Login == owner); }
private static ArgosPlatform GetPlatform(string platformId) { platformId = platformId.Normalize(); if (platformId.StartsWith("/a:", StringComparison.OrdinalIgnoreCase)) platformId = platformId.Substring(3); if (platformId.StartsWith("/argos:", StringComparison.OrdinalIgnoreCase)) platformId = platformId.Substring(7); if (platformId.StartsWith("/p:", StringComparison.OrdinalIgnoreCase)) platformId = platformId.Substring(3); if (platformId.StartsWith("/platform:", StringComparison.OrdinalIgnoreCase)) platformId = platformId.Substring(10); var database = new AnimalMovementDataContext(); return database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == platformId); }
private static CollarFile GetCollarFile(string fileId) { fileId = fileId.Normalize(); if (fileId.StartsWith("/f:", StringComparison.OrdinalIgnoreCase)) fileId = fileId.Substring(3); if (fileId.StartsWith("/file:", StringComparison.OrdinalIgnoreCase)) fileId = fileId.Substring(6); int id; if (!Int32.TryParse(fileId, out id) || id < 1) return null; var database = new AnimalMovementDataContext(); return (from collar in database.CollarFiles where (collar.LookupCollarFileFormat.Code == 'H' || collar.LookupCollarFileFormat.ArgosData == 'Y') && collar.FileId == id select collar).FirstOrDefault(); }
private static void HandleException(Exception ex, CollarFile file, ArgosPlatform platform) { if (file == null) { Console.WriteLine("Processor exception handler called without a file: {0}", ex.Message); return; } if (file.LookupCollarFileFormat.ArgosData != 'Y') { Console.WriteLine("Processor exception handler called with a Non-Argos Data file({1}): {0}", ex.Message, file.FileId); return; } var db = new AnimalMovementDataContext(); db.ArgosFileProcessingIssues_Insert(file.FileId, ex.Message, platform == null ? null : platform.PlatformId, null, null, null, null); }
/// <summary> /// Find and process all of the Argos or DataLog files that need full or partial processing /// </summary> /// <param name="handler">Delegate to handle exceptions on each file, so that processing can continue. /// If the handler is null, processing will stop on first exception. /// The handler can throw it's own exception to halt further processing</param> /// <param name="pi">A project investigator. /// If provided, only files owned by or in projects managed by the given PI will be processed /// If null, then all files needing processing will be processed</param> public static void ProcessAll(Action<Exception, CollarFile, ArgosPlatform> handler = null, ProjectInvestigator pi = null) { var database = new AnimalMovementDataContext(); var views = new AnimalMovementViews(); foreach (var fileId in views.NeverProcessedArgosFiles.Select(x => x.FileId) .Concat(views.NeverProcessedDataLogFiles.Select(x => x.FileId)) .Concat(views.NeverProcessedIdfFiles.Select(x => x.FileId))) { var file = database.CollarFiles.First(f => f.FileId == fileId); if (pi != null && (file.ProjectInvestigator == null || file.ProjectInvestigator.Login != pi.Login) && (file.Project == null || file.Project.ProjectInvestigator1.Login != pi.Login)) continue; try { ProcessFile(file); } catch (Exception ex) { if (handler == null) throw; handler(ex, file, null); } } foreach (var item in views.PartiallyProcessedArgosFiles) { var file = database.CollarFiles.First(f => f.FileId == item.FileId); var platform = database.ArgosPlatforms.First(p => p.PlatformId == item.PlatformId); if (pi != null && file.ProjectInvestigator != pi && (file.Project == null || file.Project.ProjectInvestigator1 != pi)) continue; try { ProcessFile(file, platform); } catch (Exception ex) { if (handler == null) throw; handler(ex, file, platform); } } }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Investigator is in a different DataContext, get one in this DataContext if (Investigator != null) Investigator = Database.ProjectInvestigators.FirstOrDefault(pi => pi.Login == Investigator.Login); //Validate Program and Editor on load, so we can show a messagebox. }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //File is in a different DataContext, get one in this DataContext if (File != null) File = Database.CollarParameterFiles.FirstOrDefault(f => f.FileId == File.FileId); if (File == null) throw new InvalidOperationException("Collar Parameter File Details Form not provided a valid Parameter File."); var functions = new AnimalMovementFunctions(); IsEditor = functions.IsInvestigatorEditor(File.Owner, CurrentUser) ?? false; HasParameters = File.CollarParameters.Any(); ParametersDataGridView.DataSource = null; }
private static void LogIssueForFile(int fileid, string message, string platform = null, string collarMfgr = null, string collarId = null, DateTime? firstTransmission = null, DateTime? lastTransmission = null) { if (Properties.Settings.Default.LogErrorsToConsole) Console.WriteLine(message); if (Properties.Settings.Default.LogErrorsToLogFile) try { File.AppendAllText(Properties.Settings.Default.FileProcessorLogFilePath, String.Format("{0}: {1}" + Environment.NewLine, DateTime.Now, message)); } catch (Exception ex) { Debug.Print("Unable to log to file " + ex.Message); } var issue = new ArgosFileProcessingIssue { FileId = fileid, Issue = message, PlatformId = platform, CollarManufacturer = collarMfgr, CollarId = collarId, FirstTransmission = firstTransmission, LastTransmission = lastTransmission }; var database = new AnimalMovementDataContext(); database.ArgosFileProcessingIssues.InsertOnSubmit(issue); database.SubmitChanges(); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Collar and Platform are in a different DataContext, get them in this DataContext if (Collar != null) Collar = Database.Collars.FirstOrDefault( c => c.CollarManufacturer == Collar.CollarManufacturer && c.CollarId == Collar.CollarId); if (Platform != null) Platform = Database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == Platform.PlatformId); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Investigator is in a different DataContext, get one in this DataContext if (Investigator != null) Investigator = Database.ProjectInvestigators.FirstOrDefault(pi => pi.Login == Investigator.Login); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Get a CollarDeployment in this DataContext if (CollarDeployment != null) CollarDeployment = Database.CollarDeployments.FirstOrDefault(d => d.DeploymentId == CollarDeployment.DeploymentId); if (CollarDeployment == null) throw new InvalidOperationException("Collar Deployments Form not provided a valid Deployment."); var functions = new AnimalMovementFunctions(); IsEditor = (functions.IsProjectEditor(CollarDeployment.Animal.ProjectId, CurrentUser) ?? false) || (functions.IsInvestigatorEditor(CollarDeployment.Collar.Manager, CurrentUser) ?? false); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Project is in a different DataContext, get one in this DataContext if (Project != null) Project = Database.Projects.FirstOrDefault(p => p.ProjectId == Project.ProjectId); //If Project is not provided, Current user must be a PI or an project editor if (Project == null) if (!Database.Projects.Any(p => p.ProjectInvestigator == CurrentUser) && !Database.ProjectEditors.Any(e => e.Editor == CurrentUser)) throw new InvalidOperationException("Add Animal Form not provided a valid Project or you are not a PI or editor on any projects."); Functions = new AnimalMovementFunctions(); IsEditor = Project != null && (Functions.IsProjectEditor(Project.ProjectId, CurrentUser) ?? false); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Platform is in a different DataContext, get one in this DataContext if (Platform != null) Platform = Database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == Platform.PlatformId); if (Platform == null) throw new InvalidOperationException("Argos Platform Details Form not provided a valid Platform."); var functions = new AnimalMovementFunctions(); IsEditor = functions.IsInvestigatorEditor(Platform.ArgosProgram.Manager, CurrentUser) ?? false; }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; IsEditor = Database.ProjectInvestigators.Any( pi => pi.Login == CurrentUser || pi.ProjectInvestigatorAssistants.Any(a => a.Assistant == CurrentUser)); MakeLists(); }
private static ArgosProgram GetProgram(string programId) { if (programId.StartsWith("/program:")) programId = programId.Substring(9); var database = new AnimalMovementDataContext(); return database.ArgosPrograms.FirstOrDefault(p => p.ProgramId == programId); }
private static ArgosPlatform GetPlatform(string platformId) { if (platformId.StartsWith("/platform:")) platformId = platformId.Substring(10); var database = new AnimalMovementDataContext(); return database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == platformId); }
private static ProjectInvestigator GetProjectInvestigator(string pi) { var database = new AnimalMovementDataContext(); return database.ProjectInvestigators.FirstOrDefault(p => p.Login == pi); }
/// <summary> /// Retrieves data for an Argos Program from the Argos Web Server /// </summary> /// <param name="program">The Argos Program (contains Platforms) to retrieve from the server</param> /// <param name="daysToRetrieve">The number of most recent days to retrieve from the server</param> public static void DownloadArgosProgram(ArgosProgram program, int? daysToRetrieve = null) { int daysSinceLastDownload; if (daysToRetrieve.HasValue) daysSinceLastDownload = daysToRetrieve.Value; else { var database = new AnimalMovementDataContext(); var dateOfLastDownload = (from log in database.ArgosDownloads where log.ProgramId == program.ProgramId && log.FileId != null orderby log.TimeStamp descending select log.TimeStamp).FirstOrDefault(); //Problem: using Days always truncates. // If I download at 1am on day 1, and then 11pm on day 2, (1.9 days -> 1day), // I will miss any data created between 1am and 11pm on day 1. // However, rounding up may cause a lot of duplication, // (i.e. 6:01am on day1 and then 6:02am on day2, will need to download 2 days to get the extra minute) // by default we should round up to make sure that all data is obtained. However, since this is // typically called on a scheduled task at the same time (+/- download/processing time) everyday. // I will check if we are close to an even day, and then round tword that day var timespan = DateTime.Now - dateOfLastDownload; int extraDay = timespan.Hours == 0 && timespan.Minutes < 5 ? 0 : 1; daysSinceLastDownload = timespan.Days + extraDay; } var days = Math.Min(ArgosWebSite.MaxDays, daysSinceLastDownload); if (days < ArgosWebSite.MinDays) return; string errors; var results = ArgosWebSite.GetProgram(program.UserName, program.Password, program.ProgramId, days, out errors); CollarFile file = FileLoader.LoadProgram(program, days, results, errors); if (file == null) return; FileProcessor.ProcessFile(file); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Get an ArgosDeployment in this data context ArgosDeployment = Database.ArgosDeployments.FirstOrDefault(d => d.DeploymentId == DeploymentId); if (ArgosDeployment == null) throw new InvalidOperationException("Argos Deployments Form not provided a valid Argos Deployment Id."); var functions = new AnimalMovementFunctions(); IsEditor = (functions.IsInvestigatorEditor(ArgosDeployment.Collar.Manager, CurrentUser) ?? false) || (functions.IsInvestigatorEditor(ArgosDeployment.ArgosPlatform.ArgosProgram.Manager, CurrentUser) ?? false); }
private static void ProcessIdfFile(CollarFile file) { LogGeneralMessage(String.Format("Start local processing of file {0}", file.FileId)); var databaseViews = new AnimalMovementViews(); var idfLink = databaseViews.CollarParametersForIridiumDownload(file.FileId).FirstOrDefault(); if (idfLink == null) { LogGeneralMessage("No parameters found. Skipping file."); return; } var database = new AnimalMovementDataContext(); var tpfFile = database.CollarParameterFiles.FirstOrDefault(f => f.FileId == idfLink.ParameterFileId); if (tpfFile == null) { LogGeneralMessage("No collar parameter file found. Skipping file."); return; } var collar = database.Collars.FirstOrDefault( c => c.CollarManufacturer == idfLink.CollarManufacturer && (c.CollarId == idfLink.CollarId || c.CollarId == idfLink.CollarId.Substring(0,6))); if (collar == null) { LogGeneralMessage("No collar found. Skipping file."); return; } var databaseFunctions = new AnimalMovementFunctions(); //FIXME: check that this is generic to all telonics processing and not just Argos files databaseFunctions.ArgosFile_ClearProcessingResults(file.FileId); databaseFunctions.CollarFile_FixOwnerOfIdfFile((file.FileId)); var processor = new Gen4Processor(tpfFile.Contents.ToArray()); var lines = processor.ProcessIdf(file.Contents.ToArray()); var data = Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, lines) + Environment.NewLine); var filename = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.ToString("yyyyMMdd") + ".csv"; var fileLoader = new FileLoader(filename, data) { Project = null, Owner = tpfFile.ProjectInvestigator, //Must match result from databaseFunctions.CollarFile_FixOwnerOfIdfFile Collar = collar, Status = file.Status, ParentFileId = file.FileId, AllowDuplicates = false }; fileLoader.Load(); LogGeneralMessage("Finished local processing of file"); }
public static bool PiWantsEmails(string address) { var database = new AnimalMovementDataContext(); var pi = database.ProjectInvestigators.FirstOrDefault(p => p.Email == address); if (pi == null) return false; var db = new SettingsDataContext(); var setting = db.Settings.FirstOrDefault(s => s.Username == pi.Login && s.Key == WantsEmailKey); //default is to get email, unless explicitly and correctly denied return setting == null || setting.Value != false.ToString(CultureInfo.InvariantCulture); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; Investigator = Database.ProjectInvestigators.FirstOrDefault(pi => pi.Login == CurrentUser); }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Collar is in a different DataContext, get one in this DataContext if (Collar != null) Collar = Database.Collars.FirstOrDefault(c => c.CollarManufacturer == Collar.CollarManufacturer && c.CollarId == Collar.CollarId); if (Collar == null) throw new InvalidOperationException("Collar Details Form not provided a valid Collar."); DatabaseFunctions = new AnimalMovementFunctions(); DatabaseViews = new AnimalMovementViews(); IsEditor = DatabaseFunctions.IsInvestigatorEditor(Collar.Manager, CurrentUser) ?? false; }
private static ProjectInvestigator GetDefaultProjectInvestagator() { var piLogin = Properties.Settings.Default.DefaultProjectInvestigator; var database = new AnimalMovementDataContext(); var projectInvestigator = database.ProjectInvestigators.First(p => p.Login == piLogin); return projectInvestigator; }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; //Project is in a different DataContext, get one in this DataContext if (Project != null) Project = Database.Projects.FirstOrDefault(p => p.ProjectId == Project.ProjectId); if (Project == null) throw new InvalidOperationException("Project Details Form not provided a valid project."); var functions = new AnimalMovementFunctions(); IsInvestigator = Project.ProjectInvestigator.Normalize().Equals(CurrentUser.Normalize(), StringComparison.OrdinalIgnoreCase); IsEditor = functions.IsProjectEditor(Project.ProjectId, CurrentUser) ?? false; }
private void LoadDataContext() { Database = new AnimalMovementDataContext(); //Database.Log = Console.Out; }