Example #1
0
        public static void UpdateScriptFromStation(String BlockID, String BlockName, String StationName, DateTime ExecutionTimeStart, DateTime ExecutionTimeEnd, int CommandCounter, int ExecutionNumber, String DeviceName, String DeviceCategory, String CommandName, String Owner, int DelayTime, String Parameter, String ScriptState)
        {
            ScriptTB ExistingScript = ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockID == BlockID && Item.Value.ExecutionNumber == ExecutionNumber).Value;

            if (ExistingScript != null)
            {
                ExistingScript.ScriptState = ScriptState;
                UpdateScriptToMonitoring(ExistingScript);
                DatabaseSynchronization.ScriptSaveChange(false);
            }
        }
Example #2
0
        private static void SetStationStateToWaitingStation(Object ScriptBlockID)
        {
            String BlockID = ScriptBlockID.ToString();
            List <KeyValuePair <String, ScriptTB> > ScriptList = ScriptDBBuffer.Where(Item => Item.Value.BlockID == BlockID).ToList();

            foreach (KeyValuePair <String, ScriptTB> ScriptNode in ScriptList)
            {
                ScriptNode.Value.ScriptState = SCRIPTSTATE.WAITINGSTATION.ToString();
                UpdateScriptToMonitoring(ScriptNode.Value);
            }

            DatabaseSynchronization.ScriptSaveChange(true);
        }
Example #3
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);
        }
Example #4
0
        public static void LoadScriptFromDB()
        {
            List <ScriptTB> ScriptList = DatabaseSynchronization.GetScript();

            if (ScriptList == null)
            {
                return;
            }

            foreach (ScriptTB ThisScript in ScriptList)
            {
                ScriptDBBuffer.TryAdd(ThisScript.BlockID + ThisScript.ExecutionNumber, ThisScript);
            }
        }
Example #5
0
        public static Boolean RemoveScript(String BlockID, int ExecutionNumber)
        {
            try
            {
                ScriptTB ThisBuffer = ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockID == BlockID && Item.Value.ExecutionNumber == ExecutionNumber).Value;
                ScriptDBBuffer.TryRemove(ThisBuffer.BlockID + ThisBuffer.ExecutionNumber, out ThisBuffer);
                DatabaseSynchronization.DeleteScript(ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockID == BlockID && Item.Value.ExecutionNumber == ExecutionNumber).Value);
                DatabaseSynchronization.ScriptSaveChange(true);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #6
0
        private static void UpdateExecutionState(STATIONNAME StationName, Object Value)
        {
            String[] ValueArr        = Value.ToString().Split(new char[] { ',' });
            String   BlockID         = ValueArr[0];
            int      ExecutionNumber = Convert.ToInt32(ValueArr[1]);
            String   ScriptState     = ValueArr[2];

            ScriptTB ScriptDB = ScriptDBBuffer.FirstOrDefault(Item => Item.Value.BlockID == BlockID && Item.Value.ExecutionNumber == ExecutionNumber).Value;

            if (ScriptDB == null)
            {
                return;
            }

            ScriptDB.ScriptState = ScriptState;
            DatabaseSynchronization.ScriptSaveChange(true);

            if (ScriptState == "ENDTIMEPASSED")
            {
                Console.WriteLine("ENDTIMEPASSED");
            }

            UpdateScriptToMonitoring(ScriptDB);
        }
Example #7
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);
                }
            });
        }
Example #8
0
 public static void RemoveAllScriptDB()
 {
     ScriptDBBuffer.Clear();
     DatabaseSynchronization.DeleteAllScript();
     DatabaseSynchronization.ScriptSaveChange(false);
 }