public async Task <ActionResult> Edit([Bind(Include = "Id,GroupName")] EditDeviceGroupViewModel NewDeviceGroup) { if (ModelState.IsValid) { DeviceGroup deviceGroup = await db.DeviceGroups.FindAsync(NewDeviceGroup.Id); if (deviceGroup == null) { return(HttpNotFound()); } deviceGroup.GroupName = NewDeviceGroup.GroupName; db.Entry(deviceGroup).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(NewDeviceGroup)); }
public async Task <ActionResult> Edit([Bind(Include = "Id,DeviceName,IPAddress")] EditeDeviceViewModel NewDevice, params Guid[] selectedGroups) { if (ModelState.IsValid) { Device device = await db.Devices.FindAsync(NewDevice.Id); if (device == null) { return(HttpNotFound()); } device.DeviceName = NewDevice.DeviceName; device.IPAddress = NewDevice.IPAddress; device.DeviceGroups.Clear(); selectedGroups = selectedGroups ?? new Guid[] { }; var ExceptGroups = db.DeviceGroups.Where(c => selectedGroups.Contains(c.Id)).Except(device.DeviceGroups); foreach (DeviceGroup Group in ExceptGroups) { if (selectedGroups.Contains(Group.Id)) { device.DeviceGroups.Add(Group); } else { device.DeviceGroups.Remove(Group); } } db.Entry(device).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(NewDevice)); }
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); } }