public static void Start()
        {
            PushHub.PushData(a =>
            {
                try
                {
                    double time        = DateTime.Now.GetTotalMilliseconds();// - 28800000;
                    float load         = SystemInfo.CpuLoad;
                    double temperature = SystemInfo.GetCPUTemperature();
                    double mem         = (1 - SystemInfo.MemoryAvailable.To <double>() / SystemInfo.PhysicalMemory.To <double>()) * 100;
                    a.receiveLoad($"[{time},{load},{mem},{temperature}]");//CPU

                    var read  = SystemInfo.GetDiskData(DiskData.Read) / 1024;
                    var write = SystemInfo.GetDiskData(DiskData.Write) / 1024;
                    a.receiveReadWrite($"[{time},{read},{write}]");//磁盘IO

                    var up   = SystemInfo.GetNetData(NetData.Received) / 1024;
                    var down = SystemInfo.GetNetData(NetData.Sent) / 1024;
                    a.receiveUpDown($"[{time},{down},{up}]");//网络上下载

                    if (mem > 90)
                    {
                        Windows.ClearMemorySilent();
                    }
                    //缓存历史数据
                    if (CommonHelper.HistoryCpuLoad.Count < 50 || (time / 10000).ToInt32() % 12 == 0)
                    {
                        CommonHelper.HistoryCpuLoad.Add(new object[] { time, load });
                        CommonHelper.HistoryCpuTemp.Add(new object[] { time, temperature });
                        CommonHelper.HistoryMemoryUsage.Add(new object[] { time, mem });
                        CommonHelper.HistoryIORead.Add(new object[] { time, read });
                        CommonHelper.HistoryIOWrite.Add(new object[] { time, write });
                        CommonHelper.HistoryNetReceive.Add(new object[] { time, up });
                        CommonHelper.HistoryNetSend.Add(new object[] { time, down });
                        if (CommonHelper.HistoryCpuLoad.Count > 720)
                        {
                            CommonHelper.HistoryCpuLoad.RemoveAt(0);
                            CommonHelper.HistoryMemoryUsage.RemoveAt(0);
                            CommonHelper.HistoryCpuTemp.RemoveAt(0);
                        }
                        if (CommonHelper.HistoryIORead.Count > 720)
                        {
                            CommonHelper.HistoryIORead.RemoveAt(0);
                            CommonHelper.HistoryIOWrite.RemoveAt(0);
                            CommonHelper.HistoryNetReceive.RemoveAt(0);
                            CommonHelper.HistoryNetSend.RemoveAt(0);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogManager.Error(typeof(CollectRunningInfo), e.Message);
                }
            });
        }
Example #2
0
        public JsonResult Push(string msg, string user)
        {
            AjaxResult result = new AjaxResult();

            try
            {
                PushHub pushHub = new PushHub();
                pushHub.Send(user, msg);
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Example #3
0
        public ActionResult ProcessLogin(Users user)
        {
            PushHub hub = null;

            if (this.HttpContext.Application["hub"] == null)
            {
                hub = new PushHub();
                this.HttpContext.Application["hub"] = hub;
            }
            else
            {
                hub = this.HttpContext.Application["hub"] as PushHub;
            }
            FormsAuthentication.SetAuthCookie(user.LoginName, true, FormsAuthentication.FormsCookiePath);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, user.LoginName, DateTime.Now, DateTime.Now.AddMinutes(20), false, user.LoginName);
            // generate new identity
            FormsIdentity identity = new FormsIdentity(ticket);
            HttpCookie    cookie   = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));

            // write to client.
            Response.Cookies.Add(cookie);
            return(Json(new { message = "登陆成功", state = 1 }));
        }
Example #4
0
        private Score UpdateScore(int scoreId, int newValue,
                                  IScoreRepository scoreRepository,
                                  IBracketRepository bracketRepository,
                                  ITeamRepository teamRepository)
        {
            var score   = scoreRepository.GetScoreByID(scoreId);
            var bracket = bracketRepository.GetBracketByID(score.BracketID);

            if (!bracket.Finished && newValue >= 0)
            {
                score.Value = newValue;
                scoreRepository.UpdateScore(score);
            }

            ConnectionHub.SendToAll(UPDATE);

            var scores = (List <Score>)scoreRepository.GetScores(_ => _.BracketID == score.BracketID);
            var score1 = scores[0];
            var score2 = scores[1];
            var team   = teamRepository.GetTeamByID(score.TeamID);
            var team1  = teamRepository.GetTeamByID(score1.TeamID);
            var team2  = teamRepository.GetTeamByID(score2.TeamID);

            var body = string.Format("{0} ({1}) - ({2}) {3}", team1.Name, score1.Value, score2.Value, team2.Name);

            var notification = new Notification()
            {
                Title  = string.Format("{0} scored!", team.Name),
                Body   = body,
                Params = { bracket.TournamentID.ToString() }
            };

            PushHub.Send(notification);

            return(score);
        }
Example #5
0
        private Bracket UpdateBracket(Bracket bracket, bool finished,
                                      int bracketId,
                                      IBracketRepository bracketRepository,
                                      IScoreRepository scoreRepository,
                                      ITeamRepository teamRepository)
        {
            if (!bracket.Finished)
            {
                bracket.Finished = finished;
                bracketRepository.UpdateBracket(bracket);
            }
            var brackets    = (List <Bracket>)bracketRepository.GetBrackets(_ => _.TournamentID == bracket.TournamentID);
            var notFinished = brackets.Where(b => b.Finished == false).ToList();

            if (brackets.Any() && !notFinished.Any())
            {
                var lowestLevel = brackets.OrderBy(b => b.Level).First().Level;
                if (lowestLevel > 0)
                {
                    var filteredBrackets = brackets.Where(b => b.Level == lowestLevel).ToList();
                    for (var index = 0; index + 1 < filteredBrackets.Count; index += 2)
                    {
                        var newBracket = new Bracket()
                        {
                            Level        = lowestLevel - 1,
                            Finished     = false,
                            TournamentID = bracket.TournamentID
                        };
                        var createdBracket = bracketRepository.InsertBracket(newBracket);

                        var bracketA        = filteredBrackets[index];
                        var bracketB        = filteredBrackets[index + 1];
                        var winnerAID       = FindWinnerID(bracketA.ID, scoreRepository);
                        var winnerBID       = FindWinnerID(bracketB.ID, scoreRepository);
                        var winnerIDs       = new int[] { winnerAID, winnerBID };
                        var teamsPerBracket = 2;
                        for (var x = 0; x < teamsPerBracket; x++)
                        {
                            var newScore = new Score()
                            {
                                BracketID = createdBracket.ID,
                                TeamID    = winnerIDs[x],
                                Value     = 0
                            };
                            scoreRepository.InsertScore(newScore);
                        }
                    }
                }
            }

            ConnectionHub.SendToAll(UPDATE);

            var scores = (List <Score>)scoreRepository.GetScores(_ => _.BracketID == bracketId);
            var score1 = scores[0];
            var score2 = scores[1];
            var team1  = teamRepository.GetTeamByID(score1.TeamID);
            var team2  = teamRepository.GetTeamByID(score2.TeamID);

            var body = string.Format("{0} ({1}) - ({2}) {3}", team1.Name, score1.Value, score2.Value, team2.Name);

            var notification = new Notification()
            {
                Title  = "A bracket was finished!",
                Body   = body,
                Params = { bracket.TournamentID.ToString() }
            };

            PushHub.Send(notification);

            return(bracket);
        }
Example #6
0
        private Tournament CreateTournament(
            ITournamentRepository tournamentRepository,
            ITeamRepository teamRepository,
            IBracketRepository bracketRepository,
            IScoreRepository scoreRepository,
            Tournament tournament,
            Team[] teamsNames,
            int categoryId,
            int userId
            )
        {
            tournament.CategoryID      = categoryId;
            tournament.AdministratorID = userId;
            var createdTournament = tournamentRepository.InsertTournament(tournament);

            var initialLevel = (int)Math.Log2(teamsNames.Length) - 1;

            for (int i = 0; i + 1 < teamsNames.Length; i += 2)
            {
                var team1 = new Team
                {
                    Name = teamsNames[i].Name
                };
                var team2 = new Team
                {
                    Name = teamsNames[i + 1].Name
                };
                var createdTeam1 = teamRepository.InsertTeam(team1);
                var createdTeam2 = teamRepository.InsertTeam(team2);
                if (i % 2 == 0)
                {
                    var bracket = new Bracket
                    {
                        Level      = initialLevel,
                        Finished   = false,
                        Tournament = createdTournament
                    };
                    var createdBracket = bracketRepository.InsertBracket(bracket);
                    var score1         = new Score
                    {
                        Value   = 0,
                        Team    = createdTeam1,
                        Bracket = createdBracket
                    };
                    var score2 = new Score
                    {
                        Value   = 0,
                        Team    = createdTeam2,
                        Bracket = createdBracket
                    };
                    scoreRepository.InsertScore(score1);
                    scoreRepository.InsertScore(score2);
                }
            }

            ConnectionHub.SendToAll(UPDATE);

            var notification = new Notification()
            {
                Title  = "New tournament created!",
                Body   = createdTournament.Name,
                Params = { createdTournament.ID.ToString() }
            };

            PushHub.Send(notification);

            return(createdTournament);
        }
Example #7
0
 public ActionResult Unsubscribe([FromBody] PushSubscriptionModel model)
 {
     PushHub.Unsubscribe(model.Subscription);
     return(Ok());
 }
Example #8
0
 public ActionResult <string> GetVapidPublicKey()
 {
     return(Ok(PushHub.GetVapidPublicKey()));
 }