Introducing what you can create a class with PowerShell and make effective use of the classes introduced so far.
SimulatorAction Class
The parts to be modified by each individual are the Action and Pre functions.
You can create a derivative of SimulatorAction, override Action and Pre, or modify this class directly.
using namespace AutomationConnectIQ.Lib;
class SimulatorAction {
[string]$Device
[string]$OutputDir
[Simulator]$Sim
[TimeSimulator]$Time
[int]$Number
[string]$key
[string]$project
[void]Capture()
{
$this.Time.Action([TimeSimulator+ExecuteType]::Start)
Start-sleep -Milliseconds 500
$this.Time.Action([TimeSimulator+ExecuteType]::Pause)
$bitmap = $this.Sim.Capture()
$filename = $this.Number.ToString("D2") + ".png"
$bitmap.Save($(Join-Path $this.OutputDir $this.Device $filename))
$this.Time.Action([TimeSimulator+ExecuteType]::Stop)
$this.Number++
}
[bool]Action([string]$dev, [Simulator]$simulator)
{
$this.Sim = $simulator
$this.Device = $dev
$this.Number = 0
if (-Not (Test-Path -Path $this.OutputDir)) {
New-Item -Path $this.OutputDir -ItemType Directory
}
if (-not (Test-Path $(Join-Path $this.OutputDir $this.Device))) {
New-Item -Path $(Join-Path $this.OutputDir $this.Device) -ItemType Directory
}
$simulator.SetGPSPosition(35.685233, 139.752485)
$simulator.SetLanguage([Simulator+Language]::English)
$this.Time = $simulator.CreateTime()
$this.Time.Open()
$this.Time.Time = Get-Date "2019-12-31 13:59:59"
$this.Capture()
$this.Time.Close()
return $true # CheckAll ends there if it returns with $ false
}
[void]Pre([Simulator]$simulator)
{
$this.Sim = $simulator
$simulator.SetBatteryStatus(50, $false)
}
[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]CheckAll() {
$check = New-Object Checker -Property @{
Key = $this.key
Project = $this.project
}
$check.Check($true, $this.Action, $this.Pre, $null)
}
}
$action = [SimulatorAction]::new()
$action.key = "developer_key"
$action.project = "monkey.jungle"
$action.OutputDir = "output"
$action.CheckAll()
Line 12: Capture function
A little improvement of AutomationConnectIQ-Sample 7 is made so that the save destination folder is the OutputDir device name and the numbered png file is saved under it.
Also, every time I call Capture, the number is counted up.
Line 25:Action function
It is a function to be passed to the Check method of the Checker class shown in AutomationConnectIQ-Sample 8 and AutomationConnectIQ-Sample 9.
Lines 27-29 initialize the variables used in SimulatorAction.
Line 50:Pre function
It is a function to perform pre-processing to be passed to the Check method of the Checker class shown in AutomationConnectIQ-Sample 9.
Line 66:BuildAndLoad function
By passing the device name, the program of the device is built, the simulator is started, and the process of loading the program is performed.
Line 56:Simulate function
Call the Action function to perform simulation processing.
Line 79:CheckAll function
Call a method that checks all devices in the Checker class.
How to Use
The contents of the Action function and Pre function are changed, but it seems better to create the contents of the Action function by the following procedure.
- Start the simulator with the BuildAndLoad function.
- After changing the Action function, call the Simulate function.
- After creating the Action and Pre functions, call the CheckAll function.
It seems that it is better to use Visual Studio Code to edit PowerShell scripts with less effort.
If you follow the above steps, you can check by changing line 93 of the code and doing F5 (execution).
In Capture of SimulatorAction, folders are created for each device, so it may be a little lacking in listability. In that case, I think it’s a good idea to change the Capture so that the capture file is saved under a single folder.
In that case, it is necessary to modify the process of creating the save destination folder on the 31st to 36th lines at the same time.
コメント