star-cloud/mqtt-gateway/internal/bridge/redis_pusher.go
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

42 lines
827 B
Go

package bridge
import (
"context"
"encoding/json"
"log"
"time"
"github.com/redis/go-redis/v9"
)
type RedisPusher struct {
client *redis.Client
key string
}
type BridgePayload struct {
Type string `json:"type"`
SerialNo string `json:"serial_no"`
Payload interface{} `json:"payload"`
ReceivedAt string `json:"received_at"`
}
func NewRedisPusher(client *redis.Client, key string) *RedisPusher {
return &RedisPusher{client: client, key: key}
}
func (p *RedisPusher) Push(payload BridgePayload) error {
payload.ReceivedAt = time.Now().Format(time.RFC3339)
data, err := json.Marshal(payload)
if err != nil {
return err
}
ctx := context.Background()
err = p.client.RPush(ctx, p.key, data).Err()
if err != nil {
log.Printf("Failed to push to Redis: %v", err)
}
return err
}