# convertScmAlarm.ps1 orig_file new_file
#----------------------------------------------
# Script conversione alarm list
#
# parte da file di input (tipicamente C:\Kvara\Xilog Plus Default\Country\Ita\app.msg) 
# e genera un file di output nel formato necessario per MTConnect Adapter
#----------------------------------------------
#
# abilitazione script: 
# Set-ExecutionPolicy -Scope Process -ExecutionPolicy unrestricted
#.\convertScmAlarm.ps1 .\app.msg AlarmListEsaGv.map



param(
[string]$orig_file_path = '',
[string]$new_file_path = ''
)

#$orig_file = Get-Content $orig_file_path

#Write-Output $orig_file -replace "@","###"
#Write-Output $orig_file -replace "@","###"


#(Get-Content $orig_file_path) `
#    -replace '"(\d+),(\d{1,})"', '$1.$2' `
#    -replace '@', '#' |
#  Out-File $new_file_path


(Get-Content $orig_file_path) | 
Where-Object {$_ -match '@' } |
Where-Object {$_ -notmatch '@0' } |
#cerco righe complete (3 oggetti)
Foreach-Object {$_ -replace '"', ''} |
Foreach-Object {$_ -replace "^@(\d+),(\w+),(\w+)", '$1|PLC|WARNING|[$1] $3'} | 
# cerca righe SENZA codice colore = FAULT
Foreach-Object {$_ -replace "^@(\d+),(\w?)", '$1|PLC|FAULT|[$1] $2'} | 
 
Set-Content $new_file_path

#"@21,B,M00 ATTIVO: START CICLO" -replace "^@(\d+),(\w+),(\w+)", '$1|$2|$3'
#Foreach-Object {$_ -replace '@', ''}  |
