|
| 1 | +function Get-LaMetricTime |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Gets LaMetricTime |
| 6 | + .DESCRIPTION |
| 7 | + Gets LaMetricTime devices. |
| 8 | + .EXAMPLE |
| 9 | + Get-LaMetricTime |
| 10 | + .EXAMPLE |
| 11 | + Get-LaMetricTime -Audio # Gets audio settings |
| 12 | + .EXAMPLE |
| 13 | + Get-LaMetricTime -Bluetooth # Gets bluetooth settings |
| 14 | + .EXAMPLE |
| 15 | + Get-LaMetricTime -Notification # Gets notifications (there may be none) |
| 16 | + .EXAMPLE |
| 17 | + Get-LaMetricTime -Audio # Gets audio settings |
| 18 | + .LINK |
| 19 | + Connect-LaMetricTime |
| 20 | + .LINK |
| 21 | + Set-LaMetricTime |
| 22 | + #> |
| 23 | + [CmdletBinding(PositionalBinding=$false,DefaultParameterSetName='ListDevices')] |
| 24 | + [OutputType([PSObject])] |
| 25 | + param( |
| 26 | + # One or more IP Addresses of LaMetricTime devices. |
| 27 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 28 | + [Alias('LaMetricTimeIPAddress')] |
| 29 | + [IPAddress[]] |
| 30 | + $IPAddress, |
| 31 | + |
| 32 | + # If set, will get apps from an LaMetric device. |
| 33 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/apps')] |
| 34 | + [Alias('App','Apps','Applications')] |
| 35 | + [switch] |
| 36 | + $Application, |
| 37 | + |
| 38 | + # If set, will get audio settings of an LaMetric Time device |
| 39 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/audio')] |
| 40 | + [switch] |
| 41 | + $Audio, |
| 42 | + |
| 43 | + # If set, will get bluetooth settings of an LaMetric Time device |
| 44 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/bluetooth')] |
| 45 | + [switch] |
| 46 | + $Bluetooth, |
| 47 | + |
| 48 | + # If set, will get display settings of an LaMetric Time device |
| 49 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/display')] |
| 50 | + [switch] |
| 51 | + $Display, |
| 52 | + |
| 53 | + # If set, will get LaMetric Time notifications |
| 54 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/notifications')] |
| 55 | + [Alias('Notifications')] |
| 56 | + [switch] |
| 57 | + $Notification, |
| 58 | + |
| 59 | + # If set, will get details about a particular package of an LaMetric Time device. |
| 60 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/apps/$Package',ValueFromPipelineByPropertyName)] |
| 61 | + [string] |
| 62 | + $Package, |
| 63 | + |
| 64 | + # If set, will get wifi settings of an LaMetric Time device |
| 65 | + [Parameter(Mandatory,ParameterSetName='api/v2/device/wifi')] |
| 66 | + [switch] |
| 67 | + $WiFi |
| 68 | + ) |
| 69 | + |
| 70 | + begin { |
| 71 | + if (-not $script:LaMetricTimeCache) { |
| 72 | + $script:LaMetricTimeCache = @{} |
| 73 | + } |
| 74 | + if ($home) { |
| 75 | + $lightScriptRoot = Join-Path $home -ChildPath LightScript |
| 76 | + } |
| 77 | + $friendlyParameterSetNames = @{ |
| 78 | + "api/v2/device/apps" = "Application" |
| 79 | + 'api/v2/device/apps/$packages' = "Application.Details" |
| 80 | + } |
| 81 | + $expandPropertiesIn = @("api/v2/device/apps") |
| 82 | + } |
| 83 | + process { |
| 84 | + #region Default to All Devices |
| 85 | + if (-not $IPAddress) { # If no -IPAddress was passed |
| 86 | + if ($home) { |
| 87 | + # Read all .LaMetricTime.clixml files beneath your LightScript directory. |
| 88 | + Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.LaMetricTime.clixml -Force | |
| 89 | + Import-Clixml | |
| 90 | + ForEach-Object { |
| 91 | + if (-not $_) { return } |
| 92 | + $laMetricTimeDevice = $_ |
| 93 | + $script:LaMetricTimeCache["$($laMetricTimeDevice.IPAddress)"] = $laMetricTimeDevice |
| 94 | + } |
| 95 | + |
| 96 | + $IPAddress = $script:LaMetricTimeCache.Keys # The keys of the device cache become the -IPAddress. |
| 97 | + } |
| 98 | + if (-not $IPAddress) { # If we still have no -IPAddress |
| 99 | + Write-Warning "No -IPAddress provided and no cached devices found" # warn |
| 100 | + return # and return. |
| 101 | + } |
| 102 | + } |
| 103 | + #endregion Default to All Devices |
| 104 | + |
| 105 | + if ($PSCmdlet.ParameterSetName -like 'api*') { |
| 106 | + foreach ($ip in $IPAddress) { |
| 107 | + $ipAndPort = "${ip}:8080" |
| 108 | + $endpoint = |
| 109 | + $ExecutionContext.SessionState.InvokeCommand.ExpandString($PSCmdlet.ParameterSetName) -replace '^api' |
| 110 | + $typename = |
| 111 | + if ($friendlyParameterSetNames[$PSCmdlet.ParameterSetName]) { |
| 112 | + $friendlyParameterSetNames[$PSCmdlet.ParameterSetName] |
| 113 | + } else { |
| 114 | + $lastSegment = @($endpoint -split '/')[-1] |
| 115 | + ($lastSegment.Substring(0,1).ToUpper() + $lastSegment.Substring(1)) -replace 's$' |
| 116 | + } |
| 117 | + #region Connect to the Device |
| 118 | + |
| 119 | + http://$ipAndPort/api/$endpoint -Headers @{ |
| 120 | + Authorization = "Basic $laMetricB64Key" |
| 121 | + } | |
| 122 | + & { process { |
| 123 | + $out = $_ |
| 124 | + if ($expandPropertiesIn -contains $PSCmdlet.ParameterSetName) { |
| 125 | + foreach ($prop in $out.psobject.properties) { |
| 126 | + $prop.value.pstypenames.clear() |
| 127 | + $prop.value.pstypenames.add("LaMetric.Time.$typeName") |
| 128 | + $prop.value |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + elseif ($out -is [Collections.IList]) { |
| 133 | + foreach ($o in $out) { |
| 134 | + $o.pstypenames.clear() |
| 135 | + $o.pstypenames.add("LaMetric.Time.$typename") |
| 136 | + $o |
| 137 | + } |
| 138 | + } |
| 139 | + else { |
| 140 | + $out.pstypenames.clear() |
| 141 | + $out.pstypenames.add("LaMetric.Time.$typename") |
| 142 | + $out |
| 143 | + } |
| 144 | + |
| 145 | + } } |
| 146 | + } |
| 147 | + } |
| 148 | + elseif ($PSCmdlet.ParameterSetName -eq 'ListDevices') { |
| 149 | + $script:LaMetricTimeCache.Values |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
0 commit comments