# CI-Eng-Sample live data: writes test values into the sample items so displays, trends and alarms # move (internal items stay NOT INIT after a CI Server start). CI Server R1.03.00. Replaces # displays\trend_feed.ps1. Not part of the CI Server package: installed with install_feed.ps1 into # C:\Users\Public\CI-Eng-Sample\feed, from where the display button "Start" runs it. # # Usage: powershell -ExecutionPolicy Bypass -File cieng_feed.ps1 [-Seconds 3600] [-UntilFlag] [-Alarms] [-System] [-Cycle 5] # (default) run for -Seconds (3600) # -UntilFlag run while CI-Eng-Sample.DEMO.FEED_ON = 1 (display buttons Start / Stop); sets it to 1 # at start and 0 at the end # -Alarms LEVEL 50 +/- 33 % (crosses L 20 and H 80: alarms that need an acknowledgement); # without it 50 +/- 25 % (stays inside the limits) # -System also write SYSTEM.CPU_LOAD / DISK_C / PING_LOCALHOST / PROC_ALM with this PC's real # values - skipped automatically while the monitoring processes (intcpumon, intdskmon, # inttcpmon, intmon; see ..\monitoring) are running, so they do not overwrite each other # Every cycle (default 5 s): TANK1.LEVEL (sine, 10 min), TANK1.INFLOW (250 +/- 150 m3/h, 4 min); # PUMP1.RUNNING = 1 while INFLOW > 250, only in auto mode (PUMP1.MODE = 1). # Only one copy runs at a time; log: %TLS_LST_PATH%\cieng_feed.log (start / stop lines). param([int]$Seconds = 3600, [switch]$UntilFlag, [switch]$Alarms, [switch]$System, [int]$Cycle = 5) $itmval = 'itmval' if ($env:TLS_ROOT_PATH) { $exe = Join-Path $env:TLS_ROOT_PATH 'tls\exe\itmval.exe' if (Test-Path $exe) { $itmval = $exe } } $S = 'CI-Eng-Sample.' $logDir = $env:TLS_LST_PATH if (-not $logDir) { $logDir = $PSScriptRoot } $log = Join-Path $logDir 'cieng_feed.log' function Get-Val([string]$name) { $out = & $itmval -g -n ($S + $name) 2>$null foreach ($l in $out) { if ($l -match '^Value\s*=\s*(.+)$') { return $Matches[1].Trim() } } return $null } function Put([string]$name, $value) { & $itmval -p -n ($S + $name) -v $value > $null 2>&1 } function Write-Log([string]$text) { try { Add-Content -Path $log -Value ('{0} {1}' -f (Get-Date -Format s), $text) } catch { } } $created = $false $mutex = New-Object Threading.Mutex($true, 'Global\CIENG_FEED', [ref]$created) if (-not $created) { Write-Log 'already running - this copy exits'; exit 0 } $amp = 25 if ($Alarms) { $amp = 33 } $monitors = @('intcpumon', 'intdskmon', 'inttcpmon', 'intmon') $mode = 'seconds=' + $Seconds if ($UntilFlag) { $mode = 'until FEED_ON = 0' } $end = (Get-Date).AddSeconds($Seconds) try { Write-Log ('start ({0}, alarms={1}, system={2}, user {3})' -f $mode, [bool]$Alarms, [bool]$System, [Environment]::UserName) if ($UntilFlag) { Put 'DEMO.FEED_ON' 1 } while ($true) { if ($UntilFlag) { if ((Get-Val 'DEMO.FEED_ON') -ne '1') { break } } elseif ((Get-Date) -ge $end) { break } $t = [double]((Get-Date) - (Get-Date).Date).TotalSeconds $level = [math]::Round(50 + $amp * [math]::Sin(2 * [math]::PI * $t / 600), 1) $inflow = [math]::Round(250 + 150 * [math]::Sin(2 * [math]::PI * $t / 240), 1) Put 'TANK1.LEVEL' $level Put 'TANK1.INFLOW' $inflow if ((Get-Val 'PUMP1.MODE') -eq '1') { Put 'PUMP1.RUNNING' ([int]($inflow -gt 250)) } if ($System -and -not (Get-Process -Name $monitors -ErrorAction SilentlyContinue)) { try { $cpus = @(Get-CimInstance Win32_Processor) $cpu = 0; foreach ($c in $cpus) { $cpu += $c.LoadPercentage }; $cpu = $cpu / [math]::Max(1, $cpus.Count) Put 'SYSTEM.CPU_LOAD' ([math]::Round($cpu, 1)) $d = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" Put 'SYSTEM.DISK_C' ([math]::Round(100 * ($d.Size - $d.FreeSpace) / $d.Size, 1)) Put 'SYSTEM.PING_LOCALHOST' ([int](Test-Connection -ComputerName localhost -Count 1 -Quiet)) Put 'SYSTEM.PROC_ALM' ([int]([bool](Get-Process alm -ErrorAction SilentlyContinue))) } catch { } } Start-Sleep -Seconds $Cycle } } finally { if ($UntilFlag) { Put 'DEMO.FEED_ON' 0 } Write-Log 'stop' $mutex.ReleaseMutex() }