tbf/TestBenchFramework/BenchControl/Network/Camera/Roi/RoiDetectionOp.cs
2017-01-14 14:23:32 +01:00

130 lines
2.8 KiB
C#

using TBF.BenchControl.Network.Telnet;
namespace TBF.BenchControl.Network.Camera.Roi
{
public class RoiDetectionOp : IOperation
{
readonly CLP1611.Camera camera;
readonly Telnet.TelnetClient telnet;
readonly string command;
bool roiDetectionCommandSent;
string response;
bool passed;
bool failed;
Boxes.IntBox oroiX;
Boxes.IntBox oroiY;
Boxes.IntBox oroiWid;
Boxes.IntBox oroiHgh;
/// <summary>
/// Events: Event.None or Event.Error
/// </summary>
/// <param name="camera">CLP1611.Camera reference</param>
public RoiDetectionOp(Roi roi, CLP1611.Camera camera, Telnet.TelnetClient telnet, string command,
Boxes.IntBox oroiX,
Boxes.IntBox oroiY,
Boxes.IntBox oroiWid,
Boxes.IntBox oroiHgh)
{
this.camera = camera;
this.telnet = telnet;
this.command = command;
this.oroiX = oroiX;
this.oroiY = oroiY;
this.oroiWid = oroiWid;
this.oroiHgh = oroiHgh;
roiDetectionCommandSent = false;
response = null;
passed = false;
failed = false;
telnet.promptReceivedHandler += delegate(object sndr, PromptReceivedEventArgs args)
{
OnPromptReceived(sndr, args);
};
}
void OnPromptReceived(object sndr, PromptReceivedEventArgs args)
{
response = args.Response;
}
public void Start()
{
roiDetectionCommandSent = false;
}
public Event Run()
{
if (!roiDetectionCommandSent)
{
if (telnet.State == TelnetClient.TelnetState.Inactive)
{
response = null;
passed = false;
failed = false;
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.CLEAR_RESPONSE));
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, command));
roiDetectionCommandSent = true;
}
return Event.Busy;
}
else if (passed)
{
return Event.RoiDetectionPassed;
}
else if (failed)
{
return Event.RoiDetectionFailed;
}
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
{
oroiX.Val = x;
oroiY.Val = y;
oroiWid.Val = w;
oroiHgh.Val = h;
passed = true;
return Event.RoiDetectionPassed;
}
}
}
else
{
return Event.Busy;
}
}
public void Stop()
{
}
}
}