Created
September 22, 2021 21:49
-
-
Save joshooaj/7bc42b623d2f1690b6e1833f9d1d3dd0 to your computer and use it in GitHub Desktop.
Listens for new alarms from a Milestone XProtect VMS installation
This file contains hidden or 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
| function Trace-VmsAlarms { | |
| <# | |
| .SYNOPSIS | |
| Polls a Milestone XProtect Event Server for new alarmline entries | |
| .DESCRIPTION | |
| Starts a polling loop to ask for new alarmline entries from a Milestone XProtect Event | |
| Server. The InitialStartTime is "now" by default, so only new alarms are returned. You | |
| can either pipe the alarms to a Foreach-Object and process them there, or provide a | |
| scriptblock which will be invoked once for each alarm returned. | |
| .EXAMPLE | |
| PS C:\> Trace-VmsAlarms -PollingInterval (New-TimeSpan -Seconds 1) -ScriptBlock { param($Alarm) Write-Output "Alarm received at $($Alarm.Timestamp) from $($Alarm.SourceName)" } | |
| Explanation of what the example does | |
| #> | |
| [CmdletBinding()] | |
| [OutputType([VideoOS.Platform.Proxy.Alarm.AlarmLine])] | |
| param( | |
| # Specifies an optional scriptblock to be called when an alarm is received. The scriptblock will be invoked with an [VideoOS.Platform.Proxy.Alarm.AlarmLine] object as a parameter. If no scriptblock is provided, the alarm will be returned to the pipeline instead. | |
| [Parameter()] | |
| [scriptblock] | |
| $ScriptBlock, | |
| # Specifies the timestamp for the first alarm to be detected. The default is to only return new alarms. | |
| [Parameter()] | |
| [datetime] | |
| $InitialStartTime = (Get-Date).ToUniversalTime(), | |
| # Specifies the interval between calls to Get-AlarmLine. Default is 2 seconds. | |
| [Parameter()] | |
| [timespan] | |
| $PollingInterval = (New-TimeSpan -Seconds 2), | |
| # Specifies the time at which the function should quit. By default the command runs indefinitely and must be interrupted by CTRL+C or ending the process. | |
| [Parameter()] | |
| [datetime] | |
| $EndTrace = [datetime]::MaxValue | |
| ) | |
| process { | |
| $ascendingOrder = New-AlarmOrder -Target Timestamp -Order Ascending | |
| $startTimeCondition = New-AlarmCondition -Target Timestamp -Operator GreaterThan -Value $InitialStartTime.ToUniversalTime() | |
| while ($true) { | |
| Get-AlarmLine -Conditions $startTimeCondition -SortOrders $ascendingOrder | ForEach-Object { | |
| if ($_.Timestamp -gt $startTimeCondition.Value) { | |
| $startTimeCondition.Value = $_.Timestamp | |
| } | |
| if ($null -eq $ScriptBlock) { | |
| Write-Output $_ | |
| } | |
| else { | |
| try { | |
| $ScriptBlock.Invoke($_) | |
| } | |
| catch { | |
| Write-Error -Message "An error was thrown within the provided scriptblock. Error: $($_.Exception.Message)" | |
| } | |
| } | |
| } | |
| if ((Get-Date) -ge $EndTrace) { | |
| break | |
| } | |
| Start-Sleep -Seconds $PollingInterval.TotalSeconds | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment