private ComputeDeviceState QueryDeviceState(ComputeDeviceInfo currentDeviceInfo, int port) { bool validPort = currentDeviceInfo.PortList.Any(p => p == port); if (!validPort) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] QueryDeviceState(devId={0}, port={1}) Port does not exists", currentDeviceInfo.ComputeDeviceId, port); Console.ResetColor(); return(null); } if (computeDeviceStateMap.ContainsKey(currentDeviceInfo)) { if (computeDeviceStateMap[currentDeviceInfo].ContainsKey(port)) { return(computeDeviceStateMap[currentDeviceInfo][port]); } else { return(computeDeviceStateMap[currentDeviceInfo][port] = new ComputeDeviceState()); } } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] QueryDeviceState(devId={0}, port={1}) Unable to fetch data", currentDeviceInfo.ComputeDeviceId, port); Console.ResetColor(); return(null); }
public string _GetShortFormattedComputeDeviceState(ComputeDeviceState state) { string result = "("; foreach (var algo in state.AssignedAlgorithmDescriptionMap) { if (algo.Value) { result += "Algo=" + algo.Key.AlgorithmTypeId + "," + algo.Key.Name + ";"; } } foreach (var preset in state.AssignedPresetDescriptionMap) { if (preset.Value) { result += "PresetId&Name=" + preset.Key.Id + "," + preset.Key.Name + ";"; } } foreach (var camera in state.OpenCameraMap) { if (camera.Value) { result += "Camera=" + camera.Key.Id + "," + camera.Key.Label + ";"; } } result += ")"; return(result); }
// Private ExecutePresetHelper private string ExecutePresetHelper(string presetId, CameraConfiguration cameraConfiguration, string postScript) { int RunCompatibility = 0; bool targetCameraOpen = false; bool targetAlgorithmActive = false; PresetDescription presetConfiguration = utility.QueryPresetById(presetId, brokerHubHost, brokerHubPort); if (presetConfiguration == null) { throw new Exception("Preset not found: " + presetId); } ComputeDeviceInfo computeDevice = utility.QueryComputeDeviceById(presetConfiguration.ComputeDeviceId); int port = presetConfiguration.Port; ComputeDeviceState currentState = QueryDeviceState(computeDevice, port); logger.LogComputeDeviceStateMap(computeDeviceStateMap, string.Format("QueryDeviceState({0}, {1}) = {2}", computeDevice.ComputeDeviceId, port.ToString(), logger._GetShortFormattedComputeDeviceState(currentState))); if (currentState == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] ExecutePreset(presetId={0}, cameraId={1}) Failure in current state", presetId, cameraConfiguration.Id); Console.ResetColor(); return(null); } // Any logic for deciding on run // Update statusMap with conditions if (!(currentState.IsAnyAlgorithmsActive() || currentState.IsAnyCameraActive() || currentState.IsAnyPresetActive())) { RunCompatibility = 1; } // If same algorithm, camera is there but preset is over, then run else if (currentState.IsAlgorithmActive(presetConfiguration.AlgorithmId) && currentState.IsCameraActive(cameraConfiguration.Id) && !(currentState.IsPresetActive(presetId))) { RunCompatibility = 2; } else { RunCompatibility = 5; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[FIX NEEDED] ExecutePreset(presetId={0}, cameraId={1}) Backdoor activated for allowing execution", presetId, cameraConfiguration.Id); Console.ResetColor(); } if (currentState.IsCameraActive(cameraConfiguration.Id)) { targetCameraOpen = true; } if (currentState.IsAlgorithmActive(presetConfiguration.AlgorithmId)) { targetAlgorithmActive = true; } // If run compatibility satisfies, run the algorithm in specific compute device if (RunCompatibility > 0) { string result = ""; // If camera already not loaded, call LoadCamera if (!targetCameraOpen) { result = utility.LoadCamera(cameraConfiguration.Id, computeDevice, port); if (result == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] ExecutePreset(presetId={0}, cameraId={1}) LoadCamera() Failed", presetId, cameraConfiguration.Id); Console.ResetColor(); return(null); } else { computeDeviceStateMap[computeDevice][port].SetCameraActive(cameraConfiguration); logger.LogComputeDeviceStateMap(computeDeviceStateMap, string.Format("SetCameraActive={0}", cameraConfiguration.Id + "," + cameraConfiguration.Label)); } } // If preset request to run ScalarExecuteCommand attribute, call the InitCommandFirst if (!targetAlgorithmActive) { result = utility.ExecuteAlgorithm(presetConfiguration, computeDevice.IpAddress, "InitCommand"); if (result == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] ExecutePreset(presetId={0}, cameraId={1}) InitAlgo Failed", presetId, cameraConfiguration.Id); Console.ResetColor(); return(null); } } computeDeviceStateMap[computeDevice][port].SetPresetActive(presetConfiguration, utility.GetAlgorithmsList()); presetCameraMap[presetId] = cameraConfiguration; logger.LogComputeDeviceStateMap(computeDeviceStateMap, "SetPresetActive=" + presetConfiguration.Id + "," + presetConfiguration.Name); // If preset is one time run, then clear preset from the statusMap if (presetConfiguration.ReturnResult && !presetConfiguration.InfiniteLoop && (presetConfiguration.RunOnce || presetConfiguration.LoopLimit == 1)) { computeDeviceStateMap[computeDevice][port].ClearPreset(presetConfiguration.Id); logger.LogComputeDeviceStateMap(computeDeviceStateMap, "One Run ClearPreset=" + presetConfiguration.Id + "," + presetConfiguration.Name); } if (postScript == null) { result = utility.ExecuteAlgorithm(presetConfiguration, computeDevice.IpAddress); } else { result = utility.ExecuteAlgorithm(presetConfiguration, computeDevice.IpAddress, resultStatementToOverride: postScript); } if (result == null) { computeDeviceStateMap[computeDevice][port].ClearPreset(presetConfiguration.Id); logger.LogComputeDeviceStateMap(computeDeviceStateMap, "Preset Failure ClearPreset=" + presetConfiguration.Id + "," + presetConfiguration.Name); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] ExecutePreset(presetId={0}, cameraId={1}) Preset Execution Failed", presetId, cameraConfiguration.Id); Console.ResetColor(); return(null); } return(result); } return(null); }