Пример #1
0
        private static Boolean ResponseToStation(ScriptBuffer ThisBuffer)
        {
            Boolean IsSend = false;

            if (ScriptDBBuffer.Count > 0)
            {
                List <ScriptTB> WaitingScriptList = ScriptDBBuffer.Values.Where(Item => Item.ScriptState == "CREATED" || Item.ScriptState == "WAITINGSERVER" && Item.ExecutionNumber == 1).ToList();

                if (WaitingScriptList.Count > 0)
                {
                    while (WaitingScriptList.Count > 0)
                    {
                        String Message = "";

                        Boolean IsAllScriptRecived = IsBlockComplete(WaitingScriptList[0], out Message);
                        if (IsAllScriptRecived)
                        {
                            if (!VerifyTotalExecutionTime(WaitingScriptList[0]))
                            {
                                if (ThisBuffer != null)
                                {
                                    WebSockets.ReturnScriptResult(ThisBuffer.WSConnection, ThisBuffer.Script.BlockName, ThisBuffer.Script.BlockID, ThisBuffer.Script.ExecutionNumber.ToString(), ThisBuffer.Script.CommandName.ToString(), "All script is sending to client.", "Script_Success");
                                }

                                List <ScriptTB> InValidScript = ScriptDBBuffer.Values.Where(Item => Item.BlockID == WaitingScriptList[0].BlockID).ToList();

                                foreach (ScriptTB ThisScript in InValidScript)
                                {
                                    ScriptTB TempScript = null;
                                    ScriptDBBuffer.TryRemove(ThisScript.BlockID + ThisScript.ExecutionNumber, out TempScript);
                                    DatabaseSynchronization.DeleteScript(TempScript);

                                    RemoveScriptToMonitoring(TempScript);
                                }
                                return(false);
                            }

                            SendScriptToStation(WaitingScriptList[0]);
                            WaitingScriptList.RemoveAll(Item => Item.BlockID == WaitingScriptList[0].BlockID);
                            IsSend = true;
                        }
                        else
                        {
                            WaitingScriptList.RemoveAt(0);
                        }
                    }
                }
            }

            return(IsSend);
        }
Пример #2
0
 private static void ResponseToClient(ScriptBuffer ThisBuffer)
 {
     if (ThisBuffer != null)
     {
         String  Message            = "";
         Boolean IsAllScriptRecived = IsBlockComplete(ThisBuffer.Script, out Message);
         if (IsAllScriptRecived)
         {
             WebSockets.ReturnScriptResult(ThisBuffer.WSConnection, ThisBuffer.Script.BlockName, ThisBuffer.Script.BlockID, ThisBuffer.Script.ExecutionNumber.ToString(), ThisBuffer.Script.CommandName.ToString(), "All script is sending to client.", "Script_Success");
         }
         else
         {
             WebSockets.ReturnScriptResult(ThisBuffer.WSConnection, ThisBuffer.Script.BlockName, ThisBuffer.Script.BlockID, ThisBuffer.Script.ExecutionNumber.ToString(), ThisBuffer.Script.CommandName.ToString(), Message, "Script_OK");
         }
     }
 }
Пример #3
0
        public static void NewScriptFromSocket(ScriptStructure NewScriptStructure, WSConnection ScriptConnection)
        {
            ScriptBuffer NewBuffer = new ScriptBuffer();

            NewBuffer.Script                    = new ScriptTB();
            NewBuffer.Script.TargetID           = NewScriptStructure.TargetID;
            NewBuffer.Script.BlockID            = NewScriptStructure.BlockID;
            NewBuffer.Script.BlockName          = NewScriptStructure.BlockName;
            NewBuffer.Script.CommandCounter     = NewScriptStructure.CommandCounter;
            NewBuffer.Script.CommandName        = NewScriptStructure.CommandName;
            NewBuffer.Script.DelayTime          = (int)NewScriptStructure.DelayTime;
            NewBuffer.Script.DeviceCategory     = NewScriptStructure.DeviceCategory.ToString();
            NewBuffer.Script.DeviceName         = NewScriptStructure.DeviceName.ToString();
            NewBuffer.Script.ExecutionNumber    = NewScriptStructure.ExecutionNumber.Value;
            NewBuffer.Script.ExecutionTimeEnd   = NewScriptStructure.ExecutionTimeEnd;
            NewBuffer.Script.ExecutionTimeStart = NewScriptStructure.ExecutionTimeStart;
            NewBuffer.Script.Owner              = NewScriptStructure.Owner;
            NewBuffer.Script.Parameter          = String.Join(",", NewScriptStructure.Parameter);
            NewBuffer.Script.ScriptState        = NewScriptStructure.ScriptState.ToString();
            NewBuffer.Script.StationName        = NewScriptStructure.StationName.ToString();
            NewBuffer.WSConnection              = ScriptConnection;

            NewScriptBuffer.Enqueue(NewBuffer);
        }
Пример #4
0
        private static void ReadScriptFromBuffer()
        {
            Task TTCSTask = Task.Run(() =>
            {
                Boolean IsDatabaseUpdate = false;
                while (IsScriptActive)
                {
                    ScriptBuffer ThisBuffer = null;
                    if (NewScriptBuffer.TryDequeue(out ThisBuffer))
                    {
                        ScriptTB ExistingScript = ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockName == ThisBuffer.Script.BlockName && Item.Value.ScriptState != "EXECUTED").Value;
                        if (ExistingScript == null)  //New block name
                        {
                            ThisBuffer.Script.TargetID    = TTCSHelper.GenNewID();
                            ThisBuffer.Script.BlockID     = ThisBuffer.Script.TargetID;
                            ThisBuffer.Script.ScriptState = "CREATED";
                            ScriptDBBuffer.TryAdd(ThisBuffer.Script.BlockID + ThisBuffer.Script.ExecutionNumber, ThisBuffer.Script);

                            DatabaseSynchronization.InsertScript(ThisBuffer.Script);
                            IsDatabaseUpdate = true;
                            AddScriptToMonitoring(ThisBuffer.Script);
                        }
                        else if (ExistingScript.ScriptState != "EXECUTING") //Existing block name
                        {
                            ScriptTB ExistingBlockScript = ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockName == ThisBuffer.Script.BlockName && Item.Value.ExecutionNumber == ThisBuffer.Script.ExecutionNumber && Item.Value.ScriptState != "EXECUTED").Value;

                            if (ExistingBlockScript == null)  //Add new script to the same BlockName
                            {
                                ExistingBlockScript         = ThisBuffer.Script;
                                ExistingBlockScript.BlockID = ExistingScript.BlockID;
                                ScriptDBBuffer.TryAdd(ExistingBlockScript.BlockID + ExistingBlockScript.ExecutionNumber, ExistingBlockScript);

                                DatabaseSynchronization.InsertScript(ExistingBlockScript);
                                IsDatabaseUpdate = true;
                                AddScriptToMonitoring(ExistingBlockScript);
                            }
                            else
                            {
                                if (!IsEqualScript(ExistingBlockScript, ThisBuffer.Script))  //Update existing BlockName
                                {
                                    ExistingBlockScript.CommandCounter     = ThisBuffer.Script.CommandCounter;
                                    ExistingBlockScript.CommandName        = ThisBuffer.Script.CommandName;
                                    ExistingBlockScript.DelayTime          = ThisBuffer.Script.DelayTime;
                                    ExistingBlockScript.DeviceCategory     = ThisBuffer.Script.DeviceCategory;
                                    ExistingBlockScript.DeviceName         = ThisBuffer.Script.DeviceName;
                                    ExistingBlockScript.ExecutionTimeEnd   = ThisBuffer.Script.ExecutionTimeEnd;
                                    ExistingBlockScript.ExecutionTimeStart = ThisBuffer.Script.ExecutionTimeStart;
                                    ExistingBlockScript.Owner       = ThisBuffer.Script.Owner;
                                    ExistingBlockScript.Parameter   = ThisBuffer.Script.Parameter;
                                    ExistingBlockScript.ScriptState = "CREATED";
                                    ExistingBlockScript.StationName = ThisBuffer.Script.StationName;

                                    UpdateScriptToMonitoring(ExistingBlockScript);
                                }
                            }
                        }
                    }

                    ResponseToClient(ThisBuffer);
                    if (!IsDatabaseUpdate)
                    {
                        IsDatabaseUpdate = ResponseToStation(ThisBuffer);
                    }
                    else
                    {
                        ResponseToStation(ThisBuffer);
                    }

                    if (!IsDatabaseUpdate)
                    {
                        IsDatabaseUpdate = RemoveExpireScript();
                    }
                    else
                    {
                        RemoveExpireScript();
                    }

                    if (IsDatabaseUpdate)
                    {
                        IsDatabaseUpdate = !DatabaseSynchronization.ScriptSaveChange(true);
                    }

                    Thread.Sleep(10);
                }
            });
        }