예제 #1
0
 internal void OnNotifyExecutionStepChanged(ExCommsExecutionStepEntity entity)
 {
     using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "OnNotifyExecutionStepChanged"))
     {
         try
         {
             if (_cbExecutionStepChanged.HasClients)
             {
                 _cbExecutionStepChanged.InvokeCallback((i) =>
                 {
                     i.ExecutionStepChanged(entity);
                 });
             }
         }
         catch (Exception ex)
         {
             method.Exception(ex);
         }
     }
 }
        public bool Execute(IFreeformEntity_Msg request)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "Execute"))
            {
                bool   result = default(bool);
                string key    = string.Empty;

                try
                {
                    key = request.IpAddress;
                    ExecutionStepDictionary steps = _executionSteps;
                    _isExecuting = true;

                    //_gmuExecutionStepsExecuting.AddOrUpdate(key, true, (k, b) => { return true; });

                    //if (!_gmuExecutionSteps.ContainsKey(key))
                    //{
                    //    steps = new ExecutionStepDictionary();
                    //    steps.Entity.GmuIpAddress = key;
                    //    steps.CreateExecutionSteps(ExecutionStepFactory.Current.DeviceType, _messageWiseSteps, _groupedSteps);
                    //    _gmuExecutionSteps.Add(key, steps);
                    //    _entities.Add(steps.Entity);
                    //}
                    //else
                    //{
                    //    steps = _gmuExecutionSteps[key];
                    //}

                    if (steps != null)
                    {
                        // has clients
                        bool hasClients = false;
                        ExCommsExecutionStepEntity entity          = steps.Entity;
                        IExecutionStep             execStepCurrent = null;
                        IExecutionStep             execStepLatest  = null;
                        entity.Steps.Clear();

                        if (ExCommsServerImpl.Current != null)
                        {
                            hasClients = ExCommsServerImpl.Current.HasStepChangedClients;
                        }

#if !OLD_CODE
                        // 1. find the execution step by message
                        string messageKey = request.EntityUniqueKeyDirection;
                        if (_messageWiseSteps.ContainsKey(messageKey))
                        {
                            var stepList = _messageWiseSteps[messageKey];
                            if (stepList.Count == 1)
                            {
                                execStepCurrent = stepList[0];
                            }
                        }

                        // execute the step
                        if (execStepCurrent != null)
                        {
                            // last step
                            IExecutionStep execStepLast = (steps.Current ?? steps.Start);
                            if (hasClients && execStepLast != null)
                            {
                                entity.Steps.Add(execStepLast.GetType().Name);
                            }

                            // execute the current step
                            execStepLatest = execStepCurrent.Execute(request);

                            //// move to or set to next step
                            //if (execStepLatest != null &&
                            //    execStepLatest.ExecutionResult == ExecutionStepResult.Success)
                            //{
                            //    if (execStepLatest.NextStep != null)
                            //    {
                            //        if (execStepLatest.MoveToNextStep)
                            //        {
                            //            if (hasClients && execStepLatest.NextStep != null)
                            //                entity.Steps.Add(execStepLatest.NextStep.GetType().Name);
                            //            execStepLatest = execStepLatest.NextStep.Execute(request);
                            //        }

                            //        //if (execStepLatest.SetNextStep)
                            //        //{
                            //        //    if (hasClients)
                            //        //        entity.Steps.Add(execStepLatest.GetType().Name);
                            //        //    execStepLatest = execStepLatest.NextStep;
                            //        //    execStepLatest.ExecutionResult = ExecutionStepResult.Success;
                            //        //}
                            //    }
                            //}

                            if (execStepLatest != null &&
                                execStepLatest.ExecutionResult == ExecutionStepResult.Success &&
                                !Object.ReferenceEquals(steps.Current, execStepLatest))
                            {
                                steps.Current = execStepLatest;
                            }
                        }
#else
                        // last step
                        IExecutionStep step = (steps.Current ?? steps.Start);
                        if (hasClients && step != null)
                        {
                            entity.Steps.Add(step.GetType().Name);
                        }

                        // previous step
                        if (step.ExecutionResult == ExecutionStepResult.Failed &&
                            step.PrevStep != null)
                        {
                            step = step.PrevStep;
                        }

                        // execute the current step
                        if (hasClients && step != null)
                        {
                            entity.Steps.Add(step.GetType().Name);
                        }
                        IExecutionStep step2 = step.Execute(request);

                        // move to or set to next step
                        if (step2 != null &&
                            step2.ExecutionResult == ExecutionStepResult.Success)
                        {
                            if (step2.NextStep != null)
                            {
                                if (step2.MoveToNextStep)
                                {
                                    if (hasClients && step2.NextStep != null)
                                    {
                                        entity.Steps.Add(step2.NextStep.GetType().Name);
                                    }
                                    step2 = step2.NextStep.Execute(request);
                                }

                                if (step2.SetNextStep)
                                {
                                    if (hasClients)
                                    {
                                        entity.Steps.Add(step2.GetType().Name);
                                    }
                                    step2 = step2.NextStep;
                                    step2.ExecutionResult = ExecutionStepResult.Success;
                                }
                            }
                        }

                        if (step2 != null &&
                            step2.ExecutionResult == ExecutionStepResult.Success &&
                            !Object.ReferenceEquals(steps.Current, step2))
                        {
                            steps.Current = step2;
                        }
#endif

                        if (hasClients)
                        {
                            if (execStepLatest != null)
                            {
                                entity.Steps.Add(execStepLatest.UniqueKey);
                            }
                            ExCommsServerImpl.Current.OnNotifyExecutionStepChanged(entity);
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
                finally
                {
                    //_gmuExecutionStepsExecuting.AddOrUpdate(key, false, (k, b) => { return false; });
                    _isExecuting = false;
                }

                return(result);
            }
        }