Public/Update-BridgeStatus.ps1

<#
    .SYNOPSIS
    Ελέγχει το αποτέλεσμα εξαγωγής JSON και εγείρει τερματικό σφάλμα σε αποτυχία.

.DESCRIPTION
Μετατρέπει την αποτυχία αποθήκευσης του στιγμιότυπου σε τερματικό σφάλμα για τον καλούντα.

    .PARAMETER ExportResult
    Το αποτέλεσμα της Export-BridgeStatusJson.

    .PARAMETER OutputFile
    Η διαδρομή του αρχείου εξόδου.

    .PARAMETER Cmdlet
    Το αντικείμενο PSCmdlet του καλούντος.
#>

function Assert-BridgeSnapshotExport {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$ExportResult,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$OutputFile,
        [Parameter(Mandatory)][ValidateNotNull()][System.Management.Automation.PSCmdlet]$Cmdlet
    )
    if (-not $ExportResult.Success) {
        $Cmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new(
            [System.Exception]::new($ExportResult.ErrorMessage),
            $ExportResult.ErrorCode,
            [System.Management.Automation.ErrorCategory]::WriteError,
            $OutputFile
        ))
    }
}

<#
    .SYNOPSIS
    Αποστέλλει την κατάλληλη ειδοποίηση για ένα συμβάν αλλαγής γέφυρας.

.DESCRIPTION
Επιλέγει αποστολή για άνοιγμα ή κλείσιμο και περνά τα στοιχεία του συμβάντος στην αντίστοιχη ειδοποίηση.

    .PARAMETER DiffEvent
    Το συμβάν διαφοράς κατάστασης γέφυρας.

    .PARAMETER ApiKey
    Το API Key για OCR ανάλυση.

    .PARAMETER PoUserKey
    Το User Key του παραλήπτη Pushover.

    .PARAMETER PoApiKey
    Το API Token της εφαρμογής Pushover.

    .PARAMETER Configuration
    Το αντικείμενο διαμόρφωσης.
#>

function Invoke-BridgeDiffEventDelivery {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$DiffEvent,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$ApiKey,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$PoUserKey,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$PoApiKey,
        [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$Configuration
    )
    $notificationSplat = @{
        CurrentState  = $DiffEvent.StateInfo
        PoUserKey     = $PoUserKey
        PoApiKey      = $PoApiKey
        Configuration = $Configuration
    }
    if ($DiffEvent.Type -eq 'Closed') {
        $notificationSplat.ApiKey = $ApiKey
        Invoke-BridgeClosedNotification @notificationSplat
    } else {
        Invoke-BridgeOpenedNotification @notificationSplat
    }
}

<#
    .SYNOPSIS
    Συνθέτει το snapshot καταστάσεων προς αποθήκευση μετά από μερική επιτυχία ειδοποιήσεων.

    .DESCRIPTION
    Ξεκινά από την προηγούμενη κατάσταση ανά γέφυρα και ενημερώνει τις αμετάβλητες
    καθώς και όσες γέφυρες ειδοποιήθηκαν επιτυχώς με την τρέχουσα εγγραφή τους.

    .PARAMETER PreviousState
    Η προηγούμενη λίστα καταστάσεων γεφυρών.

    .PARAMETER CurrentState
    Η τρέχουσα λίστα καταστάσεων γεφυρών.

    .PARAMETER DiffEvents
    Τα γεγονότα αλλαγών κατάστασης που προέκυψαν από τη σύγκριση.

    .PARAMETER NotifiedBridgeNames
    Τα ονόματα των γεφυρών των οποίων η ειδοποίηση ολοκληρώθηκε επιτυχώς.
    #>

function Resolve-BridgeAlertSnapshot {
    [CmdletBinding()]
    [OutputType([object[]])]
    param (
        [Parameter()][AllowNull()][AllowEmptyCollection()][object[]]$PreviousState = @(),
        [Parameter()][AllowNull()][AllowEmptyCollection()][object[]]$CurrentState = @(),
        [Parameter()][AllowNull()][AllowEmptyCollection()][object[]]$DiffEvents = @(),
        [Parameter()][AllowNull()][AllowEmptyCollection()][string[]]$NotifiedBridgeNames = @()
    )
    $persistedByBridge = [ordered]@{}
    foreach ($entry in $PreviousState) {
        $persistedByBridge[$entry.GefyraName] = $entry
    }
    $pendingBridgeNames = @(
        $DiffEvents |
            ForEach-Object -Process { @($_.StateInfo) } |
            ForEach-Object -Process { $_.GefyraName } |
            Where-Object -FilterScript { $_ -notin $NotifiedBridgeNames }
    )
    foreach ($entry in $CurrentState) {
        if ($entry.GefyraName -notin $pendingBridgeNames) {
            $persistedByBridge[$entry.GefyraName] = $entry
        }
    }
    return @($persistedByBridge.Values)
}

<#
.SYNOPSIS
Returns the records to persist when scheduled openings have been deferred.
.DESCRIPTION
Keeps each deferred bridge's saved alert state and attaches the latest unconfirmed observation.
.PARAMETER CurrentState
The records from the current poll.
.PARAMETER DeferredOpenings
Saved records keyed by bridge name.
#>

function Resolve-BridgeDeferredSnapshot {
    param([object[]]$CurrentState, [hashtable]$DeferredOpenings)

    foreach ($entry in $CurrentState) {
        if ($DeferredOpenings.ContainsKey($entry.GefyraName)) {
            $saved = $DeferredOpenings[$entry.GefyraName].PSObject.Copy()
            $observation = [pscustomobject]@{
                Status             = $entry.GefyraStatus
                Timestamp          = Get-BridgeOptionalProperty -InputObject $entry -PropertyName 'Timestamp'
                ReopeningConfirmed = $false
            }
            $saved | Add-Member -NotePropertyName Observation -NotePropertyValue $observation -Force
            $saved
        } else {
            $entry
        }
    }
}

<#
    .SYNOPSIS
    Συγκρίνει προηγούμενη και τρέχουσα κατάσταση γεφυρών.

    .DESCRIPTION
    Η Update-BridgeStatus συγκρίνει δύο snapshots γεφυρών
    και ανιχνεύει αλλαγές κατάστασης.

    .PARAMETER OutputFile
    Το path του αρχείου JSON για αποθήκευση νέου snapshot.

    .PARAMETER ApiKey
    Το API Key για OCR ανάλυση αν απαιτηθεί.

    .PARAMETER PoUserKey
    Το User Key του παραλήπτη για ειδοποίηση.

    .PARAMETER PoApiKey
    Το API Token της εφαρμογής Pushover.

    .PARAMETER Configuration
    Αντικείμενο διαμόρφωσης με ρυθμίσεις, statuses και URLs.

    .OUTPUTS
    [object[]]

    .EXAMPLE
    $config = New-BridgeConfiguration
    $updateParams = @{
        OutputFile = 'C:\Logs\bridge-status.json'
        ApiKey = 'abc123'
        PoUserKey = 'user123'
        PoApiKey = 'token123'
        Configuration = $config
    }
    Update-BridgeStatus @updateParams

    .NOTES
    Χρησιμοποιεί OCR αν χρειάζεται, συγκρίνει states και αποστέλλει ειδοποιήσεις.
    #>

function Update-BridgeStatus {
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([object[]])]

    param (
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$OutputFile,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$ApiKey,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$PoUserKey,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$PoApiKey,
        [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$Configuration
    )
    begin {
    }
    process {
        $writeBridgeLogSplat = @{
            Stage   = $Configuration.LoggingConfig.InfoStage
            Message = 'Starting comparison...'
            Level   = $Configuration.LoggingConfig.VerboseLevel
        }
        Write-BridgeLog @writeBridgeLogSplat -Configuration $Configuration
        $previousState = @()
        if (Test-Path -LiteralPath $OutputFile) {
            $getDiorigaPreviousStatusSplat = @{
                InputFile     = $OutputFile
                Configuration = $Configuration
            }
            $previousState = Get-BridgePreviousStatus @getDiorigaPreviousStatusSplat
        }
        $currentState = Get-BridgeStatus -Configuration $Configuration

        if ($PSCmdlet.ShouldProcess("BridgeWatcher", "Update status and send notifications")) {
            $deferredOpenings = @{}
            $invokeSplat = @{
                PreviousState    = $previousState
                CurrentState     = $currentState
                Configuration    = $Configuration
                DeferredOpenings = $deferredOpenings
            }
            $diffEvents = Invoke-BridgeStatusComparison @invokeSplat
            $snapshotCurrent = $currentState
            if ($deferredOpenings.Count -gt 0) {
                $snapshotCurrent = @(Resolve-BridgeDeferredSnapshot -CurrentState $currentState -DeferredOpenings $deferredOpenings)
            }
            $notifiedBridgeNames = [System.Collections.Generic.List[string]]::new()
            $notificationFailures = @()
            foreach ($diffEvent in $diffEvents) {
                $deliverySplat = @{
                    DiffEvent     = $diffEvent
                    ApiKey        = $ApiKey
                    PoUserKey     = $PoUserKey
                    PoApiKey      = $PoApiKey
                    Configuration = $Configuration
                }
                try {
                    Invoke-BridgeDiffEventDelivery @deliverySplat
                } catch {
                    $notificationFailures += $_
                    continue
                }

                $eventBridgeNames = [string[]]@($diffEvent.StateInfo | ForEach-Object -Process { [string]$_.GefyraName })
                $notifiedBridgeNames.AddRange($eventBridgeNames)
                $snapshotSplat = @{
                    PreviousState       = $previousState
                    CurrentState        = $snapshotCurrent
                    DiffEvents          = @($diffEvents)
                    NotifiedBridgeNames = $notifiedBridgeNames.ToArray()
                }
                $checkpointState = @(Resolve-BridgeAlertSnapshot @snapshotSplat)
                $checkpointResult = Export-BridgeStatusJson -Data $checkpointState -Path $OutputFile -Configuration $Configuration
                Assert-BridgeSnapshotExport -ExportResult $checkpointResult -OutputFile $OutputFile -Cmdlet $PSCmdlet
            }
            if ($notificationFailures.Count -gt 0) {
                $PSCmdlet.ThrowTerminatingError($notificationFailures[0])
            }

            $exportBridgeStatusJsonSplat = @{
                Data          = $snapshotCurrent
                Path          = $OutputFile
                Configuration = $Configuration
            }
            $exportResult = Export-BridgeStatusJson @exportBridgeStatusJsonSplat
            Assert-BridgeSnapshotExport -ExportResult $exportResult -OutputFile $OutputFile -Cmdlet $PSCmdlet
        }
        $writeBridgeLogSplat.Message = 'Finished comparison and saved snapshot.'
        Write-BridgeLog @writeBridgeLogSplat -Configuration $Configuration
        return $currentState
    }
}