From 7d15073bb59823744e8df0fbc82d44af1ca0e2fb Mon Sep 17 00:00:00 2001 From: Michal Buzik Date: Thu, 14 Nov 2024 13:05:33 +0100 Subject: [PATCH] improve logs and added try, catch blocks --- .../Network/Camera/CLP1611/GrabImagesOp.cs | 355 ++++++++++-------- TBF/Rig/Network/Camera/Roi/RoiDetectionOp.cs | 152 ++++---- 2 files changed, 297 insertions(+), 210 deletions(-) diff --git a/TBF/Rig/Network/Camera/CLP1611/GrabImagesOp.cs b/TBF/Rig/Network/Camera/CLP1611/GrabImagesOp.cs index 1921ab114..d6f1c3cdf 100644 --- a/TBF/Rig/Network/Camera/CLP1611/GrabImagesOp.cs +++ b/TBF/Rig/Network/Camera/CLP1611/GrabImagesOp.cs @@ -1,6 +1,7 @@ /// /// Copyright (c) 2017 Sensus Slovensko a.s. /// + using System; using System.Diagnostics; using log4net; @@ -11,45 +12,49 @@ 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()"); } + 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)); - readonly Camera camera; - readonly Telnet.TelnetClient telnet; - readonly string[] imgFileNames; - readonly bool blackAndWhite; - readonly bool lowResolution; - readonly ImageRotation imageRotation; + public override string ToString() + { + return string.Format("GrabImageOp()"); + } - string command; - bool grabImageCommandSent; - string response; - bool grabPassed; - bool grabFailed; - bool transferringGrabbedImage; + 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 + /// + /// 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; + this.camera = camera; + this.telnet = camera.Telnet; + this.imgFileNames = imgFileNames; + this.blackAndWhite = blackAndWhite; + this.lowResolution = lowResolution; + this.imageRotation = imageRotation; - grabImageCommandSent = false; - transferringGrabbedImage = false; + grabImageCommandSent = false; + transferringGrabbedImage = false; if (camera.DebugLevel == DebugMode.Normal || camera.DebugLevel == DebugMode.DetectedOn) { @@ -58,46 +63,62 @@ namespace TBF.Rig.Network.Camera.CLP1611 OnPromptReceived(sndr, a); }; } - } + } - void OnPromptReceived(object sndr, PromptReceivedEventArgs a) - { - response = a.Response; - /// TODO - } + void OnPromptReceived(object sndr, PromptReceivedEventArgs a) + { + response = a.Response; + /// TODO + } - public void Start() - { - grabImageCommandSent = false; - transferringGrabbedImage = false; + 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]); + try + { + File.Delete(imgFileNames[0]); + } + catch (Exception e) + { + log.ErrorFormat("Celan up failed! Delete file atempt {0} failed! Detail: {1}", imgFileNames[0], e.StackTrace); + } - if (camera.CameraCfg.DebugLevel == DebugMode.Simulate || camera.CameraCfg.DebugLevel == DebugMode.DetectedOff) + 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]); + + try + { + File.Copy(string.Format("{0}\\Pictures\\sample.jpg", Program.ExecutableDir), imgFileNames[0]); + } + catch (Exception e) + { + log.ErrorFormat("Simulation/DetectedOff file copy to {0} failed! Deatil: {1}", imgFileNames[0], e.StackTrace); + } } } - } + } - public Event Run() - { + 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) { @@ -107,118 +128,158 @@ namespace TBF.Rig.Network.Camera.CLP1611 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; + try + { + 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; + /// + /// 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 + 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); + // + // 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) - { + catch (Exception e) + { + if (camera == null) + { + log.Error("Failed stand up from camera inactive status! Detail: " + e.StackTrace); + } + else + { + log.ErrorFormat("Failed stand up from camera inactive status! {0} IP={1} command={2} ! Detail: {3}", camera.Name, camera.IPAddress, command, e.StackTrace); + } + return Event.CameraBusy; + } + + 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) - { + try + { + 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; + } + } + catch (Exception e) + { + log.ErrorFormat("Somtehing get wrong with DownloadFile - grabbed.jpg or .bmp to {0}! Detials: {1}",imgFileNames[0], e.StackTrace); + return Event.CameraBusy; + } + } + else if (grabFailed) + { /// /// Normal operation, grab failed => there in no image to transfer /// - return Event.GrabFailed; - } - else if (response != null) - { + return Event.GrabFailed; + } + else if (response != null) + { /// /// Normal operation /// if (response.Contains("completed")) - { - grabPassed = true; + { + 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); + try + { + /// + /// 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 + } + } + catch (Exception e) + { + log.ErrorFormat("Sometihing get wrong! Detail:{0}", e.StackTrace); + return Event.CameraBusy; + } + } + else + { + grabFailed = true; + return Event.GrabFailed; + } } - } - } -} + else + { + return Event.CameraBusy; + } + } + + public void Stop() + { + if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) return; + + if (grabImageCommandSent) + { + try + { + telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, Telnet.TelnetClient.CtrlCCommand)); + log.WarnFormat("{0} IP={1} command=CTRL-C", camera.Name, camera.IPAddress); + } + catch (Exception e) + { + log.ErrorFormat("Failed {0} IP={1} command=CTRL-C! Detail: {2}", camera.Name, camera.IPAddress, e.StackTrace); + } + } + } + } +} \ No newline at end of file diff --git a/TBF/Rig/Network/Camera/Roi/RoiDetectionOp.cs b/TBF/Rig/Network/Camera/Roi/RoiDetectionOp.cs index f5d23540e..c05fdc180 100644 --- a/TBF/Rig/Network/Camera/Roi/RoiDetectionOp.cs +++ b/TBF/Rig/Network/Camera/Roi/RoiDetectionOp.cs @@ -92,52 +92,69 @@ namespace TBF.Rig.Network.Camera.Roi string masks = string.Empty; Roi previous = roi.PreviousRoi; int maskLevel = 0; - while (previous != null && maskLevel++ < 3) + + try { - if (!previous.Detected) + while (previous != null && maskLevel++ < 3) { - previousFailed = true; - return Event.RoiDetectionPreviousFailed; + 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; } - 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}{2}{3}{4} {5}", - masks, - string.Format(roiCfg.LoadImages ? " -l {0}" : " -n {0}", roiCfg.DetectionImagesCount), - roiCfg.SaveImages ? " -s -jpg" : "", - string.Format(" -p1 {0}", roiCfg.DetectionPeriod1), - string.Format(" -p2 {0}", roiCfg.DetectionPeriod2), - roiCfg.RoiParams); + command = string.Format("clp/RoiDetection{0}{1}{2}{3}{4} {5}", + masks, + string.Format(roiCfg.LoadImages ? " -l {0}" : " -n {0}", roiCfg.DetectionImagesCount), + roiCfg.SaveImages ? " -s -jpg" : "", + string.Format(" -p1 {0}", roiCfg.DetectionPeriod1), + string.Format(" -p2 {0}", roiCfg.DetectionPeriod2), + roiCfg.RoiParams); - // - // 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; - log.WarnFormat("{0}/{1} command={2}", roiCfg.Name, camera.Name, command); - } + // + // 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; + log.WarnFormat("{0}/{1} command={2}", roiCfg.Name, camera.Name, command); + } + catch (Exception e) + { + log.ErrorFormat("Something wrong in start! Command: {0} ! Details: {1}", command, e.StackTrace); + } + } return Event.CameraBusy; } else if (passed) { - if (transferringDetectedImage) + try { - /// ROI detection passed and image transfer is in progress - return Event.RoiDetectionPassed; + if (transferringDetectedImage) + { + /// ROI detection passed and image transfer is in progress + return Event.RoiDetectionPassed; + } + else if (0 == camera.DownloadFile("detected.jpg", imgFileNamePrefix + "detected.jpg")) + { + /// File transfer successfully started + transferringDetectedImage = true; + return Event.RoiDetectionPassed; + } + else + { + /// Wait until the previous image transfer completes + return Event.CameraBusy; + } } - else if (0 == camera.DownloadFile("detected.jpg", imgFileNamePrefix + "detected.jpg")) + catch (Exception e) { - /// File transfer successfully started - transferringDetectedImage = true; - return Event.RoiDetectionPassed; - } - else - { - /// Wait until the previous image transfer completes + log.ErrorFormat("Someting wrong with DownloadFile(detected.jpg), imgFileNamePrefix: {0}! Details: {1}", imgFileNamePrefix, e.StackTrace); return Event.CameraBusy; } } @@ -152,49 +169,58 @@ namespace TBF.Rig.Network.Camera.Roi } 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) + try { - roi.ClearRoi(); - failed = true; - log.WarnFormat("{0}/{1} Detection failed: {2}", roiCfg.Name, camera.Name, response); - 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)) + int from = response.IndexOf('['); + int to = response.IndexOf(']'); + if (!response.Contains("RESULT=0\r\n") || from < 0 || to < 0 || to < from) { roi.ClearRoi(); failed = true; - log.WarnFormat("{0}/{1} Cannot parse response: {2}", roiCfg.Name, camera.Name, response); - return Event.RoiDetectionFailed; + log.WarnFormat("{0}/{1} Detection failed: {2}", roiCfg.Name, camera.Name, response); + return Event.RoiDetectionFailed; } else { - roi.SetRoi(x, y, w, h); - log.WarnFormat("{0}/{1} Detection PASSED: x={2} y={3} w={4} h={5}", roiCfg.Name, camera.Name, x, y, w, h); - passed = true; - - /// - /// Start the file transfer - /// - if (0 == camera.DownloadFile("detected.jpg", imgFileNamePrefix + "detected.jpg")) + 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)) { - /// File transfer successfully started - transferringDetectedImage = true; - return Event.RoiDetectionPassed; + roi.ClearRoi(); + failed = true; + log.WarnFormat("{0}/{1} Cannot parse response: {2}", roiCfg.Name, camera.Name, response); + return Event.RoiDetectionFailed; } else { - return Event.CameraBusy; /// File transfer not started yet + roi.SetRoi(x, y, w, h); + log.WarnFormat("{0}/{1} Detection PASSED: x={2} y={3} w={4} h={5}", roiCfg.Name, + camera.Name, x, y, w, h); + passed = true; + + /// + /// Start the file transfer + /// + if (0 == camera.DownloadFile("detected.jpg", imgFileNamePrefix + "detected.jpg")) + { + /// File transfer successfully started + transferringDetectedImage = true; + return Event.RoiDetectionPassed; + } + else + { + return Event.CameraBusy; /// File transfer not started yet + } } } } + catch (Exception e) + { + log.ErrorFormat("Error in taken results! Details: {0}", e.StackTrace); + return Event.CameraBusy; + } } else {