public static List <DisturbanceRecording> ParseZipFilesCollection(IEnumerable <FileInfo> fileInfoCollection, int deviceId) { var disturbanceRecordings = new List <DisturbanceRecording>(); //Iterate over each ZIP file. Each ZIP file should be a DisturbanceRecording; foreach (var zipFileInfo in fileInfoCollection) { using (var zipFile = ZipFile.OpenRead(zipFileInfo.FullName)) { var dr = new DisturbanceRecording { DeviceId = deviceId }; //Parse DR Group: var drFiles = ParseDRZipGroup(zipFile.Entries); //Only create a dr if it has any files on it if (drFiles.Count > 0) { dr.DRFiles.AddRange(drFiles); //When zipFile, internal names could be anything. Use zipfile instead. dr.Name = zipFileInfo.Name.GetNameWithoutExtension(); //They all (should) have the same date dr.TriggerTime = drFiles.FirstOrDefault().CreationTime; Logger.Trace($"{dr}"); disturbanceRecordings.Add(dr); } } } return(disturbanceRecordings); }
public void TestExportDRsCreateFile() { var exportPath = "exportTestFolder/"; var exportDeviceName = "testDevice"; var exportDeviceBay = "testBay"; var exportDeviceBayId = "testBayId"; var exportDateTime = new DateTime(2018, 5, 25, 14, 30, 46); var device = new Device() { Bay = exportDeviceBay, BayId = exportDeviceBayId, Name = exportDeviceName, }; var dr = new DisturbanceRecording() { TriggerTime = exportDateTime, }; var list = new List <DisturbanceRecording>() { dr, }; var filename = ExportService.GetZipFileName(device, dr); var fullFilename = PathHelper.GetOrCreateValidPath(exportPath, filename); if (File.Exists(fullFilename)) { File.Delete(fullFilename); } Assert.False(File.Exists(fullFilename)); ExportService.ExportDisturbanceRecordings(exportPath, exportDeviceName, exportDeviceBayId, exportDeviceBay, list, false); Assert.True(File.Exists(fullFilename)); File.Delete(fullFilename); Assert.False(File.Exists(fullFilename)); }
public static IEnumerable <DisturbanceRecording> ParseSingleFilesCollection( IEnumerable <FileInfo> fileInfoCollection, int deviceId) { var disturbanceRecordings = new List <DisturbanceRecording>(); //Group files by their name. //When not zipped, DR Files all have the same name. var drFileGroups = fileInfoCollection.GroupBy(x => x.Name.GetNameWithoutExtension()); //Iterate over each file group. Each file group should be a DisturbanceRecording; foreach (var drFileGroup in drFileGroups) { Logger.Trace($"Device Id: {deviceId} - drName: {drFileGroup.Key}"); var dr = new DisturbanceRecording { DeviceId = deviceId }; //Parse DR Group: var drFiles = ParseDRFilesGroup(drFileGroup); //Only create a dr if it has any files on it if (drFiles.Count > 0) { dr.DRFiles.AddRange(drFiles); //Use Group.Key as DR Name dr.Name = drFileGroup.Key; //They all (should) have the same date dr.TriggerTime = drFiles.FirstOrDefault().CreationTime; Logger.Trace($"{dr}"); disturbanceRecordings.Add(dr); } } return(disturbanceRecordings); }
public void TestGetZipFileObjects() { var exportDeviceName = "testDevice"; var exportDeviceBay = "testBay"; var exportDeviceBayId = "testBayId"; var exportDateTime = new DateTime(2018, 5, 25, 14, 30, 46); var device = new Device() { Bay = exportDeviceBay, BayId = exportDeviceBayId, Name = exportDeviceName, }; var dr = new DisturbanceRecording() { TriggerTime = exportDateTime, }; Assert.Equal($"20180525,143046000,{exportDeviceBayId},{exportDeviceName}.zip", ExportService.GetZipFileName(device, dr)); }
public static SystemContext GetContextWithData() { var options = new DbContextOptionsBuilder <SystemContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new SystemContext(options); var ied1 = new Device() { Bay = "Bay1", DeviceType = "REL670", IPAddress = "192.168.1.1", Id = 1, IsConnected = true, HasPing = true, Name = "IED1", Station = "Station1", DisturbanceRecordings = new List <DisturbanceRecording>() }; context.Devices.Add(ied1); var dr1 = new DisturbanceRecording() { Id = 1, Name = "DR1", DeviceId = 1, TriggerTime = DateTime.ParseExact("2017-12-31 13:26", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Device = ied1, }; context.DisturbanceRecordings.Add(dr1); context.SaveChanges(); return(context); }
public void TestExportDRsOverWriteFile() { var exportPath = "exportTestFolder/"; var exportDeviceName = "testDevice"; var exportDeviceBay = "testBay"; var exportDeviceBayId = "testBayId"; var exportDateTime = new DateTime(2018, 5, 25, 14, 30, 46); var device = new Device() { Bay = exportDeviceBay, BayId = exportDeviceBayId, Name = exportDeviceName, }; var drFile = new DRFile() { FileName = "testFile.cfg", FileData = new byte[] { 12, 123, 23, 254 }, }; var dr = new DisturbanceRecording() { TriggerTime = exportDateTime, DRFiles = new List <DRFile>() { drFile, }, }; var list = new List <DisturbanceRecording>() { dr, }; var filename = ExportService.GetZipFileName(device, dr); var fullFilename = PathHelper.GetOrCreateValidPath(exportPath, filename); if (File.Exists(fullFilename)) { File.Delete(fullFilename); } Assert.False(File.Exists(fullFilename)); using (var f = File.Create(fullFilename)) { } Assert.True(File.Exists(fullFilename)); var oldSize = new FileInfo(fullFilename).Length; ExportService.ExportDisturbanceRecordings(exportPath, exportDeviceName, exportDeviceBayId, exportDeviceBay, list, false); var newSize1 = new FileInfo(fullFilename).Length; Assert.Equal(oldSize, newSize1); ExportService.ExportDisturbanceRecordings(exportPath, exportDeviceName, exportDeviceBayId, exportDeviceBay, list, true); var newSize2 = new FileInfo(fullFilename).Length; Assert.NotEqual(oldSize, newSize2); Assert.True(File.Exists(fullFilename)); File.Delete(fullFilename); Assert.False(File.Exists(fullFilename)); }
public static string GetZipFileName(Device device, DisturbanceRecording dr) { return(GetZipFileName(device.Name, device.BayId, dr.TriggerTime)); }
public static DisturbanceRecording CloneDisturbanceRecording(SystemContext context, DisturbanceRecording disturbanceRecording) { return((DisturbanceRecording)context .Entry(disturbanceRecording) .CurrentValues.ToObject()); }