LavishScript:Task Types

From Lavish Software Wiki
Jump to navigation Jump to search

A Task Type defines a routine to process a Task.

For a Task defined as...

{
 "type": "ls1.echo",
 "output":"Hello World!"}
}

... the "ls1.echo" corresponds to a Task Type that is pre-defined by LavishScript. The Task Type is responsible for interpreting the rest of the Task, which in this case indicates that it should output "Hello World!".


Task Type Properties

Task Types are defined by a JSON object with these properties, and registered via lavishmachine.NewTaskType or other available methods (such as via Package loading)

Task Type properties
name (required) The Name of the Task Type
object (required) A globally-accessible LavishScript object
method (required) A method to execute within the LavishScript object, for this Task
schema (optional) A JSON Schema which can be used to validate Tasks for this Task Type, provide GUI configuration to properly define a Task, as well as providing auto-completion for many text editors


Creating a Task Type

Task Types can be defined by LavishScript:Scripts.

To add a Task Type requires a globally-accessible LavishScript Object, e.g. defined by an objectdef and instantiated with variable(global), with a method to handle a recurring "pulse" to process the Task. The method needs no parameters, as the Context TLO will be a taskpulseargs.

Here we provide a template you can follow! Also found at gist.github.com
objectdef mytasks_Controller
{
  variable taskmanager TaskManager=${LMAC.NewTaskManager["mytaskstest"]}

	method Initialize()
	{
		This:AddTaskType["{\"name\":\"my first task type\",\"object\":\"MyTasks\",\"method\":\"Task_Template\"}"]
	}

	method Shutdown() 
	{ 
    		LMAC.TaskManager["mytaskstest"]:Destroy
	}

	method AddTaskType(string jsonData)
	{
		variable int64 id
		id:Set[${LMAC.NewTaskType["${jsonData.Escape}"].ID}]
		if ${id}
		{
			echo "LMAC: Task Type ${id} added: ${LMAC.TaskType[${id}].Name.Escape}"
		}
	}

	method Task_Template()
	{		
		switch ${Context.TaskState}
		{
		case Start
			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		case Continue
; 			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		case Stop
			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		}
	}
  
  ; begin a test!
 method Start()
 {
    TaskManager:BeginTask["{\"type\":\"my first task type\",\"duration\":1.0,\"my setting\":\"my setting value\"}"]
 }
}

variable(global) mytasks_Controller MyTasks

function main()
{
	MyTasks:Start
	while 1
	{
		waitframe
	}
}

atom atexit()
{
}

This example can be saved as, for example, mytasks.iss.

Output

Running the script will produce approximately this output (your timestamps and IDs will likely differ from this example):

LMAC: Task Type 161 added: my first task type
taskpulseargs 271000984 0 164 Start instant=FALSE {"duration":1.000000,"my setting":"my setting value","type":"my first task type"}
taskpulseargs 271001984 1000 164 Stop instant=FALSE {"duration":1.000000,"my setting":"my setting value","type":"my first task type"}


Implementing a Task Type

Example 1: Echo

In our example above using the Task_Template method, the Task itself does nothing other than output its state information for testing purposes.

The "ls1.echo" Task could be implemented as follows:

method Task_Template()
{
  echo ${Context.Task.Args[output]}
  Context.Task:Stop
} 

The Task -- to echo the "output" value -- is performed, and then the Task is immediately Stopped. This Task Type is then effectively instant, ignoring any specified duration.


Example 2: Keystroke

In this example, a Keystroke Task uses the press command to hold a combination of keys specified as "combo" in the Task. For example, if this were registered as a "keystroke" Task Type...

	method Task_Keystroke()
	{
		switch ${Context.TaskState}
		{
		case Start
			press -hold "${Context.Task.Args[combo].Escape[0]}"
			break
		case Continue
			break
		case Stop
			press -release "${Context.Task.Args[combo].Escape[0]}"
			break
		}
	}

... then we could use the following Task to press Alt+1 for 1.0 seconds.

{
  "type":"keystroke",
  "combo":"Alt+1",
  "duration":1.0
}