tbf/TestBenchFramework/BenchControl/Network/Camera/CLP1611/GrabImagesOp.cs

177 lines
4.4 KiB
C#

using System;
using System.Diagnostics;
using log4net;
using Config.Entities;
using TBF.BenchControl.Network.Telnet;
using System.IO;
namespace TBF.BenchControl.Network.Camera.CLP1611
{
public class GrabImagesOp : IOperation
{
/// TODO: Implement support for grabbing multiple images, now imgFileNames.Lenght should be 1
private static readonly ILog log = LogManager.GetLogger(typeof(GrabImagesOp));
public override string ToString() { return string.Format("GrabImageOp()"); }
readonly Camera camera;
readonly Telnet.TelnetClient telnet;
readonly string[] imgFileNames;
readonly bool blackAndWhite;
readonly bool lowResolution;
readonly ImageRotation imageRotation;
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 GrabImagesOp(Camera camera, string[] imgFileNames, bool blackAndWhite, bool lowResolution, ImageRotation imageRotation)
{
/// TODO: Implement support for grabbing multiple images, now imgFileNames.Lenght should be 1
this.camera = camera;
this.telnet = camera.Telnet;
this.imgFileNames = imgFileNames;
this.blackAndWhite = blackAndWhite;
this.lowResolution = lowResolution;
this.imageRotation = imageRotation;
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;
if (File.Exists(imgFileNames[0])) File.Delete(imgFileNames[0]);
}
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
//
int rotation = 0;
if (imageRotation == ImageRotation.Deg90)
rotation = 90;
else if (imageRotation == ImageRotation.Deg180)
rotation = 180;
else if (imageRotation == ImageRotation.Deg270)
rotation = 270;
command = string.Format("clp/GrabImage {0} {1} -r {2} {3} -n 1 {4}",
blackAndWhite ? "-bmp" : "-bmp", /// 0 ... file format and quality
blackAndWhite ? "-y8" : "-rgb", /// 1 ... B&W / color
rotation.ToString(), /// 2 ... roration
lowResolution ? "-lr" : "-hr", /// 4 ... resolution
"grabbed.bmp"); /// 5 ... filename
//
// Send (or enqueue) command
//
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.CLEAR_RESPONSE));
telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, 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.TransferFileViaScp(blackAndWhite ? "grabbed.jpg" : "grabbed.bmp", 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.TransferFileViaScp(blackAndWhite ? "grabbed.jpg" : "grabbed.bmp", 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));
}
}
}
}