/// /// Copyright (c) 2017 Sensus Slovensko a.s. /// using System; using System.Diagnostics; using log4net; using Common; using Config.Entities; using TBF.Rig.Network.Telnet; using System.IO; namespace TBF.Rig.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; /// /// Events: Event.None or Event.Error /// /// CLP1611.Camera reference 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; if (camera.DebugLevel == DebugMode.Normal || camera.DebugLevel == DebugMode.DetectedOn) { 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 ((imgFileNames != null) && (imgFileNames.Length > 0) && File.Exists(imgFileNames[0])) { /// /// An image specified, (1) delete previous image, (2) check if this is a simulation /// File.Delete(imgFileNames[0]); if (camera.CameraCfg.DebugLevel == DebugMode.Simulate || camera.CameraCfg.DebugLevel == DebugMode.DetectedOff) { /// /// Camera is in simulation mode => create a simulated image /// File.Copy(string.Format("{0}\\Pictures\\sample.jpg", Program.ExecutableDir), imgFileNames[0]); } } } public Event Run() { if ((imgFileNames == null) || (imgFileNames.Length < 1) || (imgFileNames[0] == null)) { /// /// No images to be grabbed => Done /// return Event.GrabPassed; } else if (camera.CameraCfg.DebugLevel == DebugMode.Simulate || camera.CameraCfg.DebugLevel == DebugMode.DetectedOff) { /// /// Camera is in simulation mode => create a simulated image and complete /// return Event.GrabPassed; } else if (!grabImageCommandSent) { /// /// Normal operation, no command sent yet => (1) wait until telnet state = Inactive, (2) send a command /// if (camera.Running && (telnet.State == TelnetClient.TelnetState.Inactive)) { /// /// State variables /// response = null; /// /// Prepare a 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; log.WarnFormat("{0} IP={1} command={2}", camera.Name, camera.IPAddress, command); } return Event.CameraBusy; } else if (grabPassed) { /// /// Normal operation, grab passed => transfer/transferring the image /// if (transferringGrabbedImage) { /// Grab passed and image transfer is in progress return Event.GrabPassed; } else if (0 == camera.DownloadFile(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) { /// /// Normal operation, grab failed => there in no image to transfer /// return Event.GrabFailed; } else if (response != null) { /// /// Normal operation /// if (response.Contains("completed")) { grabPassed = true; /// /// Start the file transfer /// if (0 == camera.DownloadFile(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 (camera.CameraCfg.DebugLevel == DebugMode.Simulate) return; if (grabImageCommandSent) { telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, Telnet.TelnetClient.CtrlCCommand)); log.WarnFormat("{0} IP={1} command=CTRL-C", camera.Name, camera.IPAddress); } } } }