/// /// Copyright (c) 2017 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Threading; using log4net; using Config.Entities; using TBF.BenchControl; using TBF.Resources; namespace TBF.BenchControl.Output.FileWriters.ImageArchiver { public class Archiver : ComponentBase, IOperation, GenericDevices.IResultsWriter { private static readonly ILog log = LogManager.GetLogger(typeof(Archiver)); public override string ToString() { return string.Format("Output.FileWriters.Basic({0})", Cfg.ToString(1)); } readonly ArchiverCfg writerCfg; Results.Entities.Batch batch; bool abort; /// Set to 'true' by Stop() operation to abort writing to the file public Archiver() {} public Archiver(Generic.IComponentCfg cfg) : base(cfg) { writerCfg = cfg as ArchiverCfg; log.Debug(this.ToString()); } /// /// Returns a file name derived from a DateTime structure. /// Creates directories on this path as a side effect. /// /// Date and time /// File name string GetArchivePath(string destinationPath, DateTime time, int wmPosition, string wmSN) { string directory = destinationPath; /// Ends with "\\"; switch (writerCfg.YearFolders) { case YearFolders.FourDigit: directory = string.Format("{0}{1}\\", directory, time.Year.ToString("D4")); break; case YearFolders.TwoDigit: directory = string.Format("{0}{1}\\", directory, (time.Year % 100).ToString("D2")); break; } switch (writerCfg.MonthFolders) { case MonthFolders.Name: { string monthStr; switch (time.Month) { default: case 1: monthStr = "January"; break; case 2: monthStr = "February"; break; case 3: monthStr = "March"; break; case 4: monthStr = "April"; break; case 5: monthStr = "May"; break; case 6: monthStr = "June"; break; case 7: monthStr = "July"; break; case 8: monthStr = "August"; break; case 9: monthStr = "September"; break; case 10: monthStr = "October"; break; case 11: monthStr = "November"; break; case 12: monthStr = "December"; break; } directory = string.Format("{0}{1}\\", directory, monthStr); break; } case MonthFolders.Digit: directory = string.Format("{0}{1}\\", directory, time.Month.ToString()); break; case MonthFolders.TwoDigit: directory = string.Format("{0}{1}\\", directory, time.Month.ToString("D2")); break; } switch (writerCfg.DayFolders) { case DayFolders.Digit: directory = string.Format("{0}{1}\\", directory, time.Day.ToString()); break; case DayFolders.TwoDigit: directory = string.Format("{0}{1}\\", directory, time.Day.ToString("D2")); break; } Directory.CreateDirectory(directory); try { if (string.IsNullOrEmpty(writerCfg.ArchiveFormat)) throw new Exception(); return directory + string.Format(writerCfg.ArchiveFormat, time, wmPosition, wmSN); } catch { return string.Format("{0}{1:yyMMdd-HHmm}-{2}", directory, time, wmPosition); } } /// /// Writes the test cycle results into a file /// Events: /// Event.ResultsWritten /// Event.Busy /// /// Results to write into a file /// Reference to the operation public IOperation WriteResultsOp(Results.Entities.Batch batch) { this.batch = batch; return this; } /// Start this operation public void Start() { MoveFilesToArchive(batch, writerCfg.DestinationPath); MoveFilesToArchive(batch, writerCfg.DestinationPath2); } /// Run this operation /// Event.ResultsWritten public Event Run() { return Event.ResultsWritten; } /// Stop this operation public void Stop() { } void MoveFilesToArchive(Results.Entities.Batch batch, string destination) { if (batch == null || batch.WaterMeters == null || batch.WaterMeters.Count <= 0) return; if (!string.IsNullOrEmpty(writerCfg.DestinationPath)) { foreach (var wm in batch.WaterMeters) { if (wm != null && (!writerCfg.SaveGoodOnly || wm.Passed)) { string srcFolder = Path.Combine(Program.ImagesDir, batch.BatchNr.ToString(), wm.WMPosition.ToString()); try { /// TODO: Copy files when files are on different disks. Directory.Move(srcFolder, GetArchivePath(writerCfg.DestinationPath, batch.EndTime, wm.WMPosition, wm.SerialNr)); } catch { log.ErrorFormat("Failed to move archive files from {0}", srcFolder); } } } } } } }