public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ScheduledModel model = db.Schedules.Find(id); if (model == null) { return(HttpNotFound()); } return(PartialView("Delete", model)); }
public ActionResult Delete(int id) { ScheduledModel model = db.Schedules.Find(id); // Event SysEvent ev = new SysEvent(); ev.Action = Enums.Action.Info; ev.Description = "Deleted schedule: " + model.Name; ev.ActionStatus = ActionStatus.OK; LogsController.AddEvent(ev, User.Identity.GetUserId()); db.Schedules.Remove(model); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create() { ScheduledModel model = new ScheduledModel(); ViewBag.Hours = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }; ViewBag.Minutes = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; return(View(model)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ScheduledModel model = db.Schedules.Find(id); ViewBag.Hours = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }; ViewBag.Minutes = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; if (model == null) { return(HttpNotFound()); } return(View(model)); }
/// <summary> /// Scheduler (Helper) | Executes the schedule. /// </summary> /// <param name="id"></param> private static void ScheduleExecute(int id) { ApplicationDbContext db = new ApplicationDbContext(); ScheduledModel schedule = db.Schedules.SingleOrDefault(x => x.Id == id); List <int> computerIds = new List <int>(); if (schedule.Type == ScheduledType.Individual) { computerIds = JsonConvert.DeserializeObject <List <int> >(schedule.JsonComputerList); } else if (schedule.Type == ScheduledType.Color) { computerIds = db.Colors.SingleOrDefault(x => x.Id == schedule.ColorId).Computers.Select(x => x.Id).ToList(); } else if (schedule.Type == ScheduledType.Location) { computerIds = db.Locations.SingleOrDefault(x => x.Id == schedule.LocationId).Computers.Select(x => x.Id).ToList(); } else if (schedule.Type == ScheduledType.Type) { computerIds = db.ComputerTypes.SingleOrDefault(x => x.Id == schedule.TypeId).Computers.Select(x => x.Id).ToList(); } foreach (int cid in computerIds) { try { if (schedule.Action == ScheduledAction.Wakeup) { Core.Actions.SchedulerPowerOn(cid); } else if (schedule.Action == ScheduledAction.Reboot) { Core.Actions.SchedulerPowerRecycle(cid); } else if (schedule.Action == ScheduledAction.Shutdown) { Core.Actions.SchedulerPowerOff(cid); } } catch (Exception) { } } // Update database schedule.LastRun = DateTime.Now; db.Schedules.AddOrUpdate(schedule); db.SaveChanges(); // Call SignalR var context = GlobalHost.ConnectionManager.GetHubContext <LiveUpdatesHub>(); context.Clients.All.UpdateSchedules(id, schedule.LastRun); TimeSpan ts = DateTime.Now - schedule.LastRun; if (ts.TotalDays < 18250) { string lastRun = schedule.LastRun.ToShortDateString() + " " + schedule.LastRun.ToShortTimeString(); context.Clients.All.UpdateSchedules(id, lastRun); } else { string lastRun = "Never"; context.Clients.All.UpdateSchedules(id, lastRun); } }
public ActionResult Edit(ScheduledModel model) { if (ModelState.IsValid) { bool valid = true; // Get info if (model.Type == ScheduledType.Individual) { try { List <int> clist = JsonConvert.DeserializeObject <List <int> >(model.JsonComputerList); if (clist.Count == 0) { ModelState.AddModelError(String.Empty, "The computer list cannot be empty."); valid = false; } try { string names = "|"; foreach (var id in clist) { names += ", " + db.Computers.SingleOrDefault(x => x.Id == id).Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } catch (Exception) { ModelState.AddModelError(String.Empty, "The computer list cannot be empty."); valid = false; } } if (model.Type == ScheduledType.Color) { model.JsonComputerList = ""; if (model.ColorId == 0) { ModelState.AddModelError(String.Empty, "Plese select a color."); valid = false; } try { string names = "|"; ColorModel item = db.Colors.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.ColorId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (model.Type == ScheduledType.Location) { model.JsonComputerList = ""; if (model.LocationId == 0) { ModelState.AddModelError(String.Empty, "Plese select a location."); valid = false; } try { string names = "|"; LocationModel item = db.Locations.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.LocationId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (model.Type == ScheduledType.Type) { model.JsonComputerList = ""; if (model.TypeId == 0) { ModelState.AddModelError(String.Empty, "Plese select a Computer Type."); valid = false; } try { string names = "|"; ComputerTypeModel item = db.ComputerTypes.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.TypeId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (valid) { // Event SysEvent ev = new SysEvent(); ev.Action = Enums.Action.Info; ev.Description = "Edited schedule: " + model.Name; ev.ActionStatus = ActionStatus.OK; LogsController.AddEvent(ev, User.Identity.GetUserId()); model.LastRun = DateTime.Now.AddYears(-100); db.Entry(model).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } else { return(View(model)); } } return(View(model)); }
public ActionResult Create(ScheduledModel model) { ModelState.Clear(); bool valid = model.Name != null; model.LastRun = DateTime.Now.AddYears(-100); if (!valid) { ModelState.AddModelError(String.Empty, "Name cannot be empty."); } if (model.Type == ScheduledType.Individual) { try { List <int> clist = JsonConvert.DeserializeObject <List <int> >(model.JsonComputerList); if (clist.Count == 0) { ModelState.AddModelError(String.Empty, "The computer list cannot be empty."); valid = false; } try { string names = "|"; foreach (var id in clist) { names += ", " + db.Computers.SingleOrDefault(x => x.Id == id).Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } catch (Exception) { ModelState.AddModelError(String.Empty, "The computer list cannot be empty."); valid = false; } } if (model.Type == ScheduledType.Color) { model.JsonComputerList = ""; if (model.ColorId == 0) { ModelState.AddModelError(String.Empty, "Plese select a color."); valid = false; } try { string names = "|"; ColorModel item = db.Colors.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.ColorId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (model.Type == ScheduledType.Location) { model.JsonComputerList = ""; if (model.LocationId == 0) { ModelState.AddModelError(String.Empty, "Plese select a location."); valid = false; } try { string names = "|"; LocationModel item = db.Locations.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.LocationId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (model.Type == ScheduledType.Type) { model.JsonComputerList = ""; if (model.TypeId == 0) { ModelState.AddModelError(String.Empty, "Plese select a Computer Type."); valid = false; } try { string names = "|"; ComputerTypeModel item = db.ComputerTypes.Include(x => x.Computers).SingleOrDefault(x => x.Id == model.TypeId); foreach (var computer in item.Computers) { names += ", " + computer.Name; } model.ComputerListNames = names.Replace("|, ", ""); } catch (Exception) { } } if (valid) { db.Schedules.Add(model); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.Hours = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }; ViewBag.Minutes = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; return(View(model)); } }