Skip to content

Commit

Permalink
Merge pull request #60 from StartAutomating/MoreLightScripting
Browse files Browse the repository at this point in the history
More light scripting
  • Loading branch information
StartAutomating authored Nov 1, 2022
2 parents ff01d5f + 183cfc4 commit 04d6210
Show file tree
Hide file tree
Showing 17 changed files with 532 additions and 115 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 0.2.8:

* Watch-HueSensor! (Fixes #58)
* Pixoo Improvements:
* Set-Pixoo -Beep (Fixes #57)
* Set-Pixoo -Mirror (Fixes #49)
* Set-Pixoo -Hue/-Saturation/-Luminance Fix (Fixes #56)
* Connect-Pixoo saves less information for better pipelining (Fixes #56)
* Adding Formatting for Pixoo.Weather (Fixes #59)
* LightScript.Color now supports daisy chaining (Fixes #54)

---

## 0.2.7:

* Pixoo Improvements:
Expand Down
1 change: 1 addition & 0 deletions Formatting/Pixoo.Weather.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-FormatView -TypeName Pixoo.Weather -Property Weather, CurTemp, MaxTemp, MinTemp, Pressure, WindSpeed, Visibility -AutoSize
4 changes: 1 addition & 3 deletions Formatting/Pixoo.format.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Write-formatView -TypeName Pixoo -Property IPAddress, MACAddress, Brightness

Write-formatView -TypeName Pixoo -Property RotationFlag, GyrateAngle, MirrorFlag -Name Orientation
Write-formatView -TypeName Pixoo -Property IPAddress, MACAddress
1 change: 1 addition & 0 deletions Functions/Hue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This directory contains LightScript's functions for [Philips Hue Bridges](https:
|Send-HueBridge |Send |HueBridge |[Send-HueBridge.ps1](Send-HueBridge.ps1) |
|Set-HueLight |Set |HueLight |[Set-HueLight.ps1](Set-HueLight.ps1) |
|Set-HueRule |Set |HueRule |[Set-HueRule.ps1](Set-HueRule.ps1) |
|Watch-HueSensor |Watch |HueSensor |[Watch-HueSensor.ps1](Watch-HueSensor.ps1) |
|Write-HueSensor |Write |HueSensor |[Write-HueSensor.ps1](Write-HueSensor.ps1) |


72 changes: 72 additions & 0 deletions Functions/Hue/Watch-HueSensor.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
function Watch-HueSensor
{
<#
.SYNOPSIS
Watches Hue Sensors for changes
.DESCRIPTION
Watches Hue Sensors for changes.
An event will be generated whenever a sensor changes.
.LINK
Get-HueSensor
.EXAMPLE
# Watch for events every minute
Watch-HueSensor -Interval "00:01:00"
# Whenever daylight changes
Register-EngineEvent -SourceIdentifier Daylight.Changed -Action {
if ($event.MessageData.state.Daylight) {
Set-HueLight -Brightness 0.5
Set-NanoLeaf -Brightness 0.5
} else {
Set-HueLight -Brightness 1
Set-NanoLeaf -Brightness 1
}
}
#>
[OutputType([Management.Automation.Job])]
param(
# The interval hue sensors will be polled. By default, every 2.5 seconds.
[Parameter(ValueFromPipelineByPropertyName)]
[timespan]
$Interval = '00:00:02.5'
)

process {
#region Define Background Job
$watchJob = [scriptblock]::Create((@"
Import-Module '$($MyInvocation.MyCommand.Module.Path -replace '\.psm1$', '.psd1')'
`$sleepInterval = [Timespan]'$interval'
`$lastSensorCheck = [DateTime]::UTCNow
do {
"@) + {
Register-EngineEvent -Forward -SourceIdentifier Hue.Sensor.Changed
foreach ($sensor in @(Get-HueSensor)) {
if (($sensor.State.lastUpdated -eq 'none') -or (
$sensor.State.lastUpdated -lt $LastSensorCheck
)) {
continue
}

New-Event -SourceIdentifier "Hue.Sensor.Changed" -MessageData $sensor
$eventName = "$($sensor.Name -replace '\s').Changed"
Register-EngineEvent -Forward -SourceIdentifier $eventName
New-Event -SourceIdentifier $eventName -MessageData $sensor
}
$LastSensorCheck = [datetime]::UtcNow
Start-Sleep -Milliseconds $sleepInterval.TotalMilliseconds
} + "
} while (`$true)")

#endregion Define Background Job

#region Launch Job
$jobExists = Get-Job | Where-Object Name -EQ Watch-HueSensor
if ($jobExists -and $jobExists.Interval -gt $Interval) {
$jobExists | Remove-Job -Force
}
Start-Job -ScriptBlock $watchJob -Name 'Watch-HueSensor' |
Add-Member NoteProperty Interval $Interval -Force -PassThru
#endregion Launch Job
}
}
14 changes: 8 additions & 6 deletions Functions/Pixoo/Connect-Pixoo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ function Connect-Pixoo
}

$pixooDataFile = Join-Path $lightScriptRoot ".$($macAddress).pixoo.clixml"
$pixooConf.pstypenames.clear()
$pixooConf.pstypenames.add('Pixoo')

$pixooObject = [PSCustomObject]@{
PSTypeName = 'Pixoo'
IPAddress = $IPAddress
MACAddress = $macAddress
}

# If the -DeviceID was provided
if ($DeviceId) {
# add it to the configuration data.
$pixooConf | Add-Member NoteProperty DeviceID $DeviceId -Force
$pixooObject | Add-Member NoteProperty DeviceID $DeviceId -Force
}
$pixooConf |
Add-Member NoteProperty IPAddress $IPAddress -Force -PassThru |
Add-Member NoteProperty MACAddress $macAddress -Force -PassThru |
$pixooObject |
Export-Clixml -Path $pixooDataFile
}
#endregion Save Device Information
Expand Down
115 changes: 101 additions & 14 deletions Functions/Pixoo/Set-Pixoo.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
function Set-Pixoo
{
<#
.Synopsis
.SYNOPSIS
Sets Pixoo Frames
.Description
.DESCRIPTION
Changes Pixoo Frames
.Example
Set-Pixoo -Brightness .5
.EXAMPLE
Set-Pixoo -Brightness 1
.EXAMPLE
# Set the pixoo to the 3rd visualizer (a nice frequency graph)
Set-Pixoo -Visualizer 3
.EXAMPLE
# The timer will elapse after 30 seconds.
Set-Pixoo -Timer "00:00:30"
.LINK
Get-Pixoo
#>
Expand Down Expand Up @@ -122,7 +128,37 @@ function Set-Pixoo
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet(0,90,180,270)]
[int]
$Rotation
$Rotation,

# If set, will put the Pixoo device into mirroring mode.
# This can be nice if you have two Pixoos side by side.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$Mirror,

# If set, the Pixoo will beep.
# -BeepTime controls how long a -Beep will last
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Beeps')]
[switch]
$Beep,

# -BeepTime controls how long a -Beep will last
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('BeepFor')]
[timespan]
$BeepTime = '00:00:00.25',

# -BeepPause controls how long to wait between -Beeps
[Parameter(ValueFromPipelineByPropertyName)]
[timespan]
$BeepPause = '00:00:00.125',

# -BeepCount controls the number of -Beeps
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateRange(1,50)]
[int]
$BeepCount = 1
)

begin {
Expand All @@ -132,7 +168,6 @@ function Set-Pixoo
if ($home) {
$lightScriptRoot = Join-Path $home -ChildPath LightScript
}
$SetPixooCmd = $myInvocation.MyCommand
}

process {
Expand All @@ -158,9 +193,7 @@ function Set-Pixoo
}
}
#endregion Default to All Devices




foreach ($ip in $ipAddress) {
$refreshDevice = $false
$invokeSplat = @{Uri="http://$ip/post";Method='POST'}
Expand All @@ -182,11 +215,12 @@ function Set-Pixoo
$paramCopy.ContainsKey("Hue") -and
$paramCopy.ContainsKey("Saturation")
) {
$RGBColor = ([PSCustomObject]@{PSTypeName='LightScript.Color'}).FromHSL($Hue, $Saturation, $Brightness)
$convertedColor = (([PSCustomObject]@{PSTypeName='LightScript.Color'}).HSLToRGB($Hue, $Saturation, $Brightness))
$RGBColor = $convertedColor.RGB
}



#region On/Off Switch
if ($On -and -not $Off) {
$invokeSplat.Body = (@{
Command = "Channel/OnOffScreen"
Expand All @@ -212,7 +246,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion On/Off Switch

#region Visualizer
if ($paramCopy.ContainsKey("Visualizer")) {
$invokeSplat.Body = (@{
Command = "Channel/SetEqPosition"
Expand All @@ -224,7 +260,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Visualizer

#region Lat/Long
if ($Latitude -and $longitude) {
$invokeSplat.Body = (@{
Command = "Sys/LogAndLat"
Expand All @@ -237,7 +275,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Lat/Long

#region RGBColor
if ($RGBColor) {
$r,$g,$b =
if ($RGBColor.Length -eq 7) {
Expand Down Expand Up @@ -281,10 +321,11 @@ function Set-Pixoo
$invokeSplat
} else {
Invoke-RestMethod @invokeSplat
}

}
}
#endregion RGBColor

#region Change Channel
if ($Channel) {
$valueList = @($myInvocation.MyCommand.Parameters.Channel.Attributes.ValidValues)
for ($index = 0; $index -lt $valueList.Count;$index++) {
Expand All @@ -302,7 +343,9 @@ function Set-Pixoo
}
}
}
#endregion Change Channel

#region Stopwatch
if ($Stopwatch) {
$valueList = @($myInvocation.MyCommand.Parameters.StopWatch.Attributes.ValidValues)
for ($index = 0; $index -lt $valueList.Count;$index++) {
Expand All @@ -320,7 +363,9 @@ function Set-Pixoo
}
}
}
#endregion Stopwatch

#region Device Rotation
if ($PSBoundParameters.ContainsKey("Rotation")) {
$invokeSplat.Body = (@{
Command = "Device/SetScreenRotationAngle"
Expand All @@ -332,7 +377,23 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Device Rotation

#region Device Mirroring
if ($PSBoundParameters.ContainsKey("Mirror")) {
$invokeSplat.Body = (@{
Command = "Device/SetMirrorMode"
Mode = $Mirror -as [bool] -as [int]
} | ConvertTo-Json -Compress)
if ($whatIfPreference) {
$invokeSplat
} elseif ($psCmdlet.ShouldProcess("$($invokeSplat.Command)")) {
Invoke-RestMethod @invokeSplat
}
}
#endregion Device Mirroring

#region Select Custom Playlist
if ($psBoundParameters.ContainsKey("CustomPlaylist")) {
$invokeSplat.Body = (@{
Command = "Channel/SetCustomPageIndex"
Expand All @@ -343,8 +404,10 @@ function Set-Pixoo
} elseif ($psCmdlet.ShouldProcess("$($invokeSplat.Command)")) {
Invoke-RestMethod @invokeSplat
}
}
}
#endregion Select Custom Playlist

#region Select Cloud Channel
if ($psBoundParameters.ContainsKey("CloudChannel")) {
$invokeSplat.Body = (@{
Command = "Channel/CloudIndex"
Expand All @@ -356,7 +419,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Select Cloud Channel

#region Timer
if ($timer) {
$invokeSplat.Body = (@{
Command = "Tools/SetTimer"
Expand All @@ -369,7 +434,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Timer

#region Noise Meter
if ($NoiseMeter.IsPresent) {
$invokeSplat.Body = (@{
Command = "Tools/SetNoiseStatus"
Expand All @@ -381,7 +448,9 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Noise Meter

#region Scoreboard
if ($paramCopy.ContainsKey("RedScore") -or $paramCopy.ContainsKey("BlueScore")) {
$invokeSplat.Body = (@{
Command = "Tools/SetScoreBoard"
Expand All @@ -408,6 +477,24 @@ function Set-Pixoo
Invoke-RestMethod @invokeSplat
}
}
#endregion Scoreboard

#region Beeps
if ($Beep) {
$invokeSplat.Body = (@{
Command = "Device/PlayBuzzer"
ActiveTimeInCycle = [int]$BeepTime.TotalMilliseconds
OffTimeInCycle = [int]$BeepPause.TotalMilliseconds
PlayTotalTime = $BeepCount * [int]($BeepTime + $BeepPause).TotalMilliseconds
Second = [int]$Timer.Seconds
} | ConvertTo-Json -Compress)
if ($whatIfPreference) {
$invokeSplat
} elseif ($psCmdlet.ShouldProcess("$($invokeSplat.Command)")) {
Invoke-RestMethod @invokeSplat
}
}
#endregion Beeps
)

if ($restOutputs -and $whatIfPreference) {
Expand Down
1 change: 1 addition & 0 deletions Functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This directory and it's subdirectories contain the functions defined in LightScr
|Set-NanoLeaf |Set |NanoLeaf |[NanoLeaf/Set-NanoLeaf.ps1](NanoLeaf/Set-NanoLeaf.ps1) |
|Set-Pixoo |Set |Pixoo |[Pixoo/Set-Pixoo.ps1](Pixoo/Set-Pixoo.ps1) |
|Set-Twinkly |Set |Twinkly |[Twinkly/Set-Twinkly.ps1](Twinkly/Set-Twinkly.ps1) |
|Watch-HueSensor |Watch |HueSensor |[Hue/Watch-HueSensor.ps1](Hue/Watch-HueSensor.ps1) |
|Watch-NanoLeaf |Watch |NanoLeaf |[NanoLeaf/Watch-NanoLeaf.ps1](NanoLeaf/Watch-NanoLeaf.ps1) |
|Write-HueSensor |Write |HueSensor |[Hue/Write-HueSensor.ps1](Hue/Write-HueSensor.ps1) |

Expand Down
Loading

0 comments on commit 04d6210

Please sign in to comment.