159 lines
3.5 KiB
C#
159 lines
3.5 KiB
C#
using TBF.BenchControl.Network.Telnet;
|
|
|
|
namespace TBF.BenchControl.Network.Camera.Roi
|
|
{
|
|
public class RoiDetectionOp : IOperation
|
|
{
|
|
readonly Roi roi;
|
|
readonly CLP1611.Camera camera;
|
|
readonly Telnet.TelnetClient telnet;
|
|
readonly string args;
|
|
|
|
string command;
|
|
bool roiDetectionCommandSent;
|
|
|
|
string response;
|
|
bool passed;
|
|
bool failed;
|
|
bool previousFailed;
|
|
|
|
Boxes.IntBox roiX;
|
|
Boxes.IntBox roiY;
|
|
Boxes.IntBox roiWid;
|
|
Boxes.IntBox roiHgh; /// Wid and Hgh are usefull when masking
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Events: Event.None or Event.Error
|
|
/// </summary>
|
|
/// <param name="camera">CLP1611.Camera reference</param>
|
|
public RoiDetectionOp(Roi roi, string args, Boxes.IntBox roiX, Boxes.IntBox roiY,
|
|
Boxes.IntBox roiWid, Boxes.IntBox roiHgh)
|
|
{
|
|
this.roi = roi;
|
|
this.camera = roi.NetCamera;
|
|
this.telnet = roi.Telnet;
|
|
this.args = args;
|
|
this.roiX = roiX;
|
|
this.roiY = roiY;
|
|
this.roiWid = roiWid;
|
|
this.roiHgh = roiHgh;
|
|
|
|
roiDetectionCommandSent = false;
|
|
|
|
telnet.promptReceivedHandler += delegate(object sndr, PromptReceivedEventArgs a)
|
|
{
|
|
OnPromptReceived(sndr, a);
|
|
};
|
|
}
|
|
|
|
void OnPromptReceived(object sndr, PromptReceivedEventArgs a)
|
|
{
|
|
response = a.Response;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
roiDetectionCommandSent = false;
|
|
}
|
|
|
|
public Event Run()
|
|
{
|
|
if (!roiDetectionCommandSent)
|
|
{
|
|
if (telnet.State == TelnetClient.TelnetState.Inactive)
|
|
{
|
|
//
|
|
// State variables
|
|
//
|
|
response = null;
|
|
passed = false;
|
|
failed = false;
|
|
previousFailed = false;
|
|
|
|
//
|
|
// Prepare command
|
|
//
|
|
string masks = string.Empty;
|
|
Roi previous = roi.PreviousRoi;
|
|
int maskLevel = 0;
|
|
while (previous != null && maskLevel++ < 3)
|
|
{
|
|
if (!previous.Detected)
|
|
{
|
|
previousFailed = true;
|
|
return Event.RoiDetectionPreviousFailed;
|
|
}
|
|
|
|
masks += string.Format(" -m {0} {1} {2} {3}", previous.RoiX.Val, previous.RoiY.Val,
|
|
previous.RoiWid.Val, previous.RoiHgh.Val);
|
|
previous = previous.PreviousRoi;
|
|
}
|
|
command = string.Format("clp/RoiDetection{0} {1}", masks, args);
|
|
|
|
//
|
|
// Send (or enqueue) command
|
|
//
|
|
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.CLEAR_RESPONSE));
|
|
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, command));
|
|
roiDetectionCommandSent = true;
|
|
}
|
|
return Event.CameraBusy;
|
|
}
|
|
else if (response != null)
|
|
{
|
|
int from = response.IndexOf('[');
|
|
int to = response.IndexOf(']');
|
|
if (!response.Contains("RESULT=0\r\n") || from < 0 || to < 0 || to < from)
|
|
{
|
|
failed = true;
|
|
return Event.RoiDetectionFailed;
|
|
}
|
|
else
|
|
{
|
|
string oroiStr = response.Substring(from + 1, to - from - 1);
|
|
string[] terms = oroiStr.Split(new char[] { ',' });
|
|
int x, y, w, h;
|
|
if (terms.Length != 5 || !int.TryParse(terms[0], out x) || !int.TryParse(terms[1], out y)
|
|
|| !int.TryParse(terms[2], out w) || !int.TryParse(terms[3], out h))
|
|
{
|
|
failed = true;
|
|
return Event.RoiDetectionFailed;
|
|
}
|
|
else
|
|
{
|
|
roiX.Val = x;
|
|
roiY.Val = y;
|
|
roiWid.Val = w;
|
|
roiHgh.Val = h;
|
|
|
|
passed = true;
|
|
return Event.RoiDetectionPassed;
|
|
}
|
|
}
|
|
}
|
|
else if (passed)
|
|
{
|
|
return Event.RoiDetectionPassed;
|
|
}
|
|
else if (failed)
|
|
{
|
|
return Event.RoiDetectionFailed;
|
|
}
|
|
else if (previousFailed)
|
|
{
|
|
return Event.RoiDetectionPreviousFailed;
|
|
}
|
|
else
|
|
{
|
|
return Event.CameraBusy;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
}
|
|
}
|
|
}
|