fix: 自启动

This commit is contained in:
2026-04-14 22:06:16 +08:00
parent d574ba3414
commit b91376663a
8 changed files with 769 additions and 0 deletions

273
WINDOWS_RECEIVER_SETUP.md Normal file
View File

@@ -0,0 +1,273 @@
# Windows 接收端环境配置
这份文档只针对接收方,也就是你的 Windows 电脑。
本文假设:
- `xMonitor` 仓库就在 Windows 上
- 你的电脑在手机热点网络里的固定 IP 是 `10.0.0.5`
- 发送端会连到 `10.0.0.5:9000`
- 你希望在 Windows 上运行 `main_monitor.py` 来接收机器人状态并在浏览器里显示
---
## 1. 安装 Python
建议使用 Python 3.10 或更新版本。
在 PowerShell 里确认 Python 可用:
```powershell
py -V
```
如果没有安装 Python
1. 打开 https://www.python.org/downloads/windows/
2. 安装最新版 Python 3
3. 安装时勾选 `Add Python to PATH`
---
## 2. 进入项目目录
在 PowerShell 中进入仓库目录:
```powershell
cd C:\Users\64187\Desktop\Workspace\xMonitor
```
---
## 3. 创建并启用虚拟环境
创建虚拟环境:
```powershell
py -3 -m venv .venv
```
启用虚拟环境:
```powershell
.\.venv\Scripts\Activate.ps1
```
如果 PowerShell 阻止脚本执行,可先临时放开当前会话:
```powershell
Set-ExecutionPolicy -Scope Process Bypass
.\.venv\Scripts\Activate.ps1
```
启用后命令行前面通常会出现 `(.venv)`
---
## 4. 安装接收端依赖
`main_monitor.py` 接收端只需要这两个包:
- `fastapi`
- `uvicorn`
安装命令:
```powershell
python -m pip install --upgrade pip
pip install fastapi uvicorn
```
---
## 5. 启动接收端
因为你的发送端现在要往 `10.0.0.5:9000` 发,所以接收端必须监听 `9000` 端口。
在 PowerShell 中运行:
```powershell
uvicorn main_monitor:app --host 0.0.0.0 --port 9000
```
说明:
- `--host 0.0.0.0`:允许热点网络里的设备访问这台 Windows 电脑
- `--port 9000`:和发送端启动参数保持一致
如果你以后改了发送端端口,这里的端口也必须一起改。
---
## 6. 放行 Windows 防火墙
第一次启动时Windows 可能会弹出防火墙提示。
请选择:
- 允许访问
- 至少勾选当前使用的网络类型
如果没有弹窗,可以手动放行 TCP 9000
```powershell
New-NetFirewallRule `
-DisplayName "xMonitor 9000" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 9000 `
-Action Allow
```
查看规则是否创建成功:
```powershell
Get-NetFirewallRule -DisplayName "xMonitor 9000"
```
---
## 7. 检查本机热点 IP
确认 Windows 当前热点网卡上的 IP 确实是你要给发送端使用的固定地址。
查看 IP
```powershell
ipconfig
```
确认对应热点网络适配器上有:
```text
IPv4 Address . . . . . . . . . . : 10.0.0.5
```
如果不是这个地址,要么把 Windows 侧固定 IP 配好,要么把发送端里的 `--ip` 改成实际地址。
---
## 8. 打开监控页面
接收端启动成功后,在本机浏览器打开:
```text
http://127.0.0.1:9000
```
或者直接打开:
```text
http://10.0.0.5:9000
```
如果发送端已经开始推数据,页面会显示机器人状态。
---
## 9. 验证接收端是否正常工作
### 看端口是否监听
```powershell
netstat -ano | findstr :9000
```
能看到 `LISTENING` 说明服务已经起来了。
### 看浏览器页面是否能打开
打开:
```text
http://127.0.0.1:9000
```
若页面能打开,说明 HTTP 服务正常。
### 看是否收到机器人数据
接收端会把机器人上报的数据写到:
```text
logs\robot_packets.jsonl
```
可以在项目目录下查看:
```powershell
Get-Content .\logs\robot_packets.jsonl -Tail 5
```
如果这里不断有新行,说明 WebSocket 接收正常。
---
## 10. 常见问题
### 1) 浏览器能打开页面,但没有机器人数据
通常检查这几项:
- 发送端是否真的发到了 `10.0.0.5:9000`
- Windows 防火墙是否放行了 `9000`
- 电脑和设备是否确实连在同一个手机热点下
- Windows 当前热点 IP 是否还是 `10.0.0.5`
### 2) PowerShell 里 `Activate.ps1` 不能执行
先执行:
```powershell
Set-ExecutionPolicy -Scope Process Bypass
```
然后再执行:
```powershell
.\.venv\Scripts\Activate.ps1
```
### 3) `uvicorn` 命令找不到
通常是虚拟环境没激活,或者依赖没装成功。
重新执行:
```powershell
.\.venv\Scripts\Activate.ps1
pip install fastapi uvicorn
```
### 4) 9000 端口被占用
查看占用:
```powershell
netstat -ano | findstr :9000
```
如果必须换端口,比如改成 `8000`,那就要同时改两边:
- Windows 接收端启动端口
- 发送端 `monitor_sender.py --port ...`
---
## 11. 最小启动流程
以后你在 Windows 上最少只需要这几步:
```powershell
cd C:\Users\64187\Desktop\Workspace\xMonitor
.\.venv\Scripts\Activate.ps1
uvicorn main_monitor:app --host 0.0.0.0 --port 9000
```
然后浏览器打开:
```text
http://127.0.0.1:9000
```
如果需要我下一步可以再给你补一份“Windows 接收端开机自启”文档,直接做成任务计划程序版本。