优化
This commit is contained in:
@@ -30,10 +30,53 @@ type CurveHandler struct {
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
const defaultModelCurveTarget = "Harman over-ear 2018"
|
||||
|
||||
func NewCurveHandler(repo *repository.CurveRepository, curveCache *cache.CurveCache, cfg config.EqualizeConfig, log *zap.Logger) *CurveHandler {
|
||||
return &CurveHandler{repo: repo, cache: curveCache, cfg: cfg, log: log}
|
||||
}
|
||||
|
||||
// ModelCurve 获取机型默认曲线(固定 target: Harman over-ear 2018)
|
||||
// GET /audio/modelCurve?brand=xxx&name=xxx&base64Resp=true
|
||||
func (h *CurveHandler) ModelCurve(c *gin.Context) {
|
||||
brand := strings.TrimSpace(queryParam(c, "brand"))
|
||||
name := strings.TrimSpace(queryParam(c, "name"))
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
if brand == "" || name == "" {
|
||||
h.writeCurveResponse(c, base64Resp, gin.H{
|
||||
"code": 400,
|
||||
"msg": "参数校验失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
|
||||
result, err := h.getCurvePoint(ctx, brand, name, defaultModelCurveTarget)
|
||||
if err != nil {
|
||||
h.log.Error("get model curve failed", zap.Error(err))
|
||||
response.InternalError(c, "获取曲线数据失败")
|
||||
return
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
h.writeCurveResponse(c, base64Resp, gin.H{
|
||||
"code": 0,
|
||||
"msg": "无曲线数据",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal([]byte(result), &resp); err != nil {
|
||||
response.InternalError(c, "解析曲线数据失败")
|
||||
return
|
||||
}
|
||||
|
||||
h.writeCurveResponse(c, base64Resp, resp)
|
||||
}
|
||||
|
||||
// GetCurve 获取目标曲线
|
||||
// GET /audio/getCurve?brand=xxx&name=xxx&target=xxx&base64Resp=true
|
||||
func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
@@ -57,7 +100,7 @@ func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
response.Fail(c, http.StatusOK, 40004, "无曲线数据")
|
||||
response.Fail(c, http.StatusOK, 0, "无曲线数据")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,6 +133,20 @@ func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resultData)
|
||||
}
|
||||
|
||||
func (h *CurveHandler) writeCurveResponse(c *gin.Context, base64Resp bool, data any) {
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(data)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
response.InternalError(c, "编码响应失败")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
// getCurvePoint 获取曲线数据:先查缓存,缓存不存在则请求 EQ 接口并缓存结果
|
||||
func (h *CurveHandler) getCurvePoint(ctx context.Context, brand, name, target string) (string, error) {
|
||||
data, acquired, err := h.cache.GetWithLock(ctx, brand, name, target)
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/luxsin/app-api/internal/storage"
|
||||
"github.com/luxsin/app-api/pkg/encode"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ModelCSVHandler struct {
|
||||
s3 *storage.S3Storage
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewModelCSVHandler(s3 *storage.S3Storage, log *zap.Logger) *ModelCSVHandler {
|
||||
return &ModelCSVHandler{s3: s3, log: log}
|
||||
}
|
||||
|
||||
// GetModelCSV 从 S3 读取耳机 CSV 频响数据
|
||||
// GET /audio/getModelCSV?brand=Abyss&model=Dinan DZ&form=over-ear&base64Resp=true
|
||||
func (h *ModelCSVHandler) GetModelCSV(c *gin.Context) {
|
||||
brand := strings.TrimSpace(queryParam(c, "brand"))
|
||||
model := strings.TrimSpace(queryParam(c, "model"))
|
||||
form := strings.TrimSpace(queryParam(c, "form"))
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
if brand == "" || model == "" || form == "" {
|
||||
h.writeModelCSVResponse(c, base64Resp, gin.H{
|
||||
"code": 400,
|
||||
"msg": "参数校验失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
key := modelCSVKey(brand, model, form)
|
||||
ctx := c.Request.Context()
|
||||
|
||||
data, err := h.s3.GetObject(ctx, key)
|
||||
if err != nil {
|
||||
var noSuchKey *types.NoSuchKey
|
||||
if errors.As(err, &noSuchKey) {
|
||||
h.log.Info("model csv not found in s3", zap.String("key", key))
|
||||
h.writeModelCSVResponse(c, base64Resp, gin.H{
|
||||
"code": 0,
|
||||
"msg": "无曲线数据",
|
||||
})
|
||||
return
|
||||
}
|
||||
h.log.Error("get model csv from s3 failed",
|
||||
zap.String("key", key),
|
||||
zap.Error(err),
|
||||
)
|
||||
h.writeModelCSVResponse(c, base64Resp, gin.H{
|
||||
"code": 500,
|
||||
"msg": "系统错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
parsed, err := parseCSVData(data)
|
||||
if err != nil || parsed == nil {
|
||||
h.log.Error("parse model csv failed", zap.String("key", key), zap.Error(err))
|
||||
h.writeModelCSVResponse(c, base64Resp, gin.H{
|
||||
"code": 0,
|
||||
"msg": "无曲线数据",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
h.writeModelCSVResponse(c, base64Resp, gin.H{
|
||||
"code": 200,
|
||||
"msg": "操作成功",
|
||||
"frequency": parsed["frequency"],
|
||||
"raw": parsed["raw"],
|
||||
})
|
||||
}
|
||||
|
||||
func modelCSVKey(brand, model, form string) string {
|
||||
filename := brand + " " + model + ".csv"
|
||||
return fmt.Sprintf("autoeq/measurements/Eafonyoung/data/%s/%s/%s",
|
||||
form, brandPrefix(brand), filename)
|
||||
}
|
||||
|
||||
func brandPrefix(brand string) string {
|
||||
runes := []rune(brand)
|
||||
if len(runes) == 0 {
|
||||
return ""
|
||||
}
|
||||
first := runes[0]
|
||||
if unicode.IsLetter(first) && unicode.IsLower(first) {
|
||||
return strings.ToUpper(string(first))
|
||||
}
|
||||
return string(first)
|
||||
}
|
||||
|
||||
func parseCSVData(data []byte) (map[string]any, error) {
|
||||
reader := csv.NewReader(bytes.NewReader(data))
|
||||
if _, err := reader.Read(); err != nil {
|
||||
return nil, fmt.Errorf("read csv header: %w", err)
|
||||
}
|
||||
|
||||
frequency := make([]float64, 0)
|
||||
raw := make([]float64, 0)
|
||||
|
||||
for {
|
||||
record, err := reader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read csv row: %w", err)
|
||||
}
|
||||
if len(record) < 2 {
|
||||
continue
|
||||
}
|
||||
freq, err1 := strconv.ParseFloat(record[0], 64)
|
||||
val, err2 := strconv.ParseFloat(record[1], 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
continue
|
||||
}
|
||||
frequency = append(frequency, freq)
|
||||
raw = append(raw, val)
|
||||
}
|
||||
|
||||
if len(frequency) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"frequency": frequency,
|
||||
"raw": raw,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *ModelCSVHandler) writeModelCSVResponse(c *gin.Context, base64Resp bool, data any) {
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(data)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 500,
|
||||
"msg": "系统错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
Reference in New Issue
Block a user