Powershell class for Check

The class that is the basis of the implementation in Sample video of AutomationConnectIQ Check class operation.

Since the implementation of the class is a little long, first the module file (psm1) that defines the class is saved, then the script using the base class and how to use it, and finally the definition of the base class. ..

Where to save the class definition file

Save the class definition file in the path set in $ env: PSModulePath as module name / module name.psm1 and use it.


In my case, I saved it under % USERPROFILE%\Documents\PowerShell\Modules\ConnectIQDevelop.

how to use

Creating a derived class

using module ConnectIQDevelop
using namespace AutomationConnectIQ.Lib
Param($Key, $Jungle, $Dev, $Output)

class SimulatorAction : ConnectIQDevelop {
	[bool]Action([string]$dev, [Simulator]$simulator)
	{
		([ConnectIQDevelop]$this).Action($dev, $simulator)
		# Put the actual processing here
		return $true
	}
	[void]Pre([Simulator]$simulator) {
		([ConnectIQDevelop]$this).Pre($simulator)
		# Put the actual processing here
	}
}

$action = [SimulatorAction]::new()
$action.key = $Key
$action.project = $Jungle
$action.OutputDir = $Output
$action.Check($Dev)
remove-variable action

Create a script like the one above and pass four parameters. Start this as a task in VS Code.

It is the loading part of the class module defined by using module ConnectIQDevelop.
In SimulatorAction, it is derived from ConnectIQDevelop class defined in the loaded module, and Action / Pre processing part is implemented individually.
Specific examples of Action / Pre are Fr45Test and Pre in the Sample video of AutomationConnectIQ Check class operation.

Creating a task file for VS Code

The task file is described in the following format. The script is script / test.ps1.
Within this task, I’m trying to pass four parameters.

{
	"version": "2.0.0",
	"tasks": [
	{
		"label": "Build",
		"type": "shell",
		"command": "${workspaceFolder}/script/test.ps1",
		"problemMatcher": [
			"$monkeyc.error",
			"$monkeyc.fileWarning",
			"$monkeyc.genericWarning"
		],
		"args": [
                	"-Key", "${config:monkeyC.developerKeyPath}",
	                "-Jungle", "${workspaceFolder}/monkey.jungle",
        	        "-Output", "${workspaceFolder}/output/Current",
	                "-Dev", "fr45",
		],
		"presentation": {
			"echo": true,
			"reveal": "always",
			"focus": false,
			"panel": "shared",
			"showReuseMessage": true,
			"clear": false
		}
	},
	]
}

By pressing Ctrl + Alt + T in VS Code, the following options will be displayed and you will be able to execute PowerShell scripts.

Definition contents of ConnectIQDevelop class

using namespace AutomationConnectIQ.Lib

class ConnectIQDevelop {
	[string]$Device		# Build target device name
	[string]$OutputDir	# Data output destination folder name
	[Simulator]$Sim		# Object for simulator operation
	[TimeSimulator]$Time	# Object for time simulation operation
	[int]$Number		# Serial number of capture file name
	[string]$key		# Key file for build
	[string]$project	# Project file name

	# Capture, but add a comment after the number
	[void]Capture([string]$comment)
	{
		$this.Time.Action([TimeSimulator+ExecuteType]::Start)
		Start-sleep -Milliseconds 500
		$this.Time.Action([TimeSimulator+ExecuteType]::Pause)
	
		$bitmap = $this.Sim.Capture()
		$filename = $this.Device + "_" + $this.Number.ToString("D2") + "_" + $comment + ".png"
		$bitmap.Save($(Join-Path $this.OutputDir $filename))
		$this.Time.Action([TimeSimulator+ExecuteType]::Stop)
		$this.Number++	
	}

	[bool]Action([string]$dev, [Simulator]$simulator)
	{
		# Initialize variables for member use in in-class methods
		# To override Action, call this method with "([ConnectIQDevelop]$this).Action($dev, $simulator)"
		$this.Sim = $simulator
		$this.Device = $dev
		$this.Number = 0

		return $true	# CheckAll ends there if it returns with $ false
	}

	[void]Pre([Simulator]$simulator) {
	}

	[void]Simulate([string]$device) {
		$this.Device = $device
		$sdk = New-Object GarminSDK -Property @{
			Key = $this.key
		}
		$this.Sim = New-Object Simulator($sdk)
		$this.Sim.WaitForInput()
		$this.Action($device, $this.Sim)
	}

	[void]BuildAndLoad([string]$device) {
		$this.Device = $device
		$sdk = New-Object GarminSDK -Property @{
			Key = $this.key
		}
		$proj = New-Object Jungle($this.project)
		$sdk.BuildProgram($proj, $this.Device)

		$this.Sim = New-Object Simulator($sdk)
		$this.Sim.WaitForInput()

		$sdk.StartProgram($proj.DefaultProgramPath, $this.Device)
		$this.Sim.WaitForDeviceStart()
	}

	[void]Build([string]$device) {
		$this.Device = $device
		$sdk = New-Object GarminSDK -Property @{
			Key = $this.key
		}
		$proj = New-Object Jungle($this.project)
		$sdk.BuildProgram($proj, $this.Device)
	}

	[void]CheckAll() {
		if (-Not (Test-Path -Path $this.OutputDir)) {
			New-Item -Path $this.OutputDir -ItemType Directory
		}

		push-Location -Path $this.OutputDir
		[Environment]::CurrentDirectory = $pwd

		$check = New-Object Checker -Property @{
			Key = $this.key
			Project = $this.project
		}
		$check.Check($true, $this.Action, $this.Pre, $null)

		Pop-Location
		[Environment]::CurrentDirectory = $pwd
	}

	[void]Check([string]$device) {
		if (-Not (Test-Path -Path $this.OutputDir)) {
			New-Item -Path $this.OutputDir -ItemType Directory
		}

		push-Location -Path $this.OutputDir
		[Environment]::CurrentDirectory = $pwd

		$check = New-Object Checker -Property @{
			Key = $this.key
			Project = $this.project
		}
		$check.Check($device, $this.Action, $this.Pre)

		Pop-Location
		[Environment]::CurrentDirectory = $pwd
	}

	[void]UnitTest([string]$device) {
		if (-Not (Test-Path -Path $this.OutputDir)) {
			New-Item -Path $this.OutputDir -ItemType Directory
		}

		push-Location -Path $this.OutputDir
		[Environment]::CurrentDirectory = $pwd

		$check = New-Object Checker -Property @{
			Key = $this.key
			Project = $this.project
		}
		$check.UnitTest($device)

		Pop-Location
		[Environment]::CurrentDirectory = $pwd
	}
}

コメント

Copied title and URL