internal void RecordCommand(DynCmd.RecordableCommand command) { // In the playback mode 'this.recordedCommands' will be // 'null' so that the incoming command will not be recorded. if (null != recordedCommands) { if (command.Redundant && (recordedCommands.Count > 0)) { // If a command is being marked "Redundant", then we will // only be interested in the most recent one. If we already // have another instance recorded immediately prior to this, // then replace the old instance with the new (for details, // see "RecordableCommand.Redundant" property). // var previousCommand = recordedCommands.Last(); if (previousCommand.GetType() != command.GetType()) { recordedCommands.Add(command); } else { // Replace the existing command instead of adding. recordedCommands[recordedCommands.Count - 1] = command; } } else { recordedCommands.Add(command); } } }
private bool LoadCommandFromFile(string commandFilePath) { if (string.IsNullOrEmpty(commandFilePath)) { return(false); } if (File.Exists(commandFilePath) == false) { return(false); } if (null != loadedCommands) { throw new InvalidOperationException( "Internal error: 'LoadCommandFromFile' called twice"); } try { // Attempt to load the XML from the specified path. XmlDocument document = new XmlDocument(); document.Load(commandFilePath); // Get to the root node of this Xml document. XmlElement commandRoot = document.FirstChild as XmlElement; if (null == commandRoot || (null == commandRoot.ChildNodes)) { return(false); } // Read in optional attributes from the command root element. XmlElementHelper helper = new XmlElementHelper(commandRoot); this.ExitAfterPlayback = helper.ReadBoolean(ExitAttribName, true); this.PauseAfterPlayback = helper.ReadInteger(PauseAttribName, 10); this.CommandInterval = helper.ReadInteger(IntervalAttribName, 20); loadedCommands = new List <DynCmd.RecordableCommand>(); foreach (XmlNode node in commandRoot.ChildNodes) { DynCmd.RecordableCommand command = null; command = DynCmd.RecordableCommand.Deserialize(node as XmlElement); if (null != command) { loadedCommands.Add(command); } } } catch (Exception) { // Something is wrong with the Xml file, invalidate the // data member that points to it, and return from here. return(false); } // Even though the Xml file can properly be loaded, it is still // possible that the loaded content did not result in any useful // commands. In this case simply return false, indicating failure. // return(null != loadedCommands && (loadedCommands.Count > 0)); }
private void OnPlaybackTimerTick(object sender, EventArgs e) { DispatcherTimer timer = sender as DispatcherTimer; timer.Stop(); // Stop the timer before command completes. if (loadedCommands.Count <= 0) // There's nothing else for playback. { if (this.ExitAfterPlayback == false) { // The playback is done, but the command file indicates that // Dynamo should not be shutdown after the playback, so here // we simply invalidate the timer. // this.playbackTimer = null; } else { // The command file requires Dynamo be shutdown after all // commands has been played back. If that's the case, we'll // reconfigure the callback to a shutdown timer, and then // change its interval to the duration specified earlier. // this.playbackTimer.Tick -= OnPlaybackTimerTick; this.playbackTimer.Tick += OnShutdownTimerTick; var interval = TimeSpan.FromMilliseconds(PauseAfterPlayback); this.playbackTimer.Interval = interval; this.playbackTimer.Start(); // Start shutdown timer. } return; } // Remove the first command from the loaded commands. DynCmd.RecordableCommand nextCommand = loadedCommands[0]; loadedCommands.RemoveAt(0); // Execute the command, this may take a while longer than the timer // inverval (usually very short), that's why the timer was stopped // before the command execution starts. After the command is done, // the timer is then resumed for the next command in queue. // nextCommand.Execute(this.owningViewModel); timer.Start(); }
private void OnPlaybackTimerTick(object sender, EventArgs e) { DispatcherTimer timer = sender as DispatcherTimer; timer.Stop(); // Stop the timer before command completes. if (loadedCommands.Count <= 0) // There's nothing else for playback. { if (this.ExitAfterPlayback == false) { // The playback is done, but the command file indicates that // Dynamo should not be shutdown after the playback, so here // we simply invalidate the timer. // this.playbackTimer = null; ChangeStateInternal(State.Stopped); } else { // The command file requires Dynamo be shutdown after all // commands has been played back. If that's the case, we'll // reconfigure the callback to a shutdown timer, and then // change its interval to the duration specified earlier. // this.playbackTimer.Tick -= OnPlaybackTimerTick; this.playbackTimer.Tick += OnShutdownTimerTick; var interval = TimeSpan.FromMilliseconds(PauseAfterPlayback); this.playbackTimer.Interval = interval; this.playbackTimer.Start(); // Start shutdown timer. ChangeStateInternal(State.ShuttingDown); } return; } // Remove the first command from the loaded commands. DynCmd.RecordableCommand nextCommand = loadedCommands[0]; loadedCommands.RemoveAt(0); // Update the cached command references. this.PreviousCommand = this.CurrentCommand; this.CurrentCommand = nextCommand; if (nextCommand is DynCmd.PausePlaybackCommand) { var command = nextCommand as DynCmd.PausePlaybackCommand; PauseCommandPlayback(command.PauseDurationInMs); return; } try { // Execute the command, this may take a while longer than the timer // inverval (usually very short), that's why the timer was stopped // before the command execution starts. After the command is done, // the timer is then resumed for the next command in queue. // nextCommand.Execute(this.owningViewModel); } catch (Exception exception) { // An exception is thrown while playing back a command. Remove any // pending commands and allow the "playbackTimer" to continue with // its next tick. Proper shutdown sequence will be initialized // when the "playbackTimer" tries to pick up the next command and // realized that there is no more commands waiting. // loadedCommands.Clear(); this.PlaybackException = exception; } timer.Start(); }