Skip to content

Commit 5c2212f

Browse files
Merge pull request #88 from StartAutomating/LightScriptImprovement
Light script improvement
2 parents a0875e2 + 1428247 commit 5c2212f

32 files changed

+2751
-70
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 0.2.9:
2+
3+
* Added Support for LaMetric Time Clocks!
4+
* Connect-LaMetricTime (#67)
5+
* Disconnect-LaMetricTime (#68)
6+
* Get-LaMetricTime (#69, #71, #72, #74, #73, #75, #77)
7+
* Set-LaMetricTime (#79, #80, #81, #82, #83, #84, #85, #86, #87 )
8+
* Pixoo Improvement:
9+
* Get-Pixoo -Font (#65) -Liked (#63) -Upload (#61)
10+
* Set-Pixoo -FileID (#62)
11+
12+
---
13+
114
## 0.2.8:
215

316
* Watch-HueSensor! (Fixes #58)

Formatting/LaMetric.Icon.format.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-FormatView -TypeName LaMetric.Icon -Property Name, BigThumbnail -AutoSize -GroupByProperty Category
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-FormatView -TypeName LaMetric.Time.Application -Property title, vendor, version

Formatting/LaMetric.Time.format.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-FormatView -TypeName LaMetric.Time -Property Name, Model, IPAddress
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function Connect-LaMetricTime
2+
{
3+
<#
4+
.SYNOPSIS
5+
Connects to a LaMetric clock
6+
.DESCRIPTION
7+
Connects to a LaMetric clock.
8+
9+
LaMetric Time devices require a local ApiKey.
10+
11+
Unfortunately, there is no way to get this key automatically.
12+
13+
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in.
14+
15+
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices).
16+
.EXAMPLE
17+
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru
18+
.LINK
19+
Get-LaMetricTime
20+
.LINK
21+
Disconnect-LaMetricTime
22+
#>
23+
[OutputType([Nullable], [PSObject])]
24+
param(
25+
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
26+
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
27+
[Alias('LaMetricTimeIPAddress')]
28+
[IPAddress]
29+
$IPAddress,
30+
31+
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
32+
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)]
33+
[Alias('LaMetricTimeApiKey')]
34+
[string]
35+
$ApiKey,
36+
37+
# If set, will output the connection information.
38+
[switch]
39+
$PassThru
40+
)
41+
42+
begin {
43+
if ($home) {
44+
$lightScriptRoot = Join-Path $home -ChildPath LightScript
45+
}
46+
}
47+
48+
process {
49+
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey"))
50+
$ipAndPort = "${ipAddress}:8080"
51+
52+
#region Connect to the Device
53+
$laMetricDevice =
54+
http://$ipAndPort/api/v2/device -Headers @{
55+
Authorization = "Basic $laMetricB64Key"
56+
}
57+
58+
# If we could not connect, return
59+
if (-not $laMetricDevice) {
60+
return
61+
}
62+
#endregion Connect to the Device
63+
64+
if ($laMetricDevice) {
65+
#region Save Device Information
66+
[decorate(PSTypeName='LaMetric.Time',Clear)]$laMetricDevice
67+
68+
if ($home -and $laMetricDevice) {
69+
if (-not (Test-Path $lightScriptRoot)) {
70+
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
71+
if (-not $createLightScriptDir) { return }
72+
}
73+
74+
75+
$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml"
76+
77+
$laMetricDevice |
78+
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru |
79+
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru |
80+
Export-Clixml -Path $laMetricDataFile
81+
}
82+
#endregion Save Device Information
83+
if ($PassThru) {
84+
$laMetricDevice
85+
}
86+
}
87+
}
88+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function Connect-LaMetricTime {
2+
<#
3+
.SYNOPSIS
4+
Connects to a LaMetric clock
5+
.DESCRIPTION
6+
Connects to a LaMetric clock.
7+
LaMetric Time devices require a local ApiKey.
8+
9+
Unfortunately, there is no way to get this key automatically.
10+
11+
To Connect-LaMetricTime, you'll need to visit [developer.lametric.com](https://developer.lametric.com) and sign in.
12+
You will find API Keys for your devices at [developer.lametric.com/user/devices](https://developer.lametric.com/user/devices).
13+
.EXAMPLE
14+
Connect-LaMetric -IPAddress $laMetricIP -ApiKey $myApiKey -PassThru
15+
.LINK
16+
Get-LaMetricTime
17+
.LINK
18+
Disconnect-LaMetricTime
19+
#>
20+
[OutputType([Nullable], [PSObject])]
21+
param(
22+
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
23+
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
24+
[Alias('LaMetricTimeIPAddress')]
25+
[IPAddress]
26+
$IPAddress,
27+
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
28+
[Parameter(Mandatory,Position=1,ValueFromPipelineByPropertyName)]
29+
[Alias('LaMetricTimeApiKey')]
30+
[string]
31+
$ApiKey,
32+
# If set, will output the connection information.
33+
[switch]
34+
$PassThru
35+
)
36+
begin {
37+
if ($home) {
38+
$lightScriptRoot = Join-Path $home -ChildPath LightScript
39+
}
40+
}
41+
process {
42+
$laMetricB64Key = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("dev:$ApiKey"))
43+
$ipAndPort = "${ipAddress}:8080"
44+
#region Connect to the Device
45+
$laMetricDevice =
46+
Invoke-RestMethod ('http://',$ipAndPort,'/api/v2/device' -join '') -Headers @{
47+
Authorization = "Basic $laMetricB64Key"
48+
}
49+
# If we could not connect, return
50+
if (-not $laMetricDevice) {
51+
return
52+
}
53+
#endregion Connect to the Device
54+
55+
if ($laMetricDevice) {
56+
#region Save Device Information
57+
$laMetricDevice.pstypenames.clear()
58+
$laMetricDevice.pstypenames.add('LaMetric.Time')
59+
if ($home -and $laMetricDevice) {
60+
if (-not (Test-Path $lightScriptRoot)) {
61+
$createLightScriptDir = New-Item -ItemType Directory -Path $lightScriptRoot
62+
if (-not $createLightScriptDir) { return }
63+
}
64+
$laMetricDataFile = Join-Path $lightScriptRoot ".$($laMetricDevice.serial_number).LaMetricTime.clixml"
65+
$laMetricDevice |
66+
Add-Member NoteProperty IpAddress $IPAddress -Force -PassThru |
67+
Add-Member NoteProperty ApiKey $laMetricB64Key -Force -PassThru |
68+
Export-Clixml -Path $laMetricDataFile
69+
}
70+
#endregion Save Device Information
71+
if ($PassThru) {
72+
$laMetricDevice
73+
}
74+
}
75+
}
76+
}
77+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function Disconnect-LaMetricTime
2+
{
3+
<#
4+
.Synopsis
5+
Disconnects a LaMetricTime
6+
.Description
7+
Disconnects a LaMetricTime, removing stored device info
8+
.Example
9+
Disconnect-LaMetricTime 1.2.3.4
10+
.Link
11+
Connect-LaMetricTime
12+
#>
13+
[OutputType([Nullable], [PSObject])]
14+
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
15+
param(
16+
# The IP Address for the Twinkly device. This can be discovered thru the phone user interface.
17+
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName)]
18+
[Alias('LaMetricTimeIPAddress')]
19+
[IPAddress]
20+
$IPAddress
21+
)
22+
23+
begin {
24+
if ($home) {
25+
$lightScriptRoot = Join-Path $home -ChildPath LightScript
26+
}
27+
}
28+
29+
process {
30+
@(Get-ChildItem -Filter *.LaMetricTime.clixml -Path $lightScriptRoot) |
31+
Foreach-Object {
32+
$file = $_
33+
$fileInfo = Import-Clixml -LiteralPath $file.FullName
34+
if ($fileInfo.IPAddress -eq $IPAddress -and $PSCmdlet.ShouldProcess("Remove $($fileInfo.name)")) {
35+
Remove-Item -LiteralPath $file.FullName -Force
36+
}
37+
}
38+
}
39+
}
40+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)