-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from StartAutomating/LightScriptImprovement
Light script improvement
- Loading branch information
Showing
32 changed files
with
2,751 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Write-FormatView -TypeName LaMetric.Icon -Property Name, BigThumbnail -AutoSize -GroupByProperty Category |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Write-FormatView -TypeName LaMetric.Time.Application -Property title, vendor, version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Write-FormatView -TypeName LaMetric.Time -Property Name, Model, IPAddress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
function Connect-LaMetricTime | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Connects to a LaMetric clock | ||
.DESCRIPTION | ||
Connects to a LaMetric clock. | ||
LaMetric Time devices require a local ApiKey. | ||
Unfortunately, there is no way to get this key automatically. | ||
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in. | ||
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices). | ||
.EXAMPLE | ||
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru | ||
.LINK | ||
Get-LaMetricTime | ||
.LINK | ||
Disconnect-LaMetricTime | ||
#> | ||
[OutputType([Nullable], [PSObject])] | ||
param( | ||
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface. | ||
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeIPAddress')] | ||
[IPAddress] | ||
$IPAddress, | ||
|
||
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface. | ||
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeApiKey')] | ||
[string] | ||
$ApiKey, | ||
|
||
# If set, will output the connection information. | ||
[switch] | ||
$PassThru | ||
) | ||
|
||
begin { | ||
if ($home) { | ||
$lightScriptRoot = Join-Path $home -ChildPath LightScript | ||
} | ||
} | ||
|
||
process { | ||
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey")) | ||
$ipAndPort = "${ipAddress}:8080" | ||
|
||
#region Connect to the Device | ||
$laMetricDevice = | ||
http://$ipAndPort/api/v2/device -Headers @{ | ||
Authorization = "Basic $laMetricB64Key" | ||
} | ||
|
||
# If we could not connect, return | ||
if (-not $laMetricDevice) { | ||
return | ||
} | ||
#endregion Connect to the Device | ||
|
||
if ($laMetricDevice) { | ||
#region Save Device Information | ||
[decorate(PSTypeName='LaMetric.Time',Clear)]$laMetricDevice | ||
|
||
if ($home -and $laMetricDevice) { | ||
if (-not (Test-Path $lightScriptRoot)) { | ||
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot | ||
if (-not $createLightScriptDir) { return } | ||
} | ||
|
||
|
||
$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml" | ||
|
||
$laMetricDevice | | ||
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru | | ||
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru | | ||
Export-Clixml -Path $laMetricDataFile | ||
} | ||
#endregion Save Device Information | ||
if ($PassThru) { | ||
$laMetricDevice | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
function Connect-LaMetricTime { | ||
<# | ||
.SYNOPSIS | ||
Connects to a LaMetric clock | ||
.DESCRIPTION | ||
Connects to a LaMetric clock. | ||
LaMetric Time devices require a local ApiKey. | ||
Unfortunately, there is no way to get this key automatically. | ||
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in. | ||
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices). | ||
.EXAMPLE | ||
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru | ||
.LINK | ||
Get-LaMetricTime | ||
.LINK | ||
Disconnect-LaMetricTime | ||
#> | ||
[OutputType([Nullable], [PSObject])] | ||
param( | ||
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface. | ||
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeIPAddress')] | ||
[IPAddress] | ||
$IPAddress, | ||
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface. | ||
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeApiKey')] | ||
[string] | ||
$ApiKey, | ||
# If set, will output the connection information. | ||
[switch] | ||
$PassThru | ||
) | ||
begin { | ||
if ($home) { | ||
$lightScriptRoot = Join-Path $home -ChildPath LightScript | ||
} | ||
} | ||
process { | ||
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey")) | ||
$ipAndPort = "${ipAddress}:8080" | ||
#region Connect to the Device | ||
$laMetricDevice = | ||
Invoke-RestMethod ('http://',$ipAndPort,'/api/v2/device' -join '') -Headers @{ | ||
Authorization = "Basic $laMetricB64Key" | ||
} | ||
# If we could not connect, return | ||
if (-not $laMetricDevice) { | ||
return | ||
} | ||
#endregion Connect to the Device | ||
|
||
if ($laMetricDevice) { | ||
#region Save Device Information | ||
$laMetricDevice.pstypenames.clear() | ||
$laMetricDevice.pstypenames.add('LaMetric.Time') | ||
if ($home -and $laMetricDevice) { | ||
if (-not (Test-Path $lightScriptRoot)) { | ||
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot | ||
if (-not $createLightScriptDir) { return } | ||
} | ||
$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml" | ||
$laMetricDevice | | ||
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru | | ||
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru | | ||
Export-Clixml -Path $laMetricDataFile | ||
} | ||
#endregion Save Device Information | ||
if ($PassThru) { | ||
$laMetricDevice | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function Disconnect-LaMetricTime | ||
{ | ||
<# | ||
.Synopsis | ||
Disconnects a LaMetricTime | ||
.Description | ||
Disconnects a LaMetricTime, removing stored device info | ||
.Example | ||
Disconnect-LaMetricTime 1.2.3.4 | ||
.Link | ||
Connect-LaMetricTime | ||
#> | ||
[OutputType([Nullable], [PSObject])] | ||
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')] | ||
param( | ||
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface. | ||
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeIPAddress')] | ||
[IPAddress] | ||
$IPAddress | ||
) | ||
|
||
begin { | ||
if ($home) { | ||
$lightScriptRoot = Join-Path $home -ChildPath LightScript | ||
} | ||
} | ||
|
||
process { | ||
@(Get-ChildItem -Filter *.LaMetricTime.clixml -Path $lightScriptRoot) | | ||
Foreach-Object { | ||
$file = $_ | ||
$fileInfo = Import-Clixml -LiteralPath $file.FullName | ||
if ($fileInfo.IPAddress -eq $IPAddress -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.name)")) { | ||
Remove-Item -LiteralPath $file.FullName -Force | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
function Get-LaMetricTime | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Gets LaMetricTime | ||
.DESCRIPTION | ||
Gets LaMetricTime devices. | ||
.EXAMPLE | ||
Get-LaMetricTime | ||
.EXAMPLE | ||
Get-LaMetricTime -Audio # Gets audio settings | ||
.EXAMPLE | ||
Get-LaMetricTime -Bluetooth # Gets bluetooth settings | ||
.EXAMPLE | ||
Get-LaMetricTime -Notification # Gets notifications (there may be none) | ||
.EXAMPLE | ||
Get-LaMetricTime -Audio # Gets audio settings | ||
.LINK | ||
Connect-LaMetricTime | ||
.LINK | ||
Set-LaMetricTime | ||
#> | ||
[CmdletBinding(PositionalBinding=$false,DefaultParameterSetName='ListDevices')] | ||
[OutputType([PSObject])] | ||
param( | ||
# One or more IP Addresses of LaMetricTime devices. | ||
[Parameter(ValueFromPipelineByPropertyName)] | ||
[Alias('LaMetricTimeIPAddress')] | ||
[IPAddress[]] | ||
$IPAddress, | ||
|
||
# If set, will get apps from an LaMetric device. | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/apps')] | ||
[Alias('App','Apps','Applications')] | ||
[switch] | ||
$Application, | ||
|
||
# If set, will get audio settings of an LaMetric Time device | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/audio')] | ||
[switch] | ||
$Audio, | ||
|
||
# If set, will get bluetooth settings of an LaMetric Time device | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/bluetooth')] | ||
[switch] | ||
$Bluetooth, | ||
|
||
# If set, will get display settings of an LaMetric Time device | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/display')] | ||
[switch] | ||
$Display, | ||
|
||
# If set, will get LaMetric Time notifications | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/notifications')] | ||
[Alias('Notifications')] | ||
[switch] | ||
$Notification, | ||
|
||
# If set, will get details about a particular package of an LaMetric Time device. | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/apps/$Package',ValueFromPipelineByPropertyName)] | ||
[string] | ||
$Package, | ||
|
||
# If set, will get wifi settings of an LaMetric Time device | ||
[Parameter(Mandatory,ParameterSetName='api/v2/device/wifi')] | ||
[switch] | ||
$WiFi | ||
) | ||
|
||
begin { | ||
if (-not $script:LaMetricTimeCache) { | ||
$script:LaMetricTimeCache = @{} | ||
} | ||
if ($home) { | ||
$lightScriptRoot = Join-Path $home -ChildPath LightScript | ||
} | ||
$friendlyParameterSetNames = @{ | ||
"api/v2/device/apps" = "Application" | ||
'api/v2/device/apps/$packages' = "Application.Details" | ||
} | ||
$expandPropertiesIn = @("api/v2/device/apps") | ||
} | ||
process { | ||
#region Default to All Devices | ||
if (-not $IPAddress) { # If no -IPAddress was passed | ||
if ($home) { | ||
# Read all .LaMetricTime.clixml files beneath your LightScript directory. | ||
Get-ChildItem -Path $lightScriptRoot -ErrorAction SilentlyContinue -Filter *.LaMetricTime.clixml -Force | | ||
Import-Clixml | | ||
ForEach-Object { | ||
if (-not $_) { return } | ||
$laMetricTimeDevice = $_ | ||
$script:LaMetricTimeCache["$($laMetricTimeDevice.IPAddress)"] = $laMetricTimeDevice | ||
} | ||
|
||
$IPAddress = $script:LaMetricTimeCache.Keys # The keys of the device cache become the -IPAddress. | ||
} | ||
if (-not $IPAddress) { # If we still have no -IPAddress | ||
Write-Warning "No -IPAddress provided and no cached devices found" # warn | ||
return # and return. | ||
} | ||
} | ||
#endregion Default to All Devices | ||
|
||
if ($PSCmdlet.ParameterSetName -like 'api*') { | ||
foreach ($ip in $IPAddress) { | ||
$ipAndPort = "${ip}:8080" | ||
$endpoint = | ||
$ExecutionContext.SessionState.InvokeCommand.ExpandString($PSCmdlet.ParameterSetName) -replace '^api' | ||
$typename = | ||
if ($friendlyParameterSetNames[$PSCmdlet.ParameterSetName]) { | ||
$friendlyParameterSetNames[$PSCmdlet.ParameterSetName] | ||
} else { | ||
$lastSegment = @($endpoint -split '/')[-1] | ||
($lastSegment.Substring(0,1).ToUpper() + $lastSegment.Substring(1)) -replace 's$' | ||
} | ||
#region Connect to the Device | ||
|
||
http://$ipAndPort/api/$endpoint -Headers @{ | ||
Authorization = "Basic $laMetricB64Key" | ||
} | | ||
& { process { | ||
$out = $_ | ||
if ($expandPropertiesIn -contains $PSCmdlet.ParameterSetName) { | ||
foreach ($prop in $out.psobject.properties) { | ||
$prop.value.pstypenames.clear() | ||
$prop.value.pstypenames.add("LaMetric.Time.$typeName") | ||
$prop.value | ||
} | ||
|
||
} | ||
elseif ($out -is [Collections.IList]) { | ||
foreach ($o in $out) { | ||
$o.pstypenames.clear() | ||
$o.pstypenames.add("LaMetric.Time.$typename") | ||
$o | ||
} | ||
} | ||
else { | ||
$out.pstypenames.clear() | ||
$out.pstypenames.add("LaMetric.Time.$typename") | ||
$out | ||
} | ||
|
||
} } | ||
} | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq 'ListDevices') { | ||
$script:LaMetricTimeCache.Values | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.