// turn the device off public void TurnOff(SmartSwitchDbContext context) { using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { if (scope.Resolve <IWebsocketsServer>().TurnOff(Mac)) { IsOn = false; context.Entry(this).State = EntityState.Modified; context.SaveChanges(); } } }
public static void Execute(Operations op, string mac) { // when entering this function we need to execute the task Plug device; using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { SmartSwitchDbContext context = scope.Resolve <SmartSwitchDbContext>(); device = context.Plugs.Find(mac); Execute(op, device, context); } }
public static void Execute(Operations op, Plug device, SmartSwitchDbContext context) { switch (op) { case Operations.TurnOn: device.TurnOn(context); break; case Operations.TurnOff: device.TurnOff(context); break; } }
public static void ExecuteAndScheduleNextExecution(Operations operation, string mac, int repeatEvery, int taskId) { using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { SmartSwitchDbContext context = scope.Resolve <SmartSwitchDbContext>(); var tasks = context.Tasks; var task = tasks.Find(taskId); if (task == null) { throw new Exception("task is null"); } task.JobId = BackgroundJob.Schedule(() => ExecuteAndScheduleNextExecution(operation, mac, repeatEvery, taskId), TimeSpan.FromMinutes(repeatEvery)); context.SaveChanges(); Execute(operation, context.Plugs.Find(mac), context); } }
public void AddTask(Task task, SmartSwitchDbContext context) { Tasks.Add(task); context.SaveChanges(); task.Schedule(); }