Moki hai 2 semanas
pai
achega
223218fc03
Modificáronse 6 ficheiros con 154 adicións e 129 borrados
  1. 22 10
      README.md
  2. 1 1
      go.mod
  3. 64 1
      scripts/build.bat
  4. 67 30
      scripts/build.sh
  5. 0 50
      scripts/build_ble_relay.bat
  6. 0 37
      scripts/build_ble_relay.sh

+ 22 - 10
README.md

@@ -33,14 +33,18 @@ OpenCode 状态监控工具,支持实时监控多个 OpenCode 实例的状态
 git clone <repository-url>
 cd AI-Status-Light
 
-# 编译当前平台(不含 BLE)
+# 当前平台(不含 BLE)
 go build -o bin/opencode-monitor ./cmd/monitor
 
-# 编译当前平台(含 BLE 嵌入)
-make build-with-ble
+# 当前平台(含 BLE 嵌入)
+scripts\build.bat --ble          # Windows
+bash scripts/build.sh --ble      # Linux/macOS
 
-# 编译所有平台
-make build-all
+# 仅打包 BLE 中继(供 IDE 调试用)
+scripts\build.bat --only-ble
+
+# 所有平台(不含 BLE)
+scripts\build.bat
 ```
 
 ## 使用方法
@@ -209,9 +213,9 @@ AI-Status-Light/
 │   ├── monitor/          # 监控器核心模块
 │   └── mqtt/             # MQTT 客户端模块
 ├── scripts/
+│   ├── build.bat                 # Windows 构建脚本
+│   ├── build.sh                  # Linux/macOS 构建脚本
 │   ├── ble_relay.py              # Python BLE 蓝牙中继脚本
-│   ├── build_ble_relay.bat       # Windows 打包脚本
-│   ├── build_ble_relay.sh        # Linux/macOS 打包脚本
 │   └── requirements.txt          # Python 依赖
 ├── docs/                 # 文档
 └── Makefile              # 构建配置
@@ -226,10 +230,18 @@ go mod tidy
 # 运行测试
 go test ./...
 
-# 构建
-make build
+# 构建(不含 BLE,全平台)
+scripts\build.bat
 
-# 构建所有平台
+# 构建(含 BLE,当前平台)
+scripts\build.bat --ble
+
+# 仅生成 BLE 中继(IDE 调试用)
+scripts\build.bat --only-ble
+
+# 或用 Makefile
+make build
+make build-with-ble
 make build-all
 ```
 

+ 1 - 1
go.mod

@@ -4,13 +4,13 @@ go 1.21
 
 require (
 	github.com/eclipse/paho.mqtt.golang v1.4.3
+	github.com/gorilla/websocket v1.5.0
 	modernc.org/sqlite v1.24.0
 )
 
 require (
 	github.com/dustin/go-humanize v1.0.1 // indirect
 	github.com/google/uuid v1.3.0 // indirect
-	github.com/gorilla/websocket v1.5.0 // indirect
 	github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
 	github.com/mattn/go-isatty v0.0.16 // indirect
 	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect

+ 64 - 1
scripts/build.bat

@@ -4,7 +4,69 @@ setlocal
 set BINARY_NAME=opencode-monitor
 set VERSION=dev
 
+if "%1"=="--only-ble" goto only_ble
+if "%1"=="--ble" goto ble
+goto all_platforms
+
+:only_ble
+echo === Building BLE relay only ===
+python -m pip install pyinstaller bleak
+if not exist bin mkdir bin
+python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts\ble_relay.py
+copy /Y bin\ble_relay.exe cmd\monitor\ble_relay.exe >nul
+echo.
+echo Done: bin\ble_relay.exe + cmd\monitor\ble_relay.exe
+goto end
+
+:ble
+echo === Building current platform with BLE ===
+echo Step 1: Building BLE relay...
+python -m pip install pyinstaller bleak
+if not exist bin mkdir bin
+python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts\ble_relay.py
+copy /Y bin\ble_relay.exe cmd\monitor\ble_relay.exe >nul
+echo Step 2: Building Go binary with BLE embedded...
+go mod tidy
+if not exist dist mkdir dist
+
+REM Detect architecture
+if "%PROCESSOR_ARCHITECTURE%"=="ARM64" (
+    set GOARCH=arm64
+    set SUFFIX=-windows-arm64.exe
+) else (
+    set GOARCH=amd64
+    set SUFFIX=-windows-amd64.exe
+)
+set GOOS=windows
+
+go build -tags ble -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%%SUFFIX% .\cmd\monitor
+del /Q cmd\monitor\ble_relay.exe 2>nul
+echo.
+echo Done: dist\%BINARY_NAME%%SUFFIX%
+goto end
+
+:all_platforms
+echo === Building all platforms (no BLE) ===
 if not exist dist mkdir dist
+go mod tidy
+
+echo Building for Linux (amd64)...
+set GOOS=linux
+set GOARCH=amd64
+go build -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%-linux-amd64 .\cmd\monitor
+
+echo Building for Linux (arm64)...
+set GOARCH=arm64
+go build -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%-linux-arm64 .\cmd\monitor
+
+echo Building for macOS (amd64)...
+set GOOS=darwin
+set GOARCH=amd64
+go build -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%-darwin-amd64 .\cmd\monitor
+
+echo Building for macOS (arm64)...
+set GOARCH=arm64
+go build -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%-darwin-arm64 .\cmd\monitor
 
 echo Building for Windows (amd64)...
 set GOOS=windows
@@ -16,7 +78,8 @@ set GOARCH=arm64
 go build -ldflags "-X main.Version=%VERSION%" -o dist\%BINARY_NAME%-windows-arm64.exe .\cmd\monitor
 
 echo.
-echo Build complete! Binaries in dist\
+echo Done! Binaries in dist\
 dir dist\
 
+:end
 endlocal

+ 67 - 30
scripts/build.sh

@@ -1,47 +1,84 @@
 #!/bin/bash
-
 set -e
 
+BINARY_NAME="opencode-monitor"
+VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
 PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
 
-# 构建前端(可选,需配置 WEB_DIR 环境变量指向前端项目目录)
-WEB_DIR="${WEB_DIR:-}"
-HTML_DIR="$PROJECT_DIR/internal/web/html"
+cd "$PROJECT_DIR"
 
-if [ -n "$WEB_DIR" ] && [ -d "$WEB_DIR" ]; then
-    echo "Building frontend..."
-    cd "$WEB_DIR" && npm run build
-    rm -rf "$HTML_DIR"
-    cp -r dist "$HTML_DIR"
-    echo "Frontend built and copied."
-fi
+detect_platform() {
+    local os arch suffix
+    os=$(uname -s | tr '[:upper:]' '[:lower:]')
+    arch=$(uname -m)
+    case "$os" in
+        linux*)  os="linux" ;;
+        darwin*) os="darwin" ;;
+        *)       os="windows" ;;
+    esac
+    case "$arch" in
+        x86_64|amd64) arch="amd64" ;;
+        aarch64|arm64) arch="arm64" ;;
+        *)             arch="amd64" ;;
+    esac
+    if [ "$os" = "windows" ]; then
+        suffix=".exe"
+    else
+        suffix=""
+    fi
+    echo "${os}-${arch}${suffix}"
+}
 
-cd "$PROJECT_DIR"
+if [ "$1" = "--only-ble" ]; then
+    echo "=== Building BLE relay only ==="
+    python3 -m pip install pyinstaller bleak || python -m pip install pyinstaller bleak
+    mkdir -p bin
+    python3 -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py || python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py
+    cp bin/ble_relay cmd/monitor/ble_relay
+    echo ""
+    echo "Done: bin/ble_relay + cmd/monitor/ble_relay"
 
-BINARY_NAME="opencode-monitor"
-VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
+elif [ "$1" = "--ble" ]; then
+    PLATFORM=$(detect_platform)
+    echo "=== Building for ${PLATFORM} with BLE ==="
+    echo "Step 1: Building BLE relay..."
+    python3 -m pip install pyinstaller bleak || python -m pip install pyinstaller bleak
+    mkdir -p bin
+    python3 -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py || python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py
+    cp bin/ble_relay cmd/monitor/ble_relay
+    echo "Step 2: Building Go binary with BLE embedded..."
+    go mod tidy
+    mkdir -p dist
+    go build -tags ble -ldflags "-X main.Version=${VERSION}" -o "dist/${BINARY_NAME}-${PLATFORM}" ./cmd/monitor
+    rm -f cmd/monitor/ble_relay
+    echo ""
+    echo "Done: dist/${BINARY_NAME}-${PLATFORM}"
 
-mkdir -p dist
+else
+    echo "=== Building all platforms (no BLE) ==="
+    mkdir -p dist
+    go mod tidy
 
-echo "Building for Linux (amd64)..."
-GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-amd64 ./cmd/monitor
+    echo "Building for Linux (amd64)..."
+    GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-amd64 ./cmd/monitor
 
-echo "Building for Linux (arm64)..."
-GOOS=linux GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-arm64 ./cmd/monitor
+    echo "Building for Linux (arm64)..."
+    GOOS=linux GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-arm64 ./cmd/monitor
 
-echo "Building for macOS (amd64)..."
-GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-amd64 ./cmd/monitor
+    echo "Building for macOS (amd64)..."
+    GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-amd64 ./cmd/monitor
 
-echo "Building for macOS (arm64)..."
-GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-arm64 ./cmd/monitor
+    echo "Building for macOS (arm64)..."
+    GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-arm64 ./cmd/monitor
 
-echo "Building for Windows (amd64)..."
-GOOS=windows GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-amd64.exe ./cmd/monitor
+    echo "Building for Windows (amd64)..."
+    GOOS=windows GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-amd64.exe ./cmd/monitor
 
-echo "Building for Windows (arm64)..."
-GOOS=windows GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-arm64.exe ./cmd/monitor
+    echo "Building for Windows (arm64)..."
+    GOOS=windows GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-arm64.exe ./cmd/monitor
 
-echo ""
-echo "Build complete! Binaries in dist/"
-ls -la dist/
+    echo ""
+    echo "Done! Binaries in dist/"
+    ls -la dist/
+fi

+ 0 - 50
scripts/build_ble_relay.bat

@@ -1,50 +0,0 @@
-@echo off
-setlocal
-
-echo === BLE Relay Build Script ===
-echo.
-
-REM Check Python
-where python >nul 2>nul
-if %errorlevel% neq 0 (
-    echo ERROR: Python not found. Please install Python 3 and add to PATH.
-    exit /b 1
-)
-
-REM Install dependencies
-echo Installing dependencies...
-python -m pip install pyinstaller bleak
-if %errorlevel% neq 0 (
-    echo ERROR: Failed to install dependencies.
-    exit /b 1
-)
-
-REM Create output dir
-if not exist bin mkdir bin
-
-REM Build
-echo.
-echo Building ble_relay.exe...
-python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts\ble_relay.py
-if %errorlevel% neq 0 (
-    echo ERROR: PyInstaller build failed.
-    exit /b 1
-)
-
-REM Copy to cmd/monitor for go:embed
-echo.
-echo Copying to cmd/monitor/ for embedding...
-copy /Y bin\ble_relay.exe cmd\monitor\ble_relay.exe >nul
-if %errorlevel% neq 0 (
-    echo ERROR: Failed to copy ble_relay.exe
-    exit /b 1
-)
-
-echo.
-echo === Build complete ===
-echo   bin\ble_relay.exe
-echo   cmd\monitor\ble_relay.exe
-echo.
-echo Now run: go build -tags ble -o bin\opencode-monitor.exe .\cmd\monitor
-
-endlocal

+ 0 - 37
scripts/build_ble_relay.sh

@@ -1,37 +0,0 @@
-#!/bin/bash
-set -e
-
-echo "=== BLE Relay Build Script ==="
-echo ""
-
-# Check Python
-if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
-    echo "ERROR: Python not found. Please install Python 3."
-    exit 1
-fi
-
-PYTHON=$(command -v python3 || command -v python)
-
-# Install dependencies
-echo "Installing dependencies..."
-$PYTHON -m pip install pyinstaller bleak
-
-# Create output directory
-mkdir -p bin
-
-# Build
-echo ""
-echo "Building ble_relay..."
-$PYTHON -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py
-
-# Copy to cmd/monitor for go:embed
-echo ""
-echo "Copying to cmd/monitor/ for embedding..."
-cp bin/ble_relay cmd/monitor/ble_relay
-
-echo ""
-echo "=== Build complete ==="
-echo "  bin/ble_relay"
-echo "  cmd/monitor/ble_relay"
-echo ""
-echo "Now run: go build -tags ble -o bin/opencode-monitor ./cmd/monitor"