示例#1
0
        public async Task <ActionResult> Create([Bind(Include = "GroupName")] CreateDeviceGroupViewModel deviceGroup)
        {
            if (ModelState.IsValid)
            {
                db.DeviceGroups.Add(new DeviceGroup {
                    Id        = Guid.NewGuid(),
                    GroupName = deviceGroup.GroupName
                });
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(deviceGroup));
        }
示例#2
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);
            }
        }
示例#3
0
        public async Task <ActionResult> Create(CreateDeviceViewModel device, params Guid[] selectedGroups)
        {
            if (ModelState.IsValid)
            {
                selectedGroups = selectedGroups ?? new Guid[] { };

                db.Devices.Add(new Device {
                    Id           = Guid.NewGuid(),
                    DeviceName   = device.DeviceName,
                    IPAddress    = device.IPAddress,
                    IsOnline     = false,
                    DeviceGroups = await db.DeviceGroups.Where(c => selectedGroups.Contains(c.Id)).ToListAsync()
                });
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(device));
        }
示例#4
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);
            }
        }
示例#5
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);
            }
        }