private void InitializePotentialCommand(PotentialCommand potentialCommand, bool isStored) { Type executionEventType; if (_executionEvents.TryGetValue(potentialCommand.ExecutionEvent.Id, out executionEventType)) { var executionEvent = (IExecutionEvent)Activator.CreateInstance(executionEventType); executionEvent.Initialize(potentialCommand.ExecutionEvent.Parameter); if (!executionEvent.CanExecute) { if (!isStored) { _dynamicCommandStore.AddStoredCommand(potentialCommand); } executionEvent.TheTimeHasCome += (sender, args) => { _executePotentialCommandDelegate(potentialCommand); _dynamicCommandStore.RemoveStoredCommand(potentialCommand); }; return; } } _dynamicCommandStore.RemoveStoredCommand(potentialCommand); _executePotentialCommandDelegate(potentialCommand); }
public void StopActiveCommand(int commandId) { lock (_activeCommandsLock) { var activeCommand = ActiveCommands.Where(x => x.Key.CallbackId == commandId).ToList(); if (activeCommand.Any()) { var entry = activeCommand.FirstOrDefault(); entry.Value.StopExecute(); _dynamicCommandStore.RemoveStoredCommand(entry.Key); _activeCommandStopScheduler.CommandManualStop(entry.Key); } } }
public bool ExecuteActiveCommand(PotentialCommand potentialCommand, ActiveStaticCommand activeStaticCommand) { if (potentialCommand.StopEvent == null) { return(true); } lock (_settingsLock) switch (potentialCommand.StopEvent.Id) { case 1: //DurationStopEvent var sessionInfo = _stopSchedulerSettings.Sessions.FirstOrDefault( x => x.CommandId == potentialCommand.CallbackId); var durationInfo = _stopSchedulerSettings.DurationStopEventInfos.FirstOrDefault( x => x.CommandId == potentialCommand.CallbackId); var duration = TimeSpan.FromTicks(BitConverter.ToInt64(potentialCommand.StopEvent.Parameter, 0)); if (sessionInfo != null) { //if the difference is greater than 1 min. We dont directly compare because there might be a very small difference which comes from dividing the seconds in StartupTime if (Math.Abs((StartupTime - sessionInfo.StartupTime).TotalSeconds) > 60) { //we dont execute this command again in a different session _stopSchedulerSettings.Sessions.Remove(sessionInfo); if (durationInfo != null) { _stopSchedulerSettings.DurationStopEventInfos.Remove(durationInfo); } SaveSchedulerSettings(); _dynamicCommandStore.RemoveStoredCommand(potentialCommand); return(false); } if (durationInfo != null) { _stopSchedulerSettings.DurationStopEventInfos.Remove(durationInfo); duration = DateTime.Now - durationInfo.StartTime; if (duration < TimeSpan.Zero) { _stopSchedulerSettings.Sessions.Remove(sessionInfo); SaveSchedulerSettings(); _dynamicCommandStore.RemoveStoredCommand(potentialCommand); return(false); } } } else { _stopSchedulerSettings.Sessions.Add(new SessionCommandInfo { CommandId = potentialCommand.CallbackId, StartupTime = StartupTime }); } _stopSchedulerSettings.DurationStopEventInfos.Add(new DurationStopEventInfo { CommandId = potentialCommand.CallbackId, StartTime = DateTime.Now }); SaveSchedulerSettings(); new Timer(StopCommandCallback, activeStaticCommand, duration, TimeSpan.FromMilliseconds(Timeout.Infinite)); return(true); case 2: //DateTimeStopEvent var dateTime = DateTime.FromBinary(BitConverter.ToInt64(potentialCommand.StopEvent.Parameter, 0)); var timeSpan = dateTime - DateTime.UtcNow; if (timeSpan < TimeSpan.Zero) { _dynamicCommandStore.RemoveStoredCommand(potentialCommand); return(false); } new Timer(StopAndRemoveCommandCallback, new KeyValuePair <PotentialCommand, ActiveStaticCommand>(potentialCommand, activeStaticCommand), timeSpan, TimeSpan.FromMilliseconds(-1)); break; case 3: //ShutdownStopEvent sessionInfo = _stopSchedulerSettings.Sessions.FirstOrDefault( x => x.CommandId == potentialCommand.CallbackId); if (sessionInfo != null) { //if the difference is greater than 1 min. We dont directly compare because there might be a very small difference which comes from dividing the seconds in StartupTime if (Math.Abs((StartupTime - sessionInfo.StartupTime).TotalSeconds) > 60) { //we dont execute this command again in a different session _stopSchedulerSettings.Sessions.Remove(sessionInfo); SaveSchedulerSettings(); _dynamicCommandStore.RemoveStoredCommand(potentialCommand); return(false); } } else { _stopSchedulerSettings.Sessions.Add(new SessionCommandInfo { CommandId = potentialCommand.CallbackId, StartupTime = StartupTime }); SaveSchedulerSettings(); } return(true); default: return(true); } return(true); }