155 lines
3.4 KiB
C#
155 lines
3.4 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using log4net;
|
|
using TBF.BenchControl.Network.Telnet;
|
|
|
|
namespace TBF.BenchControl.Network.Camera.CLP1611
|
|
{
|
|
public class GrabImageOp : IOperation
|
|
{
|
|
/// TODO: Implement support for grabbing multiple images, now imgFileNames.Lenght should be 1
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(GrabImageOp));
|
|
public override string ToString() { return string.Format("GrabImageOp()"); }
|
|
|
|
readonly Camera camera;
|
|
readonly Telnet.TelnetClient telnet;
|
|
readonly string[] imgFileNames;
|
|
|
|
string command;
|
|
bool grabImageCommandSent;
|
|
string response;
|
|
bool grabPassed;
|
|
bool grabFailed;
|
|
bool transferringGrabbedImage;
|
|
|
|
|
|
/// <summary>
|
|
/// Events: Event.None or Event.Error
|
|
/// </summary>
|
|
/// <param name="camera">CLP1611.Camera reference</param>
|
|
public GrabImageOp(Camera camera, string[] imgFileNames)
|
|
{
|
|
/// TODO: Implement support for grabbing multiple images, now imgFileNames.Lenght should be 1
|
|
|
|
this.camera = camera;
|
|
this.telnet = camera.Telnet;
|
|
this.imgFileNames = imgFileNames;
|
|
|
|
grabImageCommandSent = false;
|
|
transferringGrabbedImage = false;
|
|
|
|
telnet.promptReceivedHandler += delegate(object sndr, PromptReceivedEventArgs a)
|
|
{
|
|
OnPromptReceived(sndr, a);
|
|
};
|
|
}
|
|
|
|
void OnPromptReceived(object sndr, PromptReceivedEventArgs a)
|
|
{
|
|
response = a.Response;
|
|
/// TODO
|
|
}
|
|
|
|
|
|
public void Start()
|
|
{
|
|
grabImageCommandSent = false;
|
|
transferringGrabbedImage = false;
|
|
}
|
|
|
|
public Event Run()
|
|
{
|
|
if (imgFileNames.Length < 1 || imgFileNames[0] == null)
|
|
{
|
|
return Event.GrabPassed;
|
|
}
|
|
else if (!grabImageCommandSent)
|
|
{
|
|
if (telnet.State == TelnetClient.TelnetState.Inactive)
|
|
{
|
|
//
|
|
// State variables
|
|
//
|
|
response = null;
|
|
|
|
//
|
|
// Prepare command
|
|
//
|
|
command = string.Format("clp/GrabImage -jpg -q 95 -n 1 grabbed.jpg");
|
|
|
|
//
|
|
// Send (or enqueue) command
|
|
//
|
|
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.CLEAR_RESPONSE));
|
|
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_STRING, command));
|
|
grabImageCommandSent = true;
|
|
}
|
|
return Event.CameraBusy;
|
|
}
|
|
else if (grabPassed)
|
|
{
|
|
if (transferringGrabbedImage)
|
|
{
|
|
/// Grab passed and image transfer is in progress
|
|
return Event.GrabPassed;
|
|
}
|
|
else if (0 == camera.WscpTransferFile("grabbed.jpg", imgFileNames[0]))
|
|
{
|
|
/// File transfer successfully started
|
|
transferringGrabbedImage = true;
|
|
return Event.GrabPassed;
|
|
}
|
|
else
|
|
{
|
|
/// Wait until the previous image transfer completes
|
|
return Event.CameraBusy;
|
|
}
|
|
}
|
|
else if (grabFailed)
|
|
{
|
|
/// Grab failed, there in no image to transfer
|
|
return Event.GrabFailed;
|
|
}
|
|
else if (response != null)
|
|
{
|
|
if (response.Contains("completed"))
|
|
{
|
|
grabPassed = true;
|
|
|
|
///
|
|
/// Start the file transfer
|
|
///
|
|
if (0 == camera.WscpTransferFile("grabbed.jpg", imgFileNames[0]))
|
|
{
|
|
/// File transfer successfully started
|
|
transferringGrabbedImage = true;
|
|
return Event.GrabPassed;
|
|
}
|
|
else
|
|
{
|
|
return Event.CameraBusy; /// File transfer not started yet
|
|
}
|
|
}
|
|
else
|
|
{
|
|
grabFailed = true;
|
|
return Event.GrabFailed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return Event.CameraBusy;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (grabImageCommandSent)
|
|
{
|
|
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, Telnet.TelnetClient.CtrlCCommand));
|
|
}
|
|
}
|
|
}
|
|
}
|