star-cloud/simulate-heartbeat.sh
sky121113 959625a640 [FEAT] 實作 MQTT Gateway 整合與高併發 IoT 通訊架構
1. 導入 Go 語言撰寫的 MQTT Gateway (mqtt-gateway/),負責訊息接收並透過 Redis List 轉發至 Laravel。
2. 更新 compose.yaml 以包含 EMQX Broker 與 mqtt-gateway 服務,並配置相關環境變數。
3. 新增 ListenMqttQueue 指令與對應的異步處理 Job (ProcessHeartbeat, ProcessTransaction, ProcessMachineError)。
4. 實作 MqttSyncAuth 指令與 Machine Model Observer,支援自動化的機台認證資訊同步至 Redis。
5. 新增 MqttCommandService 用於處理雲端至機台的即時指令下發。
6. 更新專案規範文件 (framework.md 與 Skills) 以反映新的 MQTT 通訊機制與安全性規範。
7. 提供 simulate-heartbeat.sh 指令碼以供開發與測試使用。
2026-04-16 14:28:42 +08:00

41 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# =================================================================
# Star Cloud MQTT Heartbeat Simulator
# =================================================================
# 用於模擬機台每 10 秒發送一次心跳包。
# 依賴Docker (使用 eclipse-mosquitto 鏡像進行發送)
# =================================================================
SERIAL_NO="SN00001"
API_TOKEN="8QwOrmKX6sLmR5T1sChfvGjfc8eS4qaclOFd4ocItmeryii7SFqcC5uMpnzg"
TOPIC="machine/${SERIAL_NO}/heartbeat"
NETWORK="star-cloud_sail"
BROKER_HOST="emqx"
echo "🚀 開始模擬心跳發報 [${SERIAL_NO}]..."
echo "📡 頻率:每 10 秒一次"
echo "📝 Topic: ${TOPIC}"
echo "-------------------------------------------------"
while true; do
TIMESTAMP=$(date +"%Y-%m-%dT%H:%M:%S+08:00")
PAYLOAD="{\"current_page\":2,\"firmware_version\":\"1.0.6\",\"temperature\":$(awk "BEGIN {srand(); print 20+rand()*10}")}"
echo "[${TIMESTAMP}] 正在發送心跳..."
docker run --rm --network ${NETWORK} eclipse-mosquitto \
mosquitto_pub -h ${BROKER_HOST} -p 1883 \
-u "${SERIAL_NO}" -P "${API_TOKEN}" \
-t "${TOPIC}" -m "${PAYLOAD}"
if [ $? -eq 0 ]; then
echo "✅ 發送成功"
else
echo "❌ 發送失敗,請檢查 Docker 網路或 EMQX 狀態"
fi
echo "💤 等待 10 秒..."
sleep 10
done