feat(device): 增加设备信息持久化和OTA升级功能

- 新增设备持久化定时任务,每5分钟将Redis中采集的设备信息同步数据库
- 新增UserDevice和UserActive模型及相应数据库表和操作接口
- 设备信息上报中获取客户端公网IP,替代原X-Forwarded-For头部
- 新建OTA功能模块,实现OTA固件升级信息查询接口
- 支持OTA黑名单过滤与定向升级设备判断
- 设计OTA相关数据库结构:ota、black_list、ota_target_device表
- 缓存预热新增按品牌分组预热型号缓存
- 依赖注入新增OTA Repository及Handler路由配置
- 实现客户端公网IP提取逻辑,支持多种代理头部优先级识别
This commit is contained in:
yangy
2026-05-28 18:26:20 +08:00
parent b2c11adeba
commit f3e3e82f52
16 changed files with 1078 additions and 9 deletions
+6 -8
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/luxsin/app-api/pkg/encode"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
@@ -27,7 +28,7 @@ func (h *DeviceHandler) ReportDevInfo(c *gin.Context) {
mac := strings.TrimSpace(c.Query("mac"))
model := strings.TrimSpace(c.Query("model"))
ver := strings.TrimSpace(c.Query("ver"))
xForwardedFor := c.GetHeader("X-Forwarded-For")
clientIP := encode.ClientPublicIP(c)
if mac == "" || model == "" {
c.JSON(http.StatusOK, gin.H{
@@ -40,20 +41,17 @@ func (h *DeviceHandler) ReportDevInfo(c *gin.Context) {
if ver == "" {
ver = ""
}
if xForwardedFor == "" {
xForwardedFor = ""
}
h.log.Info("report dev info",
zap.String("remote_ip", xForwardedFor),
zap.String("time", time.Now().Format("yyyy-MM-dd HH:mm:ss")),
zap.String("remote_ip", clientIP),
zap.String("time", time.Now().Format("2006-01-02 15:04:05")),
)
deviceInfo := map[string]string{
"mac_addr": mac,
"model": model,
"active_date": time.Now().Format("yyyy-MM-dd"),
"ip_addr": xForwardedFor,
"active_date": time.Now().Format("2006-01-02"),
"ip_addr": clientIP,
"ver": ver,
}
+132
View File
@@ -0,0 +1,132 @@
package handler
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/luxsin/app-api/internal/repository"
"github.com/luxsin/app-api/internal/response"
"go.uber.org/zap"
)
type OTAHandler struct {
repo *repository.OTARepository
log *zap.Logger
}
func NewOTAHandler(repo *repository.OTARepository, log *zap.Logger) *OTAHandler {
return &OTAHandler{repo: repo, log: log}
}
// GetOTA 获取 OTA 升级信息
// GET /audio/ota?model=xxx&hw=1&mac=xx:xx:xx&beta=0
func (h *OTAHandler) GetOTA(c *gin.Context) {
modelVal := strings.TrimSpace(c.Query("model"))
hwStr := strings.TrimSpace(c.Query("hw"))
mac := strings.TrimSpace(c.Query("mac"))
betaStr := strings.TrimSpace(c.Query("beta"))
// 参数校验
if modelVal == "" || hwStr == "" {
response.BadRequest(c, "model和hw参数不能为空")
return
}
hw, err := strconv.Atoi(hwStr)
if err != nil {
response.BadRequest(c, "hw参数格式错误")
return
}
beta := 0
if betaStr != "" {
if b, err := strconv.Atoi(betaStr); err == nil {
beta = b
}
}
if mac == "" {
mac = "null"
}
ctx := c.Request.Context()
// 1. 查询最新 OTA 记录
ota, err := h.repo.GetLatestOTA(ctx, modelVal, hw, beta)
if err != nil {
h.log.Error("query ota failed", zap.Error(err))
response.InternalError(c, "查询OTA信息失败")
return
}
if ota == nil {
response.Fail(c, http.StatusOK, 40004, "无OTA升级信息")
return
}
// 2. 检查黑名单
inBlackList, err := h.repo.IsInBlackList(ctx, ota.ID, mac)
if err != nil {
h.log.Error("check black list failed", zap.Error(err))
response.InternalError(c, "查询黑名单失败")
return
}
if inBlackList {
h.log.Info("mac命中OTA黑名单", zap.String("mac", mac), zap.Int("ota_id", ota.ID))
// 命中黑名单,返回一条不在黑名单中的最新记录
ota, err = h.repo.GetLatestOTANotInBlackList(ctx, modelVal, hw, beta, mac)
if err != nil {
h.log.Error("query ota not in blacklist failed", zap.Error(err))
response.InternalError(c, "查询OTA信息失败")
return
}
if ota == nil {
response.Fail(c, http.StatusOK, 40004, "无OTA升级信息")
return
}
response.OK(c, ota)
return
}
// 3. 检查是否为定向升级
if ota.Target == 0 {
// 非定向,直接返回
response.OK(c, ota)
return
}
// 定向升级,检查 mac 是否在定向设备列表中
targetDevice, err := h.repo.FindTargetDevice(ctx, ota.ID, mac)
if err != nil {
h.log.Error("check target device failed", zap.Error(err))
response.InternalError(c, "查询定向设备失败")
return
}
if targetDevice != nil {
// 定向命中,返回该 OTA
response.OK(c, ota)
return
}
// 定向未命中,返回非定向的最新版本
notTargetOTA, err := h.repo.GetLatestOTANotTarget(ctx, modelVal, hw, beta)
if err != nil {
h.log.Error("query ota not target failed", zap.Error(err))
response.InternalError(c, "查询OTA信息失败")
return
}
if notTargetOTA == nil {
response.Fail(c, http.StatusOK, 40004, "无OTA升级信息")
return
}
response.OK(c, notTargetOTA)
}
+42
View File
@@ -0,0 +1,42 @@
package model
import "time"
// OTA 固件升级记录
type OTA struct {
ID int `json:"id"`
VerCode int `json:"verCode"`
VerName string `json:"verName"`
URL string `json:"url"`
MD5 string `json:"md5"`
Force int `json:"force"`
Desc *string `json:"desc,omitempty"`
Model *string `json:"model,omitempty"`
HW int `json:"hw"`
Target int `json:"target"`
Beta int `json:"beta"`
PawVerCode int `json:"pawVerCode"`
PawVerName string `json:"pawVerName"`
PawURL string `json:"pawUrl"`
PawMD5 string `json:"pawMd5"`
StartTime *time.Time `json:"startTime,omitempty"`
EndTime *time.Time `json:"endTime,omitempty"`
Status int `json:"status"`
}
// BlackList OTA 黑名单
type BlackList struct {
ID int `json:"id"`
OTAID int `json:"ota_id"`
Mac string `json:"mac"`
CreateAt time.Time `json:"create_at"`
}
// OTATargetDevice OTA 定向设备
type OTATargetDevice struct {
ID int `json:"id"`
OTAID int `json:"ota_id"`
MacAddr string `json:"mac_addr"`
Type int `json:"type"`
CreateAt time.Time `json:"create_at"`
}
+12
View File
@@ -0,0 +1,12 @@
package model
import "time"
type UserActive struct {
ID int
MacAddr string
Model string
ActiveDate string
IpAddr string
CreateAt time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package model
import "time"
type UserDevice struct {
ID int
MacAddr string
Model string
AddTime time.Time
Ver *string
}
+87
View File
@@ -0,0 +1,87 @@
package repository
import (
"context"
"database/sql"
"fmt"
"github.com/luxsin/app-api/internal/model"
)
type DeviceRepository struct {
db *sql.DB
}
func NewDeviceRepository(db *sql.DB) *DeviceRepository {
return &DeviceRepository{db: db}
}
func (r *DeviceRepository) FindDeviceByMac(ctx context.Context, macAddr string) (*model.UserDevice, error) {
const query = `SELECT id, mac_addr, model, add_time, ver FROM user_device WHERE mac_addr = ?`
var d model.UserDevice
var ver sql.NullString
err := r.db.QueryRowContext(ctx, query, macAddr).Scan(&d.ID, &d.MacAddr, &d.Model, &d.AddTime, &ver)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query user_device: %w", err)
}
if ver.Valid {
d.Ver = &ver.String
}
return &d, nil
}
func (r *DeviceRepository) InsertDevice(ctx context.Context, d model.UserDevice) error {
const query = `INSERT INTO user_device (mac_addr, model, ver) VALUES (?, ?, ?)`
_, err := r.db.ExecContext(ctx, query, d.MacAddr, d.Model, d.Ver)
if err != nil {
return fmt.Errorf("insert user_device: %w", err)
}
return nil
}
func (r *DeviceRepository) UpdateDeviceVer(ctx context.Context, id int, ver *string) error {
const query = `UPDATE user_device SET ver = ? WHERE id = ?`
_, err := r.db.ExecContext(ctx, query, ver, id)
if err != nil {
return fmt.Errorf("update user_device: %w", err)
}
return nil
}
func (r *DeviceRepository) FindActiveByMacAndDate(ctx context.Context, macAddr, activeDate string) (*model.UserActive, error) {
const query = `SELECT id, mac_addr, model, active_date, ip_addr, create_at FROM user_active WHERE mac_addr = ? AND active_date = ?`
var a model.UserActive
err := r.db.QueryRowContext(ctx, query, macAddr, activeDate).Scan(&a.ID, &a.MacAddr, &a.Model, &a.ActiveDate, &a.IpAddr, &a.CreateAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query user_active: %w", err)
}
return &a, nil
}
func (r *DeviceRepository) InsertActive(ctx context.Context, a model.UserActive) error {
const query = `INSERT INTO user_active (mac_addr, model, active_date, ip_addr) VALUES (?, ?, ?, ?)`
_, err := r.db.ExecContext(ctx, query, a.MacAddr, a.Model, a.ActiveDate, a.IpAddr)
if err != nil {
return fmt.Errorf("insert user_active: %w", err)
}
return nil
}
func (r *DeviceRepository) UpdateActiveIp(ctx context.Context, id int, ipAddr string) error {
const query = `UPDATE user_active SET ip_addr = ? WHERE id = ?`
_, err := r.db.ExecContext(ctx, query, ipAddr, id)
if err != nil {
return fmt.Errorf("update user_active: %w", err)
}
return nil
}
+158
View File
@@ -0,0 +1,158 @@
package repository
import (
"context"
"database/sql"
"fmt"
"github.com/luxsin/app-api/internal/model"
)
type OTARepository struct {
db *sql.DB
}
func NewOTARepository(db *sql.DB) *OTARepository {
return &OTARepository{db: db}
}
// GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录(verCode 降序)
func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, force, desc, model, hw, target, beta,
pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status
FROM ota WHERE model = ? AND hw = ? AND beta = ? AND status = 1
ORDER BY verCode DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
var startTime, endTime sql.NullTime
err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta).Scan(
&o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
&o.HW, &o.Target, &o.Beta,
&o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query ota: %w", err)
}
if desc.Valid {
o.Desc = &desc.String
}
if mdl.Valid {
o.Model = &mdl.String
}
if startTime.Valid {
o.StartTime = &startTime.Time
}
if endTime.Valid {
o.EndTime = &endTime.Time
}
return &o, nil
}
// GetLatestOTANotInBlackList 查询最新一条不在黑名单中的 OTA 记录
func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal string, hw, beta int, mac string) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, force, desc, model, hw, target, beta,
pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ?
AND id NOT IN (SELECT ota_id FROM black_list WHERE mac = ?)
ORDER BY verCode DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
var startTime, endTime sql.NullTime
err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta, mac).Scan(
&o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
&o.HW, &o.Target, &o.Beta,
&o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query ota not in blacklist: %w", err)
}
if desc.Valid {
o.Desc = &desc.String
}
if mdl.Valid {
o.Model = &mdl.String
}
if startTime.Valid {
o.StartTime = &startTime.Time
}
if endTime.Valid {
o.EndTime = &endTime.Time
}
return &o, nil
}
// GetLatestOTANotTarget 查询最新一条非定向的 OTA 记录
func (r *OTARepository) GetLatestOTANotTarget(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, force, desc, model, hw, target, beta,
pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ? AND target = 0
ORDER BY verCode DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
var startTime, endTime sql.NullTime
err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta).Scan(
&o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
&o.HW, &o.Target, &o.Beta,
&o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query ota not target: %w", err)
}
if desc.Valid {
o.Desc = &desc.String
}
if mdl.Valid {
o.Model = &mdl.String
}
if startTime.Valid {
o.StartTime = &startTime.Time
}
if endTime.Valid {
o.EndTime = &endTime.Time
}
return &o, nil
}
// IsInBlackList 检查 mac 是否在指定 ota_id 的黑名单中
func (r *OTARepository) IsInBlackList(ctx context.Context, otaID int, mac string) (bool, error) {
const query = `SELECT COUNT(*) FROM black_list WHERE ota_id = ? AND mac = ?`
var count int
if err := r.db.QueryRowContext(ctx, query, otaID, mac).Scan(&count); err != nil {
return false, fmt.Errorf("query black_list: %w", err)
}
return count > 0, nil
}
// FindTargetDevice 检查 mac 是否在指定 ota_id 的定向设备中
func (r *OTARepository) FindTargetDevice(ctx context.Context, otaID int, mac string) (*model.OTATargetDevice, error) {
const query = `SELECT id, ota_id, mac_addr, type, create_at FROM ota_target_device WHERE ota_id = ? AND mac_addr = ?`
var d model.OTATargetDevice
err := r.db.QueryRowContext(ctx, query, otaID, mac).Scan(&d.ID, &d.OTAID, &d.MacAddr, &d.Type, &d.CreateAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query ota_target_device: %w", err)
}
return &d, nil
}
+3
View File
@@ -27,6 +27,7 @@ func New(log *zap.Logger, db *sql.DB, searchClient *search.Client, rdb *redis.Cl
// Repository
brandRepo := repository.NewBrandRepository(db, brandCache)
modelRepo := repository.NewModelRepository(db, modelCache)
otaRepo := repository.NewOTARepository(db)
// Handler
health := handler.NewHealthHandler()
@@ -34,6 +35,7 @@ func New(log *zap.Logger, db *sql.DB, searchClient *search.Client, rdb *redis.Cl
model := handler.NewModelHandler(modelRepo, log)
modelList := handler.NewModelListHandler(searchClient, log)
device := handler.NewDeviceHandler(rdb, log)
ota := handler.NewOTAHandler(otaRepo, log)
v1 := r.Group("/api/v1")
{
@@ -46,6 +48,7 @@ func New(log *zap.Logger, db *sql.DB, searchClient *search.Client, rdb *redis.Cl
audio.GET("/getModel", model.GetModel)
audio.GET("/modelList", modelList.ModelList)
audio.GET("/reportDevInfo", device.ReportDevInfo)
audio.GET("/ota", ota.GetOTA)
}
return r
+166
View File
@@ -0,0 +1,166 @@
package task
import (
"context"
"encoding/json"
"time"
"github.com/luxsin/app-api/internal/model"
"github.com/luxsin/app-api/internal/repository"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
type DevicePersistTask struct {
rdb *redis.Client
repo *repository.DeviceRepository
log *zap.Logger
}
func NewDevicePersistTask(rdb *redis.Client, repo *repository.DeviceRepository, log *zap.Logger) *DevicePersistTask {
return &DevicePersistTask{rdb: rdb, repo: repo, log: log}
}
// Start 启动定时刷入协程,每 interval 执行一次
func (t *DevicePersistTask) Start(interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
t.Persist()
}
}()
t.log.Info("device persist task started", zap.String("interval", interval.String()))
}
// Persist 从 Redis Hash devices 中读取数据刷入数据库
func (t *DevicePersistTask) Persist() {
ctx := context.Background()
// 获取 Redis Hash 所有数据
devices, err := t.rdb.HGetAll(ctx, "devices").Result()
if err != nil {
t.log.Error("redis HGetAll devices failed", zap.Error(err))
return
}
if len(devices) == 0 {
return
}
t.log.Info("persisting devices from redis", zap.Int("count", len(devices)))
var succeeded []string
for mac, jsonStr := range devices {
var info struct {
MacAddr string `json:"mac_addr"`
Model string `json:"model"`
ActiveDate string `json:"active_date"`
IpAddr string `json:"ip_addr"`
Ver string `json:"ver"`
}
if err := json.Unmarshal([]byte(jsonStr), &info); err != nil {
t.log.Error("unmarshal device info failed", zap.String("mac", mac), zap.Error(err))
continue
}
// 修复旧的无效日期格式(之前 active_date 使用了 Java 格式 yyyy-MM-dd)
if info.ActiveDate == "" || info.ActiveDate == "yyyy-MM-dd" {
info.ActiveDate = time.Now().Format("2006-01-02")
}
deviceOk := t.persistDevice(ctx, info)
activeOk := t.persistActive(ctx, info)
if deviceOk && activeOk {
succeeded = append(succeeded, mac)
}
}
// 批量删除成功处理的记录
if len(succeeded) > 0 {
if err := t.rdb.HDel(ctx, "devices", succeeded...).Err(); err != nil {
t.log.Error("redis HDel failed", zap.Int("count", len(succeeded)), zap.Error(err))
} else {
t.log.Info("devices persisted and removed from redis", zap.Int("count", len(succeeded)))
}
}
}
// persistDevice 处理 user_device 表写入
func (t *DevicePersistTask) persistDevice(ctx context.Context, info struct {
MacAddr string `json:"mac_addr"`
Model string `json:"model"`
ActiveDate string `json:"active_date"`
IpAddr string `json:"ip_addr"`
Ver string `json:"ver"`
}) bool {
existing, err := t.repo.FindDeviceByMac(ctx, info.MacAddr)
if err != nil {
t.log.Error("find device by mac failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
if existing == nil {
d := model.UserDevice{
MacAddr: info.MacAddr,
Model: info.Model,
}
if info.Ver != "" {
d.Ver = &info.Ver
}
if err := t.repo.InsertDevice(ctx, d); err != nil {
t.log.Error("insert user_device failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
} else {
var ver *string
if info.Ver != "" {
ver = &info.Ver
}
if err := t.repo.UpdateDeviceVer(ctx, existing.ID, ver); err != nil {
t.log.Error("update user_device failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
}
return true
}
// persistActive 处理 user_active 表写入
func (t *DevicePersistTask) persistActive(ctx context.Context, info struct {
MacAddr string `json:"mac_addr"`
Model string `json:"model"`
ActiveDate string `json:"active_date"`
IpAddr string `json:"ip_addr"`
Ver string `json:"ver"`
}) bool {
dbActive, err := t.repo.FindActiveByMacAndDate(ctx, info.MacAddr, info.ActiveDate)
if err != nil {
t.log.Error("find active by mac and date failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
if dbActive == nil {
a := model.UserActive{
MacAddr: info.MacAddr,
Model: info.Model,
ActiveDate: info.ActiveDate,
IpAddr: info.IpAddr,
}
if err := t.repo.InsertActive(ctx, a); err != nil {
t.log.Error("insert user_active failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
} else {
if err := t.repo.UpdateActiveIp(ctx, dbActive.ID, info.IpAddr); err != nil {
t.log.Error("update user_active failed", zap.String("mac", info.MacAddr), zap.Error(err))
return false
}
}
return true
}