Skip to content

Commit

Permalink
Added Stopwatch Command
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Apr 19, 2019
1 parent 95ba841 commit 20e96af
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 0 deletions.
1 change: 1 addition & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(GetDataCommand))]
[XmlInclude(typeof(RESTCommand))]
[XmlInclude(typeof(ParseJSONArrayCommand))]
[XmlInclude(typeof(StopwatchCommand))]
public abstract class ScriptCommand
{
[XmlAttribute]
Expand Down
173 changes: 173 additions & 0 deletions taskt/Core/Automation/Commands/StopwatchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Engine Commands")]
[Attributes.ClassAttributes.Description("This command allows you to stop a program or a process.")]
[Attributes.ClassAttributes.UsesDescription("Use this command to close an application by its name such as 'chrome'. Alternatively, you may use the Close Window or Thick App Command instead.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'Process.CloseMainWindow'.")]
public class StopwatchCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Enter the instance name of the Stopwatch")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Provide a unique instance or way to refer to the stopwatch")]
[Attributes.PropertyAttributes.SampleUsage("**myStopwatch**, **{vStopWatch}**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_StopwatchName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Enter the Stopwatch Action")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Provide a unique instance or way to refer to the stopwatch")]
[Attributes.PropertyAttributes.SampleUsage("**myStopwatch**, **{vStopWatch}**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_StopwatchAction { get; set; }


[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Apply Result To Variable")]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_userVariableName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Specify String Format")]
[Attributes.PropertyAttributes.InputSpecification("Specify if a specific string format is required.")]
[Attributes.PropertyAttributes.SampleUsage("MM/dd/yy, hh:mm, etc.")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_ToStringFormat { get; set; }

[XmlIgnore]
[NonSerialized]
public ComboBox StopWatchComboBox;

[XmlIgnore]
[NonSerialized]
public List<Control> MeasureControls;

public StopwatchCommand()
{
this.CommandName = "StopwatchCommand";
this.SelectionName = "Stopwatch";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_StopwatchName = "default";
this.v_StopwatchAction = "Start Stopwatch";
}

public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var instanceName = v_StopwatchName.ConvertToUserVariable(sender);
System.Diagnostics.Stopwatch stopwatch;

var action = v_StopwatchAction.ConvertToUserVariable(sender);

switch (action)
{
case "Start Stopwatch":
//start a new stopwatch
stopwatch = new System.Diagnostics.Stopwatch();
engine.AddAppInstance(instanceName, stopwatch);
stopwatch.Start();
break;
case "Stop Stopwatch":
//stop existing stopwatch
stopwatch = (System.Diagnostics.Stopwatch)engine.AppInstances[instanceName];
stopwatch.Stop();
break;
case "Restart Stopwatch":
//restart which sets to 0 and automatically starts
stopwatch = (System.Diagnostics.Stopwatch)engine.AppInstances[instanceName];
stopwatch.Restart();
break;
case "Reset Stopwatch":
//reset which sets to 0
stopwatch = (System.Diagnostics.Stopwatch)engine.AppInstances[instanceName];
stopwatch.Reset();
break;
case "Measure Stopwatch":
//check elapsed which gives measure
stopwatch = (System.Diagnostics.Stopwatch)engine.AppInstances[instanceName];
string elapsedTime;
if (string.IsNullOrEmpty(v_ToStringFormat))
{
elapsedTime = stopwatch.Elapsed.ToString();
}
else
{
var format = v_ToStringFormat.ConvertToUserVariable(sender);
elapsedTime = stopwatch.Elapsed.ToString(format);
}

elapsedTime.StoreInUserVariable(sender, v_userVariableName);

break;
default:
throw new NotImplementedException("Stopwatch Action '" + action + "' not implemented");
}



}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_StopwatchName", this, editor));

var StopWatchComboBoxLabel = CommandControls.CreateDefaultLabelFor("v_StopwatchAction", this);
StopWatchComboBox = (ComboBox)CommandControls.CreateDropdownFor("v_StopwatchAction", this);
StopWatchComboBox.DataSource = new List<string> { "Start Stopwatch", "Stop Stopwatch", "Restart Stopwatch", "Reset Stopwatch", "Measure Stopwatch" };
StopWatchComboBox.SelectedIndexChanged += StopWatchComboBox_SelectedValueChanged;
RenderedControls.Add(StopWatchComboBoxLabel);
RenderedControls.Add(StopWatchComboBox);

//create controls for user variable
MeasureControls = CommandControls.CreateDefaultDropdownGroupFor("v_userVariableName", this, editor);

//load variables for selection
var comboBox = (ComboBox)MeasureControls[1];
comboBox.AddVariableNames(editor);

MeasureControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ToStringFormat", this, editor));

foreach (var ctrl in MeasureControls)
{
ctrl.Visible = false;
}
RenderedControls.AddRange(MeasureControls);

return RenderedControls;
}

private void StopWatchComboBox_SelectedValueChanged(object sender, EventArgs e)
{

if (StopWatchComboBox.SelectedValue.ToString() == "Measure Stopwatch")
{
foreach (var ctrl in MeasureControls)
ctrl.Visible = true;

}
else {
foreach (var ctrl in MeasureControls)
ctrl.Visible = false;
}

}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Action: " + v_StopwatchAction + ", Name: " + v_StopwatchName + "]";
}
}
}
10 changes: 10 additions & 0 deletions taskt/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions taskt/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,7 @@
<data name="action_bar_close" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\action_bar_close.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="command_stopwatch" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\command-stopwatch.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added taskt/Resources/command-stopwatch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="64565e14-001e-4b0e-a585-b216e8d187d6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="1) Start/Stop and show elapsed time" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="ffac2e5c-79e2-4873-8df2-898193928285" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Start Stopwatch" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="PauseCommand" CommandID="dfdfd903-cfe6-402a-901f-f299242154ce" CommandName="PauseCommand" IsCommented="false" SelectionName="Pause Script" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_PauseLength="3000" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="1aaf0c9e-b916-4b17-aaa7-7daf9aba8250" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Stop Stopwatch" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="94a39fd2-2c8f-4cf0-abfc-b58c80ca58c9" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Measure Stopwatch" v_userVariableName="vElapsed" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="2ecd7e12-beb6-45c5-8c2a-e72460954031" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vElapsed}" v_AutoCloseAfter="0" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="8481f5d8-f61e-4a5c-888c-de37b18d4825" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" v_Comment="2) Restart/Stop and show elapsed time" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="09638194-46a7-49e8-a615-64a020e0930e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="8" PauseBeforeExeucution="false" v_Comment="Note: Restart sets to zero and starts immediately" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="294a8213-50ba-4265-9432-6325d029dee8" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="9" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Restart Stopwatch" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="PauseCommand" CommandID="a13ff69e-04ab-42b2-b2af-758b80e594f8" CommandName="PauseCommand" IsCommented="false" SelectionName="Pause Script" DefaultPause="0" LineNumber="10" PauseBeforeExeucution="false" CommandEnabled="true" v_PauseLength="4000" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="8933f34d-b041-40ce-b173-5c8f87936ca9" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="11" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Stop Stopwatch" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="4430415c-d2ae-4d2b-b7f0-e37c1270b0aa" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="12" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Measure Stopwatch" v_userVariableName="vElapsed" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="9e56bd23-317d-4e91-a335-19ac9a1704e3" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="13" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vElapsed}" v_AutoCloseAfter="0" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="22e6aad7-3349-49f7-9735-6528ad12f33e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="14" PauseBeforeExeucution="false" v_Comment="3) Reset Stopwatch and show zero'd time" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="f6186120-e582-4e10-918b-bece34eb47a6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="15" PauseBeforeExeucution="false" v_Comment="Note: Reset sets to zero and does not start automatically" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="7adfacc6-11b1-4ec2-abf4-b54a60d3c339" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="16" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Reset Stopwatch" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="PauseCommand" CommandID="c8d3cb98-0223-4fdd-b595-a42a324b64eb" CommandName="PauseCommand" IsCommented="false" SelectionName="Pause Script" DefaultPause="0" LineNumber="17" PauseBeforeExeucution="false" CommandEnabled="true" v_PauseLength="2000" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="StopwatchCommand" CommandID="a4bb34f1-d75c-4b05-852a-c20525115680" CommandName="StopwatchCommand" IsCommented="false" SelectionName="Stopwatch" DefaultPause="0" LineNumber="18" PauseBeforeExeucution="false" CommandEnabled="true" v_StopwatchName="default" v_StopwatchAction="Measure Stopwatch" v_userVariableName="vElapsed" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="0aaa71ba-5e85-49b7-91c0-21e74897cae1" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="19" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vElapsed}" v_AutoCloseAfter="0" />
</ScriptAction>
</Commands>
<Variables>
<ScriptVariable>
<VariableName>vElapsed</VariableName>
<VariableValue xsi:type="xsd:string"></VariableValue>
</ScriptVariable>
</Variables>
</Script>
1 change: 1 addition & 0 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ public static Dictionary<string, Image> UIImageDictionary()
uiImages.Add("ParseJsonArrayCommand", taskt.Properties.Resources.command_parse);
uiImages.Add("UploadDataCommand", taskt.Properties.Resources.command_server);
uiImages.Add("GetDataCommand", taskt.Properties.Resources.command_server);
uiImages.Add("StopwatchCommand", taskt.Properties.Resources.command_stopwatch);
return uiImages;
}
public static ImageList UIImageList()
Expand Down
2 changes: 2 additions & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<Compile Include="Core\Automation\Commands\SetWindowStateCommand.cs" />
<Compile Include="Core\Automation\Commands\SMTPSendEmailCommand.cs" />
<Compile Include="Core\Automation\Commands\StartProcessCommand.cs" />
<Compile Include="Core\Automation\Commands\StopwatchCommand.cs" />
<Compile Include="Core\Automation\Commands\StopProcessCommand.cs" />
<Compile Include="Core\Automation\Commands\WaitForWindowCommand.cs" />
<Compile Include="Core\Automation\Commands\HTMLInputCommand.cs" />
Expand Down Expand Up @@ -612,6 +613,7 @@
<None Include="Resources\taskt-command-helper.png" />
<None Include="Resources\action_bar_restart.png" />
<None Include="Resources\action_bar_close.png" />
<None Include="Resources\command-stopwatch.png" />
<Content Include="Resources\taskt-ExtractPDFText.jar">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down

0 comments on commit 20e96af

Please sign in to comment.