202 lines
5.5 KiB
C#
202 lines
5.5 KiB
C#
///
|
|
/// Copyright (c) 2017-2021 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using log4net;
|
|
using Common;
|
|
using Config.Entities;
|
|
using TBF.Rig.GenericDevices;
|
|
|
|
namespace TBF.Rig.Network.Camera.RoiForFixedStartCJMS11
|
|
{
|
|
public class Roi : ComponentBase, GenericDevices.IRegReaderStillCamera
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Roi));
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
|
|
|
readonly RoiCfg roiCfg;
|
|
|
|
///
|
|
/// Camera and ICamera
|
|
///
|
|
public readonly CJMS11.Camera NetCamera;
|
|
public GenericDevices.ICamera Camera { get { return NetCamera as GenericDevices.ICamera; } }
|
|
|
|
public int Position
|
|
{
|
|
get
|
|
{
|
|
int firstDigitPos = Name.IndexOfAny(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' });
|
|
int position;
|
|
return (firstDigitPos < 0) ? 0 : (int.TryParse(Name.Substring(firstDigitPos), out position) ? position : 0);
|
|
}
|
|
}
|
|
public RegisterReaderType RegisterReaderType { get { return RegisterReaderType.Manual; } }
|
|
public double PulsesPerLtr
|
|
{
|
|
get { return roiCfg.ProcParams.PulsesPerLtr; }
|
|
set { roiCfg.ProcParams.PulsesPerLtr = value; }
|
|
}
|
|
public int RoiDeltaTimeMs { get { return roiCfg.ProcParams.RoiDeltaTimeMs; } }
|
|
public int RoiPicCount { get { return roiCfg.ProcParams.RoiPicCount; } }
|
|
public double LtrsPerPulse { get { return (PulsesPerLtr <= float.Epsilon) ? 1.0 : (1 / PulsesPerLtr); } }
|
|
public string QuantityUnits { get; set; }
|
|
|
|
double beginWMState;
|
|
public double BeginWMState
|
|
{
|
|
get { return beginWMState; }
|
|
set { beginWMState = value; }
|
|
}
|
|
|
|
double endWMState;
|
|
public double EndWMState
|
|
{
|
|
get { return endWMState; }
|
|
set { endWMState = value; }
|
|
}
|
|
|
|
public double WMVolume { get { return endWMState - beginWMState; } }
|
|
|
|
public int WMPulses { get { return (int)(WMVolume / LtrsPerPulse); } }
|
|
|
|
public int WMRefPulses { get { return StateMachine.ControlBoardMain.RefPulses; ; } }
|
|
|
|
|
|
public Roi() { }
|
|
|
|
public Roi(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
roiCfg = cfg as RoiCfg;
|
|
NetCamera = TbfComponents.FindComponent(cfg.ParentName, components) as CJMS11.Camera;
|
|
log.Warn(this.ToString());
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
#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)
|
|
{
|
|
RoiCfg tmpcfg = args.Cfg as RoiCfg;
|
|
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
|
|
{
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
|
{
|
|
roiCfg.BlackAndWhite = tmpcfg.BlackAndWhite;
|
|
roiCfg.LowResolution = tmpcfg.LowResolution;
|
|
roiCfg.ImageRotation = tmpcfg.ImageRotation;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
|
|
public void Clear()
|
|
{
|
|
log.DebugFormat("{0}:Clear()", Name);
|
|
beginWMState = 0;
|
|
endWMState = 0;
|
|
}
|
|
|
|
|
|
public IOperation GrabImageOp(string imageFileName)
|
|
{
|
|
return GrabImageOp(imageFileName, roiCfg.BlackAndWhite, roiCfg.LowResolution, roiCfg.ImageRotation);
|
|
}
|
|
|
|
// Roi.cs
|
|
public IOperation GrabImageOp(
|
|
string imageFileName,
|
|
bool blackAndWhite,
|
|
bool lowResolution,
|
|
ImageRotation imageRotation)
|
|
{
|
|
if (NetCamera == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (NetCamera.CameraCfg.DebugLevel != DebugMode.Simulate && NetCamera.IPAddress == null)
|
|
{
|
|
log.WarnFormat( "Skipping GrabImageOp for ROI '{0}': camera '{1}' was not detected.", Name, NetCamera.Name);
|
|
return null;
|
|
}
|
|
|
|
int imageCount = Math.Max(1, RoiPicCount);
|
|
|
|
string[] imageNames = CreateImageFileNames(
|
|
imageFileName,
|
|
imageCount);
|
|
|
|
return NetCamera.GrabImagesOp(
|
|
imageNames,
|
|
blackAndWhite,
|
|
lowResolution,
|
|
imageRotation);
|
|
}
|
|
|
|
private static string[] CreateImageFileNames(
|
|
string originalFileName,
|
|
int imageCount)
|
|
{
|
|
if (imageCount <= 1)
|
|
{
|
|
return new[] { originalFileName };
|
|
}
|
|
|
|
string directory = Path.GetDirectoryName(originalFileName);
|
|
string baseName = Path.GetFileNameWithoutExtension(originalFileName);
|
|
string extension = Path.GetExtension(originalFileName);
|
|
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff");
|
|
|
|
var result = new string[imageCount];
|
|
|
|
// Prvý obrázok zostáva kompatibilný s dialógom.
|
|
result[0] = originalFileName;
|
|
|
|
for (int i = 1; i < imageCount; i++)
|
|
{
|
|
string fileName = string.Format(
|
|
"{0}_{1}_{2:D2}{3}",
|
|
baseName,
|
|
timestamp,
|
|
i + 1,
|
|
extension);
|
|
|
|
result[i] = string.IsNullOrEmpty(directory)
|
|
? fileName
|
|
: Path.Combine(directory, fileName);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public IValve GetValve()
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|