211 lines
6.7 KiB
C#
211 lines
6.7 KiB
C#
///
|
|
/// 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;
|
|
|
|
#region Configuration Change Handling
|
|
|
|
public static void OnCfgChange(object sender, CfgChangeArgs args)
|
|
{
|
|
if (CfgChangeHandler == null) return;
|
|
try { CfgChangeHandler(sender, args); }
|
|
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
|
|
}
|
|
|
|
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
|
|
|
|
public override void StartChangeHandler()
|
|
{
|
|
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
|
|
{
|
|
ArchiverCfg tmpcfg = args.Cfg as ArchiverCfg;
|
|
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
|
|
{
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
|
{
|
|
writerCfg.DestinationPath = tmpcfg.DestinationPath;
|
|
writerCfg.DestinationPath2 = tmpcfg.DestinationPath2;
|
|
writerCfg.YearFolders = tmpcfg.YearFolders;
|
|
writerCfg.MonthFolders = tmpcfg.MonthFolders;
|
|
writerCfg.DayFolders = tmpcfg.DayFolders;
|
|
writerCfg.ArchiveFormat = tmpcfg.ArchiveFormat;
|
|
writerCfg.SaveGoodOnly = tmpcfg.SaveGoodOnly;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
|
|
|
|
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;
|
|
StartChangeHandler();
|
|
log.Debug(ToString());
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Returns a file name derived from a DateTime structure.
|
|
/// Creates directories on this path as a side effect.
|
|
/// </summary>
|
|
/// <param name="endTime">Date and time</param>
|
|
/// <returns>File name</returns>
|
|
string GetArchivePath(string destinationPath, DateTime startTime, DateTime endTime, int wmPosition, string wmSN)
|
|
{
|
|
string directory = destinationPath; /// Ends with "\\";
|
|
|
|
switch (writerCfg.YearFolders)
|
|
{
|
|
case YearFolders.FourDigit:
|
|
directory = string.Format("{0}{1}\\", directory, endTime.Year.ToString("D4"));
|
|
break;
|
|
case YearFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, (endTime.Year % 100).ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
switch (writerCfg.MonthFolders)
|
|
{
|
|
case MonthFolders.Name:
|
|
{
|
|
string monthStr;
|
|
switch (endTime.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, endTime.Month.ToString());
|
|
break;
|
|
case MonthFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, endTime.Month.ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
switch (writerCfg.DayFolders)
|
|
{
|
|
case DayFolders.Digit:
|
|
directory = string.Format("{0}{1}\\", directory, endTime.Day.ToString());
|
|
break;
|
|
case DayFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, endTime.Day.ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
Directory.CreateDirectory(directory);
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(writerCfg.ArchiveFormat)) throw new Exception();
|
|
return directory + string.Format(writerCfg.ArchiveFormat, startTime, endTime, wmPosition, wmSN);
|
|
}
|
|
catch
|
|
{
|
|
return string.Format("{0}{1:yyMMdd-HHmm}-{2}", directory, endTime, wmPosition);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the test cycle results into a file
|
|
/// Events:
|
|
/// Event.ResultsWritten
|
|
/// Event.Busy
|
|
/// </summary>
|
|
/// <param name="batch">Results to write into a file</param>
|
|
/// <returns>Reference to the operation</returns>
|
|
public IOperation WriteResultsOp(Results.Entities.Batch batch)
|
|
{
|
|
this.batch = batch;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
MoveFilesToArchive(batch, writerCfg.DestinationPath);
|
|
MoveFilesToArchive(batch, writerCfg.DestinationPath2);
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>Event.ResultsWritten</returns>
|
|
public Event Run()
|
|
{
|
|
return Event.ResultsWritten;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
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.StartTime, batch.EndTime, wm.WMPosition, wm.SerialNr));
|
|
}
|
|
catch
|
|
{
|
|
log.ErrorFormat("Failed to move archive files from {0}", srcFolder);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|