public PresetDescription QueryPresetById(string presetId, string host = "{{host}}", string port = "{{port}}")
 {
     if (presetDescriptionMap.ContainsKey(presetId))
     {
         PresetDescription result = presetDescriptionMap[presetId].Clone();
         result.ResultProcessingStatement = result.ResultProcessingStatement.Replace("{{host}}", host).Replace("{{port}}", port);
         return(result);
     }
     return(null);
 }
        private void BtnRunTFSSD_Click(object sender, EventArgs e)
        {
            ComputeDeviceInfo firstDevice = controller.utility.GetComputeDevicesList().First();
            PresetDescription preset      = new PresetDescription("1000", "Sample", "Algo3", firstDevice.ComputeDeviceId, firstDevice.PortList.First(), true, false, 1, true);
            string            result      = controller.utility.ExecuteAlgorithm(preset, firstDevice.IpAddress);

            if (result != null)
            {
                Console.WriteLine("[INFO] RunAlgo: " + result);
            }
            else
            {
                Console.WriteLine("Failure");
            }
        }
Exemplo n.º 3
0
        public bool SetPresetActive(PresetDescription presetDescription, List <AlgorithmDescription> algorithmDescriptionList)
        {
            int algoIndex = algorithmDescriptionList.FindIndex(algo => algo.Id == presetDescription.AlgorithmId);

            if (algoIndex < 0 || algoIndex >= algorithmDescriptionList.Count)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] SetPresetActive(presetId={0}) Algorithm not present in provided list");
                Console.ResetColor();
                return(false);
            }

            AssignedPresetDescriptionMap[presetDescription] = true;
            AssignedAlgorithmDescriptionMap[algorithmDescriptionList[algoIndex]] = true;
            return(true);
        }
Exemplo n.º 4
0
        public bool ClearPreset(string presetId)
        {
            PresetDescription presetDescription = null;

            foreach (var preset in AssignedPresetDescriptionMap)
            {
                if (preset.Key.Id == presetId && preset.Value)
                {
                    presetDescription = preset.Key;
                }
            }

            if (presetDescription != null)
            {
                AssignedPresetDescriptionMap[presetDescription] = false;
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public string ExecuteAlgorithm(PresetDescription presetDescription, string ipAddress, string commandAttribute = "ScalarExecuteCommand", string resultStatementToOverride = null)
        {
            string computeDeviceInfoId = presetDescription.ComputeDeviceId;
            string algorithmId         = presetDescription.AlgorithmId;
            int    port = presetDescription.Port;
            //string resultProcessingStatement = presetDescription.GetResultProcessingStatement(brokerHubHost, brokerHubPort);
            bool RunOnce      = presetDescription.RunOnce;
            bool InfiniteLoop = presetDescription.InfiniteLoop;
            int  LoopLimit    = presetDescription.LoopLimit;
            bool ReturnResult = presetDescription.ReturnResult;

            if (!algorithmsDescriptionMap.ContainsKey(algorithmId))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] ICANSEEUtility.ExecuteAlgorithm(algoId=" + algorithmId + ", ipAddress=" + ipAddress + ", port=" + port.ToString() + ")\n - Algorithm Id not found");
                Console.ResetColor();

                return(null);
            }

            var algorithmDescription = algorithmsDescriptionMap[algorithmId];

            if (!computeDeviceInfoMap.ContainsKey(computeDeviceInfoId))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] ICANSEEUtility.ExecuteAlgorithm(computeDevId=" + computeDeviceInfoId + ", ipAddress=" + ipAddress + ", port=" + port.ToString() + ")\n - ComputeDeviceInfo Id not found");
                Console.ResetColor();

                return(null);
            }

            var computeDeviceInfo = computeDeviceInfoMap[computeDeviceInfoId];

            string executeCommand = "";

            if (commandAttribute == "ScalarExecuteCommand" && resultStatementToOverride != null)
            {
                executeCommand = presetDescription.GetExecuteCommandWithCustomResultStatement(algorithmDescription, computeDeviceInfo, port.ToString(), resultStatementToOverride);
            }
            else if (commandAttribute == "ScalarExecuteCommand" && resultStatementToOverride == null)
            {
                executeCommand = presetDescription.GetCompleteExecuteCommand(algorithmDescription, computeDeviceInfo, port.ToString());
            }
            else if (commandAttribute == "InitCommand")
            {
                executeCommand = presetDescription.GetCompleteInitCommand(algorithmDescription, computeDeviceInfo, port.ToString());
            }
            else if (commandAttribute == "UnloadCommand")
            {
                executeCommand = presetDescription.GetCompleteUnloadCommand(algorithmDescription, computeDeviceInfo, port.ToString());
            }


            string apiCallBody = "{\n\"Fbp\":[\"Start\",\"\",\"" + executeCommand + "\"],\"RunOnce\": {{RunOnce}},\"InfiniteLoop\": {{InfiniteLoop}},\"LoopLimit\": {{LoopLimit}},\"ReturnResult\": {{ReturnResult}}}";


            try
            {
                if (RunOnce)
                {
                    apiCallBody = apiCallBody.Replace("{{RunOnce}}", "true");
                }
                else
                {
                    apiCallBody = apiCallBody.Replace("{{RunOnce}}", "false");
                }

                if (InfiniteLoop)
                {
                    apiCallBody = apiCallBody.Replace("{{InfiniteLoop}}", "true");
                }
                else
                {
                    apiCallBody = apiCallBody.Replace("{{InfiniteLoop}}", "false");
                }

                apiCallBody = apiCallBody.Replace("{{LoopLimit}}", LoopLimit.ToString());

                if (ReturnResult)
                {
                    apiCallBody = apiCallBody.Replace("{{ReturnResult}}", "true");
                }
                else
                {
                    apiCallBody = apiCallBody.Replace("{{ReturnResult}}", "false");
                }

                Console.WriteLine("[DEBUG] ApiCallBody(" + ipAddress + port.ToString() + "): " + apiCallBody);

                string result = imageClient.MakePostCall(algorithmDescription.GetUri(ipAddress, port.ToString()), apiCallBody);
                return(result);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] ICANSEEUtility.ExecuteAlgorithm(presetId=" + presetDescription.Id + ", ipAddress=" + ipAddress + ", port=" + port.ToString() + ") " + "\n" + apiCallBody + "\n" + ex.Message);
                Console.ResetColor();
            }
            return(null);
        }
Exemplo n.º 6
0
        // 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);
        }