public VirtualRobot(int port, ConcurrentQueue <DBQueueObject> VCQueue, HttpServer server, string BPAPrefix, InfluxSender influxSender)
        {
            this.port        = port;
            this.id          = Guid.NewGuid().ToString();
            this.token       = null;
            this.VCQueue     = VCQueue;
            this.server      = server;
            this.UseBPAQueue = false;
            this.dbready     = false;

            this.influxSender = influxSender;
            this.BPAPrefix    = BPAPrefix;

            DBQueueObject DBQueueObject = new DBQueueObject(this);

            //DBQueueObject.needupdate = true;
            DBQueueObject.Query = DBQueries.UpdateBPAResource(port, 2, 0, BPAPrefix);
            VCQueue.Enqueue(DBQueueObject);
            //  this.DBQueueObject = new DBQueueObject(this);

            //            this.DBQueueObject.needupdate = true;

            //            VCQueue.Enqueue(DBQueries.CreateBPAResource(id, port));


            Console.WriteLine("VirtualRobot created on " + port);
        }
 public void BPAResourceUpdater()
 {
     while (UseBPAResourceUpdater == 1)
     {
         try
         {
             DBQueueObject DBQueueObject = new DBQueueObject(null);
             DBQueueObject.Query = DBQueries.UpdateBPAResourcesLastUpdated();
             VCQueue.Enqueue(DBQueueObject);
         }
         catch (Exception e)
         { Console.WriteLine(e); }
         Thread.Sleep(20000);
     }
 }
        public VirtualController(int VCport, List <int> RobotPorts, string[] DBData, List <DBSender> DBSenders,
                                 int HTTP_Threads_Count, int UseBPAResourceUpdater, string[] InfluxData, string BPAPrefix)
        {
            this.VCport                = VCport;
            this.RobotPorts            = RobotPorts;
            this.DBSenders             = DBSenders;
            this.DBData                = DBData;
            this.InfluxData            = InfluxData;
            this.isCreatingRobots      = false;
            this.HTTP_Threads_Count    = HTTP_Threads_Count;
            this.UseBPAResourceUpdater = UseBPAResourceUpdater;
            this.BPAPrefix             = BPAPrefix;
            StartServer();

            DBQueueObject dBQueueObject = new DBQueueObject(null);

            dBQueueObject.Query = DBQueries.RebuildSessions();
            VCQueue.Enqueue(dBQueueObject);

            CreateInfluxDBSender(1);



            DBQueueObject InfluxQueueObject = new DBQueueObject(null);

            InfluxQueueObject.Query = DBQueries.InsertIntoInflux("200", "1090");

            InfluxQueue.Enqueue(InfluxQueueObject);

            //();



            new Thread(BPAResourceUpdater).Start();

            Console.WriteLine("VirtualController created on " + VCport);
            Console.WriteLine(RobotPorts.Contains(VCport));
        }
Example #4
0
        public void Start(ConcurrentQueue <DBQueueObject> VCQueue, int duration, bool UseBPAQueue, string bpaqueuename, string tag)
        {
            //        this.duration = duration;
            if (!(this is null) && this.status != 1)
            {
                Random Random    = new Random();
                double SleepTime = duration * 0.9 + duration * (Random.NextDouble() * 0.2);
                SleepTime = SleepTime * 1000;

                //                this.finishTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (int)(SleepTime * 0.001);

                status = 1;

                DBQueueObject DBQueueObject;

                if (UseBPAQueue)
                {
                    //                Thread.Sleep(1);
                    DBQueueObject            = new DBQueueObject(VR);
                    DBQueueObject.needupdate = true;
                    DBQueueObject.Query      = DBQueries.InsertBPACaseLock(VR.VS.id, bpaqueuename, tag) + ";" + DBQueries.UpdateBPAResource(port, 1, 1, VR.BPAPrefix) + "; " + DBQueries.UpdateSession(id, status);;
                    VCQueue.Enqueue(DBQueueObject);
                }

                else
                {
                    DBQueueObject            = new DBQueueObject(VR);
                    DBQueueObject.needupdate = true;
                    DBQueueObject.Query      = DBQueries.UpdateBPAResource(port, 1, 1, VR.BPAPrefix) + "; " + DBQueries.UpdateSession(id, status);;
                    VCQueue.Enqueue(DBQueueObject);
                }

                Thread sleep = new Thread(() =>
                {
                    Thread.Sleep(Convert.ToInt32(SleepTime));
                    status = 2;
                    Console.WriteLine("Sleeptime = " + SleepTime);
                    if (!(VR is null))
                    {
                        try
                        {
                            DBQueueObject = new DBQueueObject(VR);

                            if (UseBPAQueue)
                            {
                                DBQueueObject.Query = DBQueries.UpdateBPAResource(port, 2, 0, VR.BPAPrefix) + "; " +
                                                      DBQueries.UpdateSessionEndTime(id, status) + "; " + DBQueries.BPAWorkQueueItem(id, bpaqueuename, tag) +
                                                      " ; " + DBQueries.DeleteFromBPACaseLock(VR.VS.id);
                                ;
                                VCQueue.Enqueue(DBQueueObject);
                            }
                            else
                            {
                                DBQueueObject.Query = DBQueries.UpdateBPAResource(port, 2, 0, VR.BPAPrefix) + "; " +
                                                      DBQueries.UpdateSessionEndTime(id, status);
                                VCQueue.Enqueue(DBQueueObject);
                            }
                        }

                        catch (Exception e)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkMagenta;
                            Console.WriteLine("Session Start Error:");
                            if (DBQueueObject.Query is null)
                            {
                                Console.WriteLine("\nDBQueueObjectQuery is null");
                            }
                            else
                            {
                                Console.WriteLine("\nDBQueueObject.Query: " + DBQueueObject.Query);
                            }
                            if (DBQueueObject.VR is null)
                            {
                                Console.WriteLine("\nDBQueueObjectVR is null");
                            }
                            else
                            {
                                Console.WriteLine("\nDBQueueObject.VR: " + DBQueueObject.VR.id);
                            }
                            if (tag is null)
                            {
                                Console.WriteLine("\ntag is null");
                            }
                            else
                            {
                                Console.WriteLine("tag =" + tag);
                            }
                            Console.WriteLine(e);
                        }
                    }
                });
                sleep.Start();
            }
        }