diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..ac59586 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index e69de29..647f376 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,12 @@ +⚙️ 安裝步驟: + 1. 準備:將 heartbeat.ps1 與 config.json 放入 scripts 資料夾 + 2. 打包:整個 KtvSetup 資料夾複製到每台設備 + 3. 安裝:右鍵以「系統管理員身份」執行 install.ps1 + +⸻ + +📌 注意事項: + • 安裝後,腳本會每次開機自動執行並回傳心跳。 + • 若 config.json 中有變更,只需更新該檔案即可。 + • 若需卸載,只要刪除工作排程即可: + Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false \ No newline at end of file diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000..ccb0447 --- /dev/null +++ b/install.ps1 @@ -0,0 +1,19 @@ +# 建立目的資料夾 +$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 + +# 設定開機自動執行任務 +$taskName = "KtvHeartbeat" +$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$targetDir\heartbeat.ps1`"" +$trigger = New-ScheduledTaskTrigger -AtStartup +$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 diff --git a/scripts/.DS_Store b/scripts/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/scripts/.DS_Store differ diff --git a/scripts/config.json b/scripts/config.json new file mode 100644 index 0000000..fefcb30 --- /dev/null +++ b/scripts/config.json @@ -0,0 +1,7 @@ +{ + "apiUrl": "https://ktv.test", + "branchId": 1, + "roomName": "102", + "email": "MachineKTV@gmail.com", + "password": "aa147258-" +} \ No newline at end of file diff --git a/scripts/heartbeat.ps1 b/scripts/heartbeat.ps1 new file mode 100644 index 0000000..aebf958 --- /dev/null +++ b/scripts/heartbeat.ps1 @@ -0,0 +1,72 @@ +# 讀取 config 檔 +$configPath = "C:\scripts\config.json" +if (!(Test-Path $configPath)) { + Write-Error "❌ 找不到設定檔:$configPath" + exit 1 +} +$config = Get-Content $configPath | ConvertFrom-Json + +# 使用設定值 +$apiUrl = $config.apiUrl +$branchId = $config.branchId +$roomName = $config.roomName +$email = $config.email +$password = $config.password + +# 取得本機 IP +$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { + $_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*" +}).IPAddress + +function Get-Token { + $body = @{ + branch_id = $branchId + room_name = $roomName + email = $email + password = $password + } | ConvertTo-Json -Depth 3 + + $response = Invoke-RestMethod -Uri "$apiUrl/api/room/receiveRegister" -Method Post ` + -Body $body -ContentType "application/json" + + if ($response.token) { + Write-Output "✅ Token 取得成功:$($response.token)" + return $response.token + } else { + Write-Error "❌ 無法取得 Token" + exit 1 + } +} + +function Send-Heartbeat($token, $ip) { + while ($true) { + $heartbeat = @{ + hostname = $env:COMPUTERNAME + ip = $ip + cpu = [math]::Round((Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue, 2) + memory = [math]::Round((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue, 2) + disk = [math]::Round((Get-PSDrive -Name C).Used / 1MB, 2) + status = "online" + } | ConvertTo-Json -Depth 3 + + try { + Invoke-RestMethod -Uri "$apiUrl/api/room/heartbeat" -Method Post ` + -Headers @{ Authorization = "Bearer $token" } ` + -Body $heartbeat -ContentType "application/json" + + Write-Output "✅ 心跳送出:$(Get-Date)" + } catch { + Write-Error "❌ 心跳失敗:$($_.Exception.Message)" + } + + Start-Sleep -Seconds 60 + } +} + +# 執行主流程 +$token = Get-Token + +while ($true) { + Send-Heartbeat -token $token -ip $ip + Start-Sleep -Seconds 60 +} \ No newline at end of file diff --git a/操作說明.txt b/操作說明.txt new file mode 100644 index 0000000..647f376 --- /dev/null +++ b/操作說明.txt @@ -0,0 +1,12 @@ +⚙️ 安裝步驟: + 1. 準備:將 heartbeat.ps1 與 config.json 放入 scripts 資料夾 + 2. 打包:整個 KtvSetup 資料夾複製到每台設備 + 3. 安裝:右鍵以「系統管理員身份」執行 install.ps1 + +⸻ + +📌 注意事項: + • 安裝後,腳本會每次開機自動執行並回傳心跳。 + • 若 config.json 中有變更,只需更新該檔案即可。 + • 若需卸載,只要刪除工作排程即可: + Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false \ No newline at end of file