Example #1
0
        public static List <DRFile> ParseDRZipGroup(IEnumerable <ZipArchiveEntry> zippedFiles)
        {
            var list = new List <DRFile>();

            //Only the CFG files contain TriggerTime, parse them first;
            zippedFiles = zippedFiles.OrderByDescending(x => x.Name.IsExtension(FileNameExtensions.CFGExtension));

            //Fallback DateTime
            var creationTime = DateTime.Now;

            foreach (var zipFileEntry in zippedFiles.Where(x => x.Name.IsPartOfDisturbanceRecording()))
            {
                Logger.Trace($"{zipFileEntry.Name}");

                //Add filedata to the DRFile;
                var fileData    = Array.Empty <byte>();
                var triggerDate = DateTime.Now;
                using (var ms = new MemoryStream())
                {
                    zipFileEntry.Open().CopyTo(ms);
                    fileData = ms.ToArray();
                }

                //Only the CFG files contain TriggerTime;
                if (zipFileEntry.Name.IsExtension(FileNameExtensions.CFGExtension))
                {
                    var cfgContents = ReadLines(zipFileEntry.Open(), Encoding.UTF8);
                    creationTime = GetTriggerDateTime(cfgContents);
                }

                //Init the DRFile;
                var drFile = new DRFile
                {
                    FileName = zipFileEntry.Name,
                    FileSize = zipFileEntry.Length,
                    //DisturbanceRecordingId = dr.Id,
                    CreationTime = creationTime,
                    FileData     = fileData
                };

                Logger.Trace($"{drFile}");

                list.Add(drFile);
            }

            return(list);
        }
Example #2
0
        public static List <DRFile> ParseDRFilesGroup(IEnumerable <FileInfo> drFileInfos)
        {
            var list = new List <DRFile>();

            //Only the CFG files contain TriggerTime, parse them first;
            drFileInfos = drFileInfos.OrderByDescending(x => x.Name.IsExtension(FileNameExtensions.CFGExtension));

            //Fallback DateTime
            var creationTime = DateTime.Now;

            foreach (var fileInfo in drFileInfos)
            {
                Logger.Trace($"{fileInfo.Name}");

                //Add filedata to the DRFile;
                var fileData = File.ReadAllBytes(fileInfo.FullName);

                //Only the CFG files contain TriggerTime;
                if (fileInfo.Name.IsExtension(FileNameExtensions.CFGExtension))
                {
                    creationTime = GetTriggerDateTime(fileInfo.FullName);
                }

                //Init the DRFile;
                var drFile = new DRFile
                {
                    FileName = fileInfo.Name,
                    FileSize = fileInfo.Length,
                    //DisturbanceRecordingId = dr.Id,
                    CreationTime = creationTime,
                    FileData     = fileData
                };

                Logger.Trace($"{drFile}");

                list.Add(drFile);
            }

            return(list);
        }
Example #3
0
        private static void AddDRtoZipFile(ZipArchive zip, DRFile drFile)
        {
            var fileInZip = zip.Entries.Where(x => x.Name.Equals(drFile.FileName)).FirstOrDefault();

            ZipArchiveEntry zipEntry;

            if (fileInZip != null)
            {
                fileInZip.Delete();
                zipEntry = zip.CreateEntry(drFile.FileName);
            }
            else
            {
                zipEntry = zip.CreateEntry(drFile.FileName);
            }

            //Get the stream of the attachment
            using (var originalFileStream = new MemoryStream(drFile.FileData))
                using (var zipEntryStream = zipEntry.Open())
                {
                    //Copy the attachment stream to the zip entry stream
                    originalFileStream.CopyTo(zipEntryStream);
                }
        }
Example #4
0
        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));
        }