Get(), Set($device), Clear()

methods added to IODevice

manual and pester tests pass for all kinds
This commit is contained in:
pblivingston 2026-03-04 04:23:10 -05:00
parent 4ea371af2f
commit 7d9615d760
4 changed files with 174 additions and 24 deletions

View File

@ -106,6 +106,14 @@ class BusDevice : IODevice {
[string] identifier () { [string] identifier () {
return 'Bus[' + $this.index + '].Device' return 'Bus[' + $this.index + '].Device'
} }
[int] EnumCount () {
return $this.remote.GetOutputCount()
}
[PSObject] EnumDevice ([int]$eIndex) {
return $this.remote.GetOutputDevice($eIndex)
}
} }
function Make_Buses ([Object]$remote) { function Make_Buses ([Object]$remote) {

View File

@ -118,30 +118,79 @@ class IODevice : IRemote {
} }
} }
[int] EnumCount () {
throw [System.NotImplementedException]::new("$($this.GetType().Name) must override EnumCount()")
}
[PSObject] EnumDevice ([int]$eIndex) {
throw [System.NotImplementedException]::new("$($this.GetType().Name) must override EnumDevice()")
}
[PSObject] Get () {
$device = [PSCustomObject]@{
Driver = $this.driver
Name = $this.name
HardwareId = ''
IsOutput = $this.kindOfDevice -eq 'Output'
}
if (-not [string]::IsNullOrEmpty($device.Name)) {
for ($i = 0; $i -lt $this.EnumCount(); $i++) {
$eDevice = $this.EnumDevice($i)
if ($eDevice.Name -eq $device.Name -and $eDevice.Driver -eq $device.Driver) {
$device = $eDevice
break
}
}
}
return $device
}
[void] Set ([PSObject]$device) {
$v = $device.IsOutput -eq ($this.kindOfDevice -eq 'Output')
$d = $device.Driver
$n = $device.Name
if ($v -and $d -is [string] -and $n -is [string]) {
if ($d -eq '' -and $n -eq '') {
$this.Clear()
return
}
if ($d -in $this.drivers.Values) {
$this.Setter($d, $n)
return
}
}
Write-Warning "Invalid device object provided to Set method."
}
[void] Clear () {
$this.Setter('mme', '')
}
hidden $_driver = $($this | Add-Member ScriptProperty 'driver' ` hidden $_driver = $($this | Add-Member ScriptProperty 'driver' `
{ {
if ([string]::IsNullOrEmpty($this.name)) { return '' } if ([string]::IsNullOrEmpty($this.name)) { return '' }
$type = $null $type = $null
try { try {
$tmp = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "vmrtmp-$(New-Guid).xml") $tmp = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "vmrtmp-$(New-Guid).xml")
$this.remote.Setter('Command.Save', $tmp) $this.remote.Setter('Command.Save', $tmp)
$timeout = New-TimeSpan -Seconds 2 $timeout = New-TimeSpan -Seconds 2
$sw = [Diagnostics.Stopwatch]::StartNew() $sw = [Diagnostics.Stopwatch]::StartNew()
$line = $null $line = $null
do { do {
if (Test-Path $tmp) { if (Test-Path $tmp) {
try { try {
$line = Get-Content $tmp | Select-String -Pattern "<$($this.kindOfDevice)Dev index='$($this.index + 1)'" -List $line = Get-Content $tmp | Select-String -Pattern "<$($this.kindOfDevice)Dev index='$($this.index + 1)'" -List
if ($line) { break } if ($line) { break }
} }
catch {} catch {}
} }
Start-Sleep -Milliseconds 20 Start-Sleep -Milliseconds 20
} while ($sw.elapsed -lt $timeout) } while ($sw.elapsed -lt $timeout)
if ($line -and $line.ToString() -match "type='(?<type>\d+)'") { if ($line -and $line.ToString() -match "type='(?<type>\d+)'") {
$type = $matches['type'] $type = $matches['type']
} }
} }
finally { finally {

View File

@ -161,6 +161,14 @@ class StripDevice : IODevice {
[string] identifier () { [string] identifier () {
return 'Strip[' + $this.index + '].Device' return 'Strip[' + $this.index + '].Device'
} }
[int] EnumCount () {
return $this.remote.GetInputCount()
}
[PSObject] EnumDevice ([int]$eIndex) {
return $this.remote.GetInputDevice($eIndex)
}
} }
class VirtualStrip : Strip { class VirtualStrip : Strip {

View File

@ -850,24 +850,47 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' {
@{ Index = $phys_in } @{ Index = $phys_in }
) { ) {
Context 'Device' -ForEach @( Context 'Device' -ForEach @(
@{ Value = 'testInput' }, @{ Value = '' } @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' }
@{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' }
@{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' }
@{ Driver = 'mme'; Value = ''; Expected = '' }
) { ) {
It "Should set Strip[$index].Device.wdm" { BeforeEach {
$vmr.strip[$index].device.wdm = $value $vmr.strip[$index].device.Clear()
Start-Sleep -Milliseconds 800
}
It "Should set Strip[$index].Device.$($driver)" {
$vmr.strip[$index].device.name | Should -Be ''
$vmr.strip[$index].device.driver | Should -Be ''
$vmr.strip[$index].device.$($driver) = $value
Start-Sleep -Milliseconds 800 Start-Sleep -Milliseconds 800
$vmr.strip[$index].device.name | Should -Be $value $vmr.strip[$index].device.name | Should -Be $value
$vmr.strip[$index].device.driver | Should -Be $expected
} }
It "Should set Strip[$index].Device.ks" { It "Should set Strip[$index].Device" -ForEach @(
$vmr.strip[$index].device.ks = $value @{
Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $false }
Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $false }
}
) {
$initial = $vmr.strip[$index].device.Get()
$initial.Driver | Should -Be $clear.Driver
$initial.Name | Should -Be $clear.Name
$initial.HardwareId | Should -Be $clear.HardwareId
$initial.IsOutput | Should -Be $clear.IsOutput
$vmr.strip[$index].device.Set($device)
Start-Sleep -Milliseconds 800 Start-Sleep -Milliseconds 800
$vmr.strip[$index].device.name | Should -Be $value $result = $vmr.strip[$index].device.Get()
}
$result.Driver | Should -Be $device.Driver
It "Should set Strip[$index].Device.mme" { $result.Name | Should -Be $device.Name
$vmr.strip[$index].device.mme = $value $result.HardwareId | Should -Be $device.HardwareId
Start-Sleep -Milliseconds 800 $result.IsOutput | Should -Be $device.IsOutput
$vmr.strip[$index].device.name | Should -Be $value
} }
} }
@ -986,14 +1009,45 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' {
@{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' } @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' }
@{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' } @{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' }
@{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' } @{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' }
@{ Driver = 'mme'; Value = ''; Expected = 'none' } @{ Driver = 'mme'; Value = ''; Expected = '' }
) { ) {
BeforeEach {
$vmr.bus[$index].device.Clear()
Start-Sleep -Milliseconds 800
}
It "Should set Bus[$index].Device.$($driver)" { It "Should set Bus[$index].Device.$($driver)" {
$vmr.bus[$index].device.name | Should -Be ''
$vmr.bus[$index].device.driver | Should -Be ''
$vmr.bus[$index].device.$($driver) = $value $vmr.bus[$index].device.$($driver) = $value
Start-Sleep -Milliseconds 800 Start-Sleep -Milliseconds 800
$vmr.bus[$index].device.name | Should -Be $value $vmr.bus[$index].device.name | Should -Be $value
$vmr.bus[$index].device.driver | Should -Be $expected $vmr.bus[$index].device.driver | Should -Be $expected
} }
It "Should set Bus[$index].Device" -ForEach @(
@{
Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $true }
Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $true }
}
) {
$initial = $vmr.bus[$index].device.Get()
$initial.Driver | Should -Be $clear.Driver
$initial.Name | Should -Be $clear.Name
$initial.HardwareId | Should -Be $clear.HardwareId
$initial.IsOutput | Should -Be $clear.IsOutput
$vmr.bus[$index].device.Set($device)
Start-Sleep -Milliseconds 800
$result = $vmr.bus[$index].device.Get()
$result.Driver | Should -Be $device.Driver
$result.Name | Should -Be $device.Name
$result.HardwareId | Should -Be $device.HardwareId
$result.IsOutput | Should -Be $device.IsOutput
}
} }
} }
@ -1004,14 +1058,45 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' {
@{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' } @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' }
@{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' } @{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' }
@{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' } @{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' }
@{ Driver = 'mme'; Value = ''; Expected = 'none' } @{ Driver = 'mme'; Value = ''; Expected = '' }
) { ) {
BeforeEach {
$vmr.bus[$index].device.Clear()
Start-Sleep -Milliseconds 800
}
It "Should set Bus[$index].Device.$($driver)" { It "Should set Bus[$index].Device.$($driver)" {
$vmr.bus[$index].device.name | Should -Be ''
$vmr.bus[$index].device.driver | Should -Be ''
$vmr.bus[$index].device.$($driver) = $value $vmr.bus[$index].device.$($driver) = $value
Start-Sleep -Milliseconds 800 Start-Sleep -Milliseconds 800
$vmr.bus[$index].device.name | Should -Be $value $vmr.bus[$index].device.name | Should -Be $value
$vmr.bus[$index].device.driver | Should -Be $expected $vmr.bus[$index].device.driver | Should -Be $expected
} }
It "Should set Bus[$index].Device" -ForEach @(
@{
Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $true }
Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $true }
}
) {
$initial = $vmr.bus[$index].device.Get()
$initial.Driver | Should -Be $clear.Driver
$initial.Name | Should -Be $clear.Name
$initial.HardwareId | Should -Be $clear.HardwareId
$initial.IsOutput | Should -Be $clear.IsOutput
$vmr.bus[$index].device.Set($device)
Start-Sleep -Milliseconds 800
$result = $vmr.bus[$index].device.Get()
$result.Driver | Should -Be $device.Driver
$result.Name | Should -Be $device.Name
$result.HardwareId | Should -Be $device.HardwareId
$result.IsOutput | Should -Be $device.IsOutput
}
} }
} }