netgescon-day0/scripts/ops/windows/start-netgescon-panasonic-live.ps1

112 lines
3.4 KiB
PowerShell

<#
Avvia il bridge live Panasonic -> NetGescon in loop continuo.
Cosa fa:
1. risolve watcher, token, log e cartella interop
2. avvia il watcher TAPI .NET in modalita live
3. se il watcher termina con errore, aspetta pochi secondi e riparte
Note operative:
- va eseguito preferibilmente da un percorso locale Windows, non da drive mappati tipo S:
- il task schedulato puo richiamare questo script in sicurezza con -ExecutionPolicy Bypass
#>
param(
[string]$BaseUrl = 'https://staging.netgescon.it',
[string]$Extensions = '201-208,601,603,0001,0003',
[string]$LogFile = '.\netgescon-tapi-dotnet-events-live.log',
[string]$InteropOutputDir = '',
[string]$Token = '',
[int]$RestartDelaySeconds = 5,
[switch]$IncludeDiagnosticEvents
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Ensure-ParentDirectory {
param([string]$Path)
if ([string]::IsNullOrWhiteSpace($Path)) {
return
}
$parent = Split-Path -Path $Path -Parent
if (-not [string]::IsNullOrWhiteSpace($parent) -and -not (Test-Path $parent)) {
New-Item -ItemType Directory -Path $parent -Force | Out-Null
}
}
function Ensure-Directory {
param([string]$Path)
if ([string]::IsNullOrWhiteSpace($Path)) {
return
}
if (-not (Test-Path $Path)) {
New-Item -ItemType Directory -Path $Path -Force | Out-Null
}
}
$scriptPath = Join-Path $PSScriptRoot 'watch-netgescon-panasonic-tapi-dotnet-events.ps1'
if (-not (Test-Path $scriptPath)) {
throw "Watcher non trovato: $scriptPath"
}
if ([string]::IsNullOrWhiteSpace($Token)) {
$Token = [string]([Environment]::GetEnvironmentVariable('NETGESCON_CTI_SHARED_TOKEN', 'Machine'))
}
if ([string]::IsNullOrWhiteSpace($Token)) {
$Token = [string]([Environment]::GetEnvironmentVariable('NETGESCON_CTI_SHARED_TOKEN', 'User'))
}
if ([string]::IsNullOrWhiteSpace($Token)) {
throw 'Token mancante. Imposta NETGESCON_CTI_SHARED_TOKEN a livello User o Machine, oppure passa -Token.'
}
if ([string]::IsNullOrWhiteSpace($InteropOutputDir)) {
if (-not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
$InteropOutputDir = Join-Path $env:LOCALAPPDATA 'NetGescon\tapi-interop'
}
else {
$InteropOutputDir = Join-Path $PSScriptRoot 'tapi-interop'
}
}
elseif (-not [System.IO.Path]::IsPathRooted($InteropOutputDir)) {
$InteropOutputDir = Join-Path $PSScriptRoot $InteropOutputDir
}
Ensure-ParentDirectory -Path $LogFile
Ensure-Directory -Path $InteropOutputDir
while ($true) {
# Ciclo supervisore: il watcher gestisce la logica TAPI, questo launcher lo riavvia se cade.
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Write-Host "$timestamp | AVVIO bridge live Panasonic -> NetGescon"
try {
$params = @{
Extensions = $Extensions
LogFile = $LogFile
InteropOutputDir = $InteropOutputDir
BridgeMode = 'live'
BaseUrl = $BaseUrl
Token = $Token
}
if ($IncludeDiagnosticEvents.IsPresent) {
$params.IncludeDiagnosticEvents = $true
}
& $scriptPath @params
}
catch {
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Write-Host "$timestamp | ERRORE bridge live: $($_.Exception.Message)"
}
# Evita restart-loop aggressivo in caso di errore ripetuto.
Start-Sleep -Seconds ([Math]::Max(1, $RestartDelaySeconds))
}