using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Windows; using DataPROWin7.Common; using DTS.Common.Base; using DTS.Serialization; using DTS.Common.Utilities.Logging; using File = System.IO.File; using System.Xml.Linq; using DTS.Common.SharedResource.Strings; using DTS.Common.DataModel; using DTS.Common.Utils; using DTS.Common.DataModel.Classes.TestTemplate; namespace DataPROWin7.DataModel { public class DataFiles : BasePropertyChanged { public DataFiles(string expandCollapse, string testName, string id, string allOrROI, string lab, string customer, DateTime dateCreated, string description, string numberOfChannels, string testEngineer, bool isTSRAIR, string roiSuffix = "") { ExpandCollapse = expandCollapse; TestName = testName; TestId = id; AllOrROI = allOrROI; Lab = lab; Customer = customer; DateCreated = dateCreated; Description = description; NumberOfChannels = numberOfChannels; TestEngineer = testEngineer; ROISuffix = roiSuffix; IsTSRAIR = isTSRAIR; } public string LongString { get; set; } public bool TestSelected { get; set; } public string ExpandCollapse { get; set; } public string TestId { get; set; } public string TestName { get; set; } public string AllOrROI { get; set; } public string Lab { get; set; } public string Customer { get; set; } public DateTime DateCreated { get; set; } public string Description { get; set; } public string NumberOfChannels { get; set; } public string TestEngineer { get; set; } public string ROISuffix { get; set; } /// /// location of DTS file if known (populated in export tab) /// 18525 The data set is in Data folder but not listed on Export Data tab /// public string DTSFile { get; set; } public bool IsTSRAIR { get; set; } = false; } public class DataFilesList : BasePropertyChanged { /// /// returns all folders that have a dts and .chn files, recursively travels down subfolders /// private string[] GetFoldersWithData(string folder) { var folders = new List(); var subFolders = Directory.GetDirectories(folder); foreach (var subFolder in subFolders) { folders.AddRange(GetFoldersWithData(subFolder)); } var files = Directory.GetFiles(folder); if (Array.Exists(files, f => f.ToLower().Contains(".dts")) && Array.Exists(files, f => f.ToLower().Contains(".chn"))) { folders.Add(folder); } return folders.ToArray(); } public string DownloadFolder = string.Empty; /// /// parses out the test information out of a DTS file without loading the entire file into memory with DOM xml /// we could use a SAX parser instead /// 23342 Export and View Tiles very slow [APG-ATEC] /// /// /// /// /// /// /// /// /// private bool GetTestInfo(string dtsFilePath, out string lab, out string customer, out string description, out string testEngineer, out DateTime dt, out string name) { //initialize all returns, then fill them in if the info is in the file lab = string.Empty; customer = string.Empty; description = string.Empty; testEngineer = string.Empty; dt = DateTime.MinValue; name = string.Empty; try { var sFile = DTS.Serialization.SliceRaw.File.Reader.ReadTestStringsFromFile(dtsFilePath); var testInfoLine = Array.Find(sFile, f => f.Contains(" tag is incomplete, so add an end tag just so xml doesn't complain testInfoLine = $"{testInfoLine}"; var testXml = XElement.Parse(testInfoLine); description = testXml.Attribute("Description").Value; name = testXml.Attribute("Id").Value; var teLine = Array.Find(sFile, f => f.Contains("")); if (null != teLine) { var teXml = XElement.Parse(teLine); testEngineer = teXml.Value; } var labLine = Array.Find(sFile, f => f.Contains("")); if (null != labLine) { var labXml = XElement.Parse(labLine); lab = labXml.Value; } var custLine = Array.Find(sFile, f => f.Contains("")); if (null != custLine) { var custXml = XElement.Parse(custLine); customer = custXml.Value; } var timeStampLine = Array.Find(sFile, f => f.Contains("")); if (null != timeStampLine) { var timeStampXml = XElement.Parse(timeStampLine); dt = DateTime.Parse(timeStampXml.Value, CultureInfo.InvariantCulture); } return true; } catch (Exception ex) { APILogger.Log(ex); return false; } } public DataFiles[] GetAllFiles(string testName) { //FB 32926 No need to check for instance since always would have a value //var current = (App) Application.Current; //if (current == null) { return new DataFiles[0]; } var dataFiles = new List(); try { var testNameFolder = Path.GetFullPath(DataModelSettings.DownloadFolder); DownloadFolder = testNameFolder; testNameFolder = Path.Combine(testNameFolder, testName); if (Directory.Exists(testNameFolder)) { var foldersWithData = GetFoldersWithData(testNameFolder); foreach (string folder in foldersWithData) { var dtsFiles = Directory.GetFiles(folder, "*.dts"); if (1 != dtsFiles.Length) { continue; } var dtsFile = dtsFiles[0]; if (!GetTestInfo(dtsFiles[0], out var lab, out var customer, out var description, out var testEngineer, out var dt, out var name)) { continue; } var isTSRAIR = dtsFiles[0].Contains(TSRAIRGoTestSetup.TEST_NAME); var numberOfChannels = string.Empty; var directoryInfo = new FileInfo(dtsFile).Directory; if (directoryInfo != null) { var channels = Directory.GetFiles(directoryInfo.FullName, "*.chn"); numberOfChannels = channels.Length.ToString(); } var subFolders = folder.Split(Path.DirectorySeparatorChar).ToList(); var binaryFolderIndex = subFolders.IndexOf("Binary"); var allOrROI = StringResources.Table_NA; //as long as we found the binary folder, use the next folder to determine All or ROI if (binaryFolderIndex > 0) { allOrROI = subFolders[binaryFolderIndex + 1]; } var roiSuffix = TestUtils.ParseROISuffix(folder); var dtsFileInfo = new FileInfo(dtsFile); var testId = dtsFileInfo.Name.Replace(dtsFileInfo.Extension, ""); var df = new DataFiles(string.Empty, name, testId, allOrROI, lab, customer, dt, description, numberOfChannels, testEngineer, isTSRAIR, roiSuffix) { DTSFile = dtsFile }; dataFiles.Add(df); } } } catch (Exception ex) { APILogger.Log("Failed to retrieve Data Files", ex); } return dataFiles.ToArray(); } public DataFiles[] Contract(DataFiles df) { _dataFiles.RemoveAll(item => (item.TestName == df.TestName) && (item.ExpandCollapse != "-")); _dataFiles.Find(item => item.TestName == df.TestName).ExpandCollapse = "+"; return _dataFiles.ToArray(); } public DataFiles[] GetAllDataFiles() { var dataFiles = new List(); try { var dataFolder = Path.GetFullPath(DataModelSettings.DownloadFolder); if (Directory.Exists(dataFolder)) { var testFolders = Directory.GetDirectories(dataFolder); foreach (var testFolder in testFolders) { var testName = new DirectoryInfo(testFolder).Name; var testIdFolders = Directory.GetDirectories(testFolder); var fileCount = 0; foreach (var testIdFolder in testIdFolders) { if (!Directory.Exists(Path.Combine(testIdFolder, "Binary"))) continue; var binaryFolder = Path.Combine(testIdFolder, "Binary"); var testId = new DirectoryInfo(testIdFolder).Name; fileCount += GetFileCount(binaryFolder, testId); } if (fileCount <= 0) continue; var isTSRAIR = testFolder.Contains(TSRAIRGoTestSetup.TEST_NAME); var df = new DataFiles("+", testName, string.Empty, string.Empty, string.Empty, string.Empty, new DateTime(), string.Empty, string.Empty, string.Empty, isTSRAIR); dataFiles.Add(df); } } } catch (Exception ex) { APILogger.Log("Failed to retrieve Data Files", ex); } return dataFiles.ToArray(); } /// /// gets the count of tests data sets in the given binary folder /// this means it will traverse down the given folder looking for dts and chn files /// matching a pattern /// private int GetFileCount(string binaryFolder, string testId) { var count = 0; var subFolders = Directory.GetDirectories(binaryFolder); foreach (var subfolder in subFolders) { count += GetFileCount(subfolder, testId); } //different tests seem to name the dts file differently //lets not care about the name as long as there's only one dts file //and there's chn files which follow the dts file name var dtsFiles = Directory.GetFiles(binaryFolder, "*.dts"); if (1 == dtsFiles.Length) { var fi = new FileInfo(dtsFiles[0]); var testName = fi.Name.Replace(fi.Extension, ""); //make sure there's at least one file if (Directory.GetFiles(binaryFolder, $"{testName}*.*.chn").Any()) { count++; } } return count; } private List _dataFiles; public DataFiles[] DataFiles { get { _dataFiles = new List(); _dataFiles.AddRange(GetAllDataFiles()); return _dataFiles.ToArray(); } set => SetProperty(ref _dataFiles, new List(value), "DataFiles"); } private static DataFilesList _dataFileList; public static DataFilesList DataFileList => _dataFileList ?? (_dataFileList = new DataFilesList()); } }