PCM_Report/run_temperature_table.ps1

103 lines
3.2 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# PowerShell脚本运行temperature_table.py并设置正确的环境变量
# 用法: .\run_temperature_table.ps1 "2025-11-27T11:30:00" "2025-11-27T12:00:00"
param(
[Parameter(Mandatory=$true)]
[string]$StartTime,
[Parameter(Mandatory=$true)]
[string]$EndTime
)
Write-Host "运行 temperature_table.py 脚本" -ForegroundColor Green
Write-Host "=" * 50
# 从default.json加载InfluxDB配置
$configPath = Join-Path $PSScriptRoot "default.json"
try {
$config = Get-Content $configPath -Raw | ConvertFrom-Json
$influxConfig = $config.influx
Write-Host "InfluxDB配置加载成功" -ForegroundColor Green
} catch {
Write-Host "错误: 无法加载InfluxDB配置: $_" -ForegroundColor Red
exit 1
}
# 时间转换函数本地时间UTC+8转UTC
function Convert-LocalToUTC {
param([string]$LocalTime)
try {
# 解析本地时间
$localDt = [DateTime]::ParseExact($LocalTime, "yyyy-MM-ddTHH:mm:ss", $null)
# 假设是UTC+8时区
$localDt = [DateTime]::SpecifyKind($localDt, [DateTimeKind]::Unspecified)
$utcDt = $localDt.AddHours(-8) # 减去8小时得到UTC
return $utcDt.ToString("yyyy-MM-ddTHH:mm:ssZ")
} catch {
Write-Host "时间转换失败: $_" -ForegroundColor Yellow
return $LocalTime
}
}
# 转换时间
$utcStart = Convert-LocalToUTC $StartTime
$utcEnd = Convert-LocalToUTC $EndTime
Write-Host "本地时间: $StartTime$EndTime"
Write-Host "UTC时间: $utcStart$utcEnd"
Write-Host ""
# 设置环境变量
$env:INFLUX_URL = $influxConfig.url
$env:INFLUX_ORG = $influxConfig.org
$env:INFLUX_TOKEN = $influxConfig.token
$env:INFLUX_BUCKET = $influxConfig.bucket
$env:INFLUX_MEASUREMENT = $influxConfig.measurement
$env:EXPERIMENT_START = $utcStart
$env:EXPERIMENT_END = $utcEnd
$env:TABLE_LOG_LEVEL = "DEBUG"
$env:TABLE_LOG_FILE = "temperature_table.log"
Write-Host "环境变量设置:"
Write-Host " INFLUX_URL: $($env:INFLUX_URL)"
Write-Host " INFLUX_ORG: $($env:INFLUX_ORG)"
Write-Host " INFLUX_BUCKET: $($env:INFLUX_BUCKET)"
Write-Host " INFLUX_MEASUREMENT: $($env:INFLUX_MEASUREMENT)"
Write-Host " EXPERIMENT_START: $($env:EXPERIMENT_START)"
Write-Host " EXPERIMENT_END: $($env:EXPERIMENT_END)"
$maskedToken = $env:INFLUX_TOKEN.Substring(0, 8) + "****"
Write-Host " INFLUX_TOKEN: $maskedToken"
Write-Host ""
# 运行脚本
$scriptPath = Join-Path $PSScriptRoot "temperature_table.py"
Write-Host "正在运行脚本..." -ForegroundColor Yellow
try {
$result = & python $scriptPath 2>&1
$exitCode = $LASTEXITCODE
Write-Host "返回码: $exitCode" -ForegroundColor $(if ($exitCode -eq 0) { "Green" } else { "Red" })
Write-Host ""
if ($result) {
Write-Host "脚本输出:"
$result | ForEach-Object { Write-Host " $_" }
}
# 显示日志文件
$logFile = "temperature_table.log"
if (Test-Path $logFile) {
Write-Host ""
Write-Host "日志文件最后10行:" -ForegroundColor Cyan
Get-Content $logFile -Tail 10 | ForEach-Object { Write-Host " $_" }
}
exit $exitCode
} catch {
Write-Host "错误: 运行脚本失败: $_" -ForegroundColor Red
exit 1
}