diff --git a/install.ps1 b/install.ps1 index ccb0447..e1a4560 100644 --- a/install.ps1 +++ b/install.ps1 @@ -1,14 +1,22 @@ -# 建立目的資料夾 +# Set up target folder $targetDir = "C:\scripts" if (-not (Test-Path $targetDir)) { New-Item -Path $targetDir -ItemType Directory | Out-Null } -# 複製腳本與設定檔 -Copy-Item ".\scripts\heartbeat.ps1" -Destination "$targetDir\heartbeat.ps1" -Force -Copy-Item ".\scripts\config.json" -Destination "$targetDir\config.json" -Force +# Always overwrite heartbeat.ps1 +$source1 = Resolve-Path ".\scripts\heartbeat.ps1" +$dest1 = "$targetDir\heartbeat.ps1" +Copy-Item $source1 -Destination $dest1 -Force -# 設定開機自動執行任務 +# Only copy config.json if it does not exist +$source2 = Resolve-Path ".\scripts\config.json" +$dest2 = "$targetDir\config.json" +if (-not (Test-Path $dest2)) { + Copy-Item $source2 -Destination $dest2 -Force +} + +# Set up scheduled task to run on system startup $taskName = "KtvHeartbeat" $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$targetDir\heartbeat.ps1`"" $trigger = New-ScheduledTaskTrigger -AtStartup @@ -16,4 +24,4 @@ $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName -Force -Write-Host "✅ 安裝完成:腳本已部署並設為開機自動執行。" -ForegroundColor Green \ No newline at end of file +Write-Host "ok Installation Complete: The script has been deployed and will run at system startup." -ForegroundColor Green diff --git a/scripts/config.json b/scripts/config.json index fefcb30..1706c1e 100644 --- a/scripts/config.json +++ b/scripts/config.json @@ -1,5 +1,5 @@ { - "apiUrl": "https://ktv.test", + "apiUrl": "https://superstar.dnsnet.cc", "branchId": 1, "roomName": "102", "email": "MachineKTV@gmail.com", diff --git a/scripts/heartbeat.ps1 b/scripts/heartbeat.ps1 index aebf958..e4b6af1 100644 --- a/scripts/heartbeat.ps1 +++ b/scripts/heartbeat.ps1 @@ -1,19 +1,29 @@ -# 讀取 config 檔 +$logPath = "C:\scripts\heartbeat.log" + +function Log($msg) { + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + Add-Content -Path $logPath -Value "[$timestamp] $msg" +} + +# Load config $configPath = "C:\scripts\config.json" if (!(Test-Path $configPath)) { - Write-Error "❌ 找不到設定檔:$configPath" + Log "error Config file not found: $configPath" exit 1 } + $config = Get-Content $configPath | ConvertFrom-Json -# 使用設定值 -$apiUrl = $config.apiUrl +# Config values +$apiUrl = $config.apiUrl.TrimEnd('/') # 移除結尾的 / $branchId = $config.branchId $roomName = $config.roomName $email = $config.email $password = $config.password -# 取得本機 IP +Log "📡 Using API URL: $apiUrl" + +# Get IP address $ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*" }).IPAddress @@ -22,18 +32,27 @@ function Get-Token { $body = @{ branch_id = $branchId room_name = $roomName + room_ip = $ip email = $email password = $password } | ConvertTo-Json -Depth 3 - $response = Invoke-RestMethod -Uri "$apiUrl/api/room/receiveRegister" -Method Post ` - -Body $body -ContentType "application/json" + $url = "$apiUrl/api/room/receiveRegister" + Log "curl Requesting token from: $url" - if ($response.token) { - Write-Output "✅ Token 取得成功:$($response.token)" - return $response.token - } else { - Write-Error "❌ 無法取得 Token" + try { + $response = Invoke-RestMethod -Uri $url -Method Post ` + -Body $body -ContentType "application/json" + + if ($response.data.token) { + Log "ok Token acquired: $($response.data.token)" + return $response.data.token + } else { + Log "error Failed to acquire token (no token in response)" + exit 1 + } + } catch { + Log "error Token request failed: $($_.Exception.Message)" exit 1 } } @@ -54,19 +73,15 @@ function Send-Heartbeat($token, $ip) { -Headers @{ Authorization = "Bearer $token" } ` -Body $heartbeat -ContentType "application/json" - Write-Output "✅ 心跳送出:$(Get-Date)" + Log "ok Heartbeat sent successfully" } catch { - Write-Error "❌ 心跳失敗:$($_.Exception.Message)" + Log "error Heartbeat failed: $($_.Exception.Message)" } Start-Sleep -Seconds 60 } } -# 執行主流程 +# Main $token = Get-Token - -while ($true) { - Send-Heartbeat -token $token -ip $ip - Start-Sleep -Seconds 60 -} \ No newline at end of file +Send-Heartbeat -token $token -ip $ip \ No newline at end of file diff --git a/操作說明.txt b/操作說明.txt index 647f376..8904a50 100644 --- a/操作說明.txt +++ b/操作說明.txt @@ -9,4 +9,20 @@ • 安裝後,腳本會每次開機自動執行並回傳心跳。 • 若 config.json 中有變更,只需更新該檔案即可。 • 若需卸載,只要刪除工作排程即可: - Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false \ No newline at end of file + Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false + + +Set-ExecutionPolicy RemoteSigned -Scope Process -Force +& "C:\KtvSetup\install.ps1" + +Get-ScheduledTask -TaskName "KtvHeartbeat" | Select-Object TaskName, State +若狀態是 Disabled,可使用以下指令啟用: +Enable-ScheduledTask -TaskName "KtvHeartbeat" +然後再次查看執行結果 +Get-ScheduledTask -TaskName "KtvHeartbeat" | Get-ScheduledTaskInfo + + +手動測試任務能否正常執行 +Start-ScheduledTask -TaskName "KtvHeartbeat" + +Stop-ScheduledTask -TaskName "KtvHeartbeat" \ No newline at end of file