public async Task Send(RobotCommand command)
 {
     if (await IsConnected())
     {
         var sendTask = Task.Run(() =>
         {
             try
             {
                 ComPort.Write(command.ToString());
             }
             catch (Exception e)
             {
                 Logger.Instance.Log("ERROR:::" + Name + ":" + e.Message);
             }
         });
         if (!sendTask.Wait(TimeSpan.FromMilliseconds(800)))
         {
             Logger.Instance.Log("KILLED:::Robot comm to '" + Name + "' too slow");
         }
     }
     else
     {
         Logger.Instance.Log(Name + " is not connected.");
     }
 }
        private async void DBLoop()
        {
            Logger.Instance.Log("Communication loop has started");
            while (comms)
            {
                var dbTask = Task.Run(() => database.Query());
                if (!dbTask.Wait(TimeSpan.FromMilliseconds(500)))
                {
                    Logger.Instance.Log("KILLED:::DB Query too slow");
                }

                var robots = await rController.GetAllRobots();

                for (int i = 0; i < robots.Length; i++)
                {
                    var command = new RobotCommand(
                        database.DB_Info[i][1].ToString(),
                        GetPower(database.DB_Info[i][3].ToString()),
                        (int)database.DB_Info[i][2]
                        );

                    if (command != _lastCommand[i] && !robots[i]._override)
                    {
                        _lastCommand[i] = command;
                        await robots[i].Send(command);
                    }
                }

                var local = await rController.GetAllRobotInfo();

                dbTask = Task.Run(() => database.Update(local));

                if (!dbTask.Wait(TimeSpan.FromMilliseconds(500)))
                {
                    Logger.Instance.Log("KILLED:::DB Upload too slow");
                }

                await Task.Delay(100);
            }
            Logger.Instance.Log("Communication loop has stopped");
        }
        public Form1()
        {
            rController = new BotController(null);
            database    = new Database();
            _bgWorker   = new BackgroundWorker();
            _bgQuery    = new BackgroundWorker();
            _bgWorker.WorkerReportsProgress = true;
            _bgWorker.DoWork          += _bgWorker_DoWork;
            _bgWorker.ProgressChanged += _bgWorker_ProgressChanged;
            Logger.Instance.Log("Initialized");
            var c = new RobotCommand("N", 0, 0);

            _lastCommand = new List <RobotCommand>();
            _lastCommand.Add(c);
            _lastCommand.Add(c);
            _lastCommand.Add(c);
            _lastCommand.Add(c);
            _lastCommand.Add(c);
            InitializeComponent();
            var s = Logger.Instance.speed;

            numericUpDown1.Value = s;
        }