//// project has a collection of tags and is responsible for outputting its tags' data to file // //// tag has data and attributes // //// tag manager creates the projects from a single list of tags // //// pidownloader takes datasourcetype to construct and download method returns data for each tag in each project, // start and end time passed //2. set the dates and download all of the data for each project public void testDownLoadfromPI() { PIDownloader downloader = new PIDownloader(); DateTime start = DateTime.Parse("3/1/2014"); DateTime end = DateTime.Parse("4/2/2014"); foreach (Project proj in tagMgr.ProjectList.Values) { foreach (Tag t in proj.tags) { downloader.Download(t.TagName, start, end); Console.WriteLine(t.TagName); } //after done downloading each tag output the file string fileLoc = @"P:\JesseRichards\Operations\Operations Raw Data\03 2014"; proj.OutputFile(fileLoc, start, end); Console.WriteLine("Output " + proj.name); } }
//3. output the data after each project is finished downloading //4. log whats been retreived and saved public void testOutputData(string output) { string filename = output; PIDownloader downloader = new PIDownloader(DataSourceType.Operations_PI); foreach (Project p in tagMgr.ProjectList.Values) { foreach (Tag t in p.tags) { int count = 0; while (t.Data == null & count < 100) { try { count++; t.Data = downloader.Download(t.TagName, _start, _end); } catch (Exception e) { Console.WriteLine(e.Message); Thread.Sleep(1000); } } Console.WriteLine(t.TagName); } Console.WriteLine("outputting " + p.name); //output the projects data to csv before moving to the next p.OutputFile(filename, _start, _end); } }
void Download(object param) { //pass the output file location //manage the download according to if we are only getting a single segment or if we need to loop over auto incremented segments Dictionary <DateTime, DateTime> dateset = new Dictionary <DateTime, DateTime>(); if (param.ToString() == "") { MessageBox.Show("Specify an output location before downloading"); return; } if (LoopDownload) { //create a dateset to loop through DateTime workstart = start; DateTime workend = start; DateTime originalEnd = end; DateTime originalStart = start; while (workend < originalEnd) { if (workend == start) { workend = workend.AddDays(LoopDays); } else { workend = workend.AddDays(LoopDays); workstart = workstart.AddDays(LoopDays); } dateset.Add(workstart, workend); } } else { dateset.Add(start, end); } _isDownloading = true; worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) { DownloadProgress = e.ProgressPercentage; Debug.WriteLine(DownloadProgress); }; worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled != true) { OutputFile(param); } }; worker.DoWork += delegate(object sender, DoWorkEventArgs args) { PIDownloader pi = new PIDownloader(_dst); int tagdownloadcount = 0; Dictionary <DateTime, double> TagData = new Dictionary <DateTime, double>(); TagManager tmReference = null; if (TagManagers.Count == 0) { tmReference = new TagManager(_project.tags); TagManagers.Add(tmReference); } else { foreach (TagManager tm in TagManagers) { //if no tags are found--assumed to be a new dataset;add a new tag manager tmReference = tm; //add the tags that are not already in the tag manager's tags collection foreach (Tag t in _project.tags) { if (!tm.Tags.Contains(t)) { tm.Tags.Add(t); } } } } //download the tag data for the current dataset foreach (KeyValuePair <DateTime, DateTime> dates in dateset) { if (DownloadIsCanceled) { worker.CancelAsync(); DownloadedData.Clear(); _isDownloading = false; DownloadIsCanceled = false; DownloadProgressText = string.Empty; worker.ReportProgress(0); break; } start = dates.Key; end = dates.Value; foreach (Tag tag in _project.tags) { bool runagain = true; int runcount = 0; while (runagain) { try { if (Compressed) { tmReference[tag.TagName].StringData = pi.Download(tag.TagName, start, end, Compressed); if (tag.StringData != null) { DownloadProgressText = tag.StringData.Count + " Records Downloaded: " + tag.TagName; } } else { tmReference[tag.TagName].Data = pi.Download(tag.TagName, start, end); if (tag.Data != null) { DownloadProgressText = tag.Data.Count + " Records Downloaded: " + tag.TagName; } } runagain = false; } catch (Exception e) { if (e.GetType() == typeof(OutOfMemoryException)) { DownloadProgressText = "Error Downloading " + tag.TagName + " " + e.Message + " Canceling.."; worker.CancelAsync(); } else { //Console.WriteLine(tag.TagName + " " + e.Message); DownloadProgressText = "Error Downloading " + tag.TagName + " " + e.Message + " Attempt " + runcount; runcount++; runagain = true; } } } tagdownloadcount++; int progress = (int)(double.Parse(tagdownloadcount.ToString()) / double.Parse(Project.tags.Count().ToString()) * 100); Console.WriteLine(tag.TagName + " segment 4" + tag.TagNameElement(4) + " " + progress.ToString()); worker.ReportProgress(progress, Project); } //foreach tag //add the downloaded data to a datatable DownloadedData.Clear(); DownloadedData.Add(CreateDateTimeDataTable(tmReference)); //output the file OutputFile(param); tagdownloadcount = 0; } //foreach dateset _isDownloading = false; }; worker.RunWorkerAsync(); }
void Download() { _isDownloading = true; worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) { DownloadProgress = e.ProgressPercentage; }; worker.DoWork += delegate(object sender, DoWorkEventArgs args) { PIDownloader pi = new PIDownloader(_dst); int tagdownloadcount = 0; Dictionary <DateTime, double> TagData = new Dictionary <DateTime, double>(); TagManager tmReference = null; if (TagManagers.Count == 0) { tmReference = new TagManager(_project.tags); TagManagers.Add(tmReference); } else { foreach (TagManager tm in TagManagers) { //if no tags are found--assumed to be a new dataset;add a new tag manager tmReference = tm; //add the tags that are not already in the tag manager's tags collection foreach (Tag t in _project.tags) { if (!tm.Tags.Contains(t)) { tm.Tags.Add(t); } } } } //download the tag data for the current dataset foreach (Tag tag in _project.tags) { if (DownloadIsCanceled) { worker.CancelAsync(); DownloadedData.Clear(); _isDownloading = false; DownloadIsCanceled = false; DownloadProgressText = string.Empty; worker.ReportProgress(0); break; } bool runagain = true; int runcount = 0; while (runagain) { try { tmReference[tag.TagName].Data = pi.Download(tag.TagName, start, end); DownloadProgressText = tag.Data.Count + " Records Downloaded: " + tag.TagName; runagain = false; } catch (Exception e) { Console.WriteLine(tag.TagName + " " + e.Message); DownloadProgressText = "Error Downloading " + tag.TagName + " " + e.Message; runcount++; if (runcount > 3) { runagain = false; } else { runagain = true; } } } tagdownloadcount++; int progress = (int)(double.Parse(tagdownloadcount.ToString()) / double.Parse(Project.tags.Count().ToString()) * 100); Console.WriteLine(tag.TagName + " segment 4" + tag.TagNameElement(4) + " " + progress.ToString()); worker.ReportProgress(progress, Project); } //add the downloaded data to a datatable DownloadedData.Clear(); DownloadedData.Add(CreateDateTimeDataTable(tmReference)); tagdownloadcount = 0; _isDownloading = false; }; worker.RunWorkerAsync(); }