Пример #1
0
        public static async Task StartSpeedTestAsync()
        {
            DateTime SpeedTestTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm"));

            client   = new SpeedTestClient();
            settings = client.GetSettings();


            var servers    = SelectServers();
            var bestServer = SelectBestServer(servers);

            SpeedTest SpeetTestData = new SpeedTest();

            try
            {
                SpeetTestData.ISP             = settings.Client.Isp;
                SpeetTestData.ClientLatitude  = settings.Client.Latitude;
                SpeetTestData.ClientLongitude = settings.Client.Longitude;
                SpeetTestData.Server          = bestServer.Host;
                SpeetTestData.ServerLatitude  = bestServer.Latitude;
                SpeetTestData.ServerLongitude = bestServer.Longitude;
                SpeetTestData.DelayTime       = bestServer.Latency;
                var downloadSpeed = client.TestDownloadSpeed(bestServer, settings.Download.ThreadsPerUrl);
                SpeetTestData.SpeedDownload = PrintSpeed(downloadSpeed);
                var uploadSpeed = client.TestUploadSpeed(bestServer, settings.Upload.ThreadsPerUrl);
                SpeetTestData.SpeetUpload = PrintSpeed(uploadSpeed);
            }
            catch
            {
                SpeetTestData.ClientLatitude  = 0;
                SpeetTestData.ClientLongitude = 0;
                SpeetTestData.ISP             = "Failed";
                SpeetTestData.DelayTime       = -1;
                SpeetTestData.Server          = "Failed";
                SpeetTestData.ServerLatitude  = 0;
                SpeetTestData.ServerLongitude = 0;
                SpeetTestData.SpeedDownload   = "Failed";
                SpeetTestData.SpeetUpload     = "Failed";
            }

            SpeetTestData.Id       = Guid.NewGuid();
            SpeetTestData.TestTime = SpeedTestTime;

            try
            {
                using (PingvalueModels db = new PingvalueModels())
                {
                    db.SpeedTests.Add(SpeetTestData);
                    await db.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                LogGenerator.Add("Start speed test db error: " + ex.Message);
            }
        }
Пример #2
0
 protected void Application_Start()
 {
     LogGenerator.Add("Application startup");
     AreaRegistration.RegisterAllAreas();
     GlobalConfiguration.Configure(WebApiConfig.Register);
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     LogGenerator.Add("Application startup done.");
 }
Пример #3
0
        private static bool LineBotMessage(string Message)
        {
            try
            {
                isRock.LineBot.Utility.PushMessage(AppConfig.LineGroupToken, Message, AppConfig.LineToken);

                return(true);
            }
            catch (Exception ex)
            {
                LogGenerator.Add("LineBotMessage Error: " + ex.Message);
                return(false);
            }
        }
Пример #4
0
        public void Configuration(IAppBuilder app)
        {
            LogGenerator.Add("Owin startup");
            ConfigureAuth(app);
            AppConfig.LoadConfig();

            GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
            RecurringJob.AddOrUpdate(() => WorkScript.StartPingAsync(), "*/5 * * * *");
            RecurringJob.AddOrUpdate(() => WorkScript.StartSpeedTestAsync(), "*/30 * * * *");
            RecurringJob.AddOrUpdate(() => WorkScript.ClearOldDataAsync(), "0 0 * * *");

            app.UseHangfireServer();
            app.UseHangfireDashboard();
            LogGenerator.Add("Owin startup done.");
        }
Пример #5
0
        public static async Task ClearOldDataAsync()
        {
            DateTime Date = DateTime.Today.AddYears(-1);

            try
            {
                using (PingvalueModels db = new PingvalueModels())
                {
                    var PingOldData  = db.PingDatas.Where(c => c.CreateTime <= Date).ToList();
                    var SpeedOldData = db.SpeedTests.Where(c => c.TestTime <= Date).ToList();
                    db.PingDatas.RemoveRange(PingOldData);
                    db.SpeedTests.RemoveRange(SpeedOldData);
                    await db.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                LogGenerator.Add("Failed to clear old data: " + ex.Message);
            }
        }
Пример #6
0
        public static void LoadConfig()
        {
            try
            {
                string  ConfigContent = File.ReadAllText(ConfigFile);
                dynamic JsonObject    = JsonConvert.DeserializeObject(ConfigContent);

                LineToken         = JsonObject["LineToken"];
                LineRetornMessage = JsonObject["LineRetornMessage"];
                LineGroupToken    = JsonObject["LineGroupToken"];
            }
            catch (Exception ex)
            {
                LogGenerator.Add("Failed to load config: " + ex.Message);
                LineToken         = "";
                LineRetornMessage = "";
                LineGroupToken    = "";
                SaveConfig();
            }
        }
Пример #7
0
        public static async Task StartPingAsync()
        {
            DateTime        PingTime            = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            List <PingData> PingDatas           = new List <PingData>();
            List <string>   StatusChangeDevices = new List <string>();
            object          PingLock            = new object();

            try
            {
                using (PingvalueModels db = new PingvalueModels())
                {
                    Parallel.ForEach(await db.Devices.ToListAsync(), (device) =>
                    {
                        Ping pingSender  = new Ping();
                        long[] PingDelay = new long[5];
                        for (int i = 0; i < 5; i++)
                        {
                            IPAddress address = IPAddress.Parse(device.IPAddress);
                            PingReply reply   = pingSender.Send(address);
                            if (reply.Status == IPStatus.Success)
                            {
                                PingDelay[i] = reply.RoundtripTime;
                            }
                            else
                            {
                                PingDelay[i] = long.MaxValue;
                            }
                        }
                        pingSender.Dispose();
                        lock (PingLock)
                        {
                            if (PingDelay.Where(c => c != long.MaxValue).Count() > 0)
                            {
                                if (!device.IsOnline)
                                {
                                    device.IsOnline = !device.IsOnline;
                                    StatusChangeDevices.Add(
                                        "群組 :" + string.Join(",", device.DeviceGroups.Select(c => c.GroupName)) +
                                        " 設備 :" + device.DeviceName +
                                        " IP位置 :" + device.IPAddress +
                                        " 在 " + PingTime +
                                        " 恢復連線"
                                        );
                                    db.Entry(device).State = EntityState.Modified;
                                }
                            }
                            else
                            if (device.IsOnline)
                            {
                                device.IsOnline = !device.IsOnline;
                                StatusChangeDevices.Add(
                                    "群組 :" + string.Join(",", device.DeviceGroups.Select(c => c.GroupName)) +
                                    " 設備 :" + device.DeviceName +
                                    " IP位置 :" + device.IPAddress +
                                    " 在 " + PingTime +
                                    " 離線"
                                    );
                                db.Entry(device).State = EntityState.Modified;
                            }

                            PingDatas.Add(new PingData
                            {
                                Id         = Guid.NewGuid(),
                                CreateTime = PingTime,
                                Device     = device,
                                Delay1     = PingDelay[1],
                                Delay2     = PingDelay[2],
                                Delay3     = PingDelay[3],
                                Delay4     = PingDelay[4]
                            });
                        }
                    });
                    db.PingDatas.AddRange(PingDatas);
                    await db.SaveChangesAsync();
                }

                if (StatusChangeDevices.Count != 0)
                {
                    LineBotMessage(string.Join("\n", StatusChangeDevices));
                }
            }
            catch (Exception ex)
            {
                LogGenerator.Add("Failed to start ping: " + ex.Message);
            }
        }