2026-06-05 20:15:30 +08:00
|
|
|
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 频响数据
|
2026-06-12 15:50:01 +08:00
|
|
|
//
|
|
|
|
|
// @Summary 获取耳机原始频响 CSV 数据
|
|
|
|
|
// @Description 从 S3 读取指定机型的测量 CSV 数据,返回 frequency 和 raw 数组
|
|
|
|
|
// @Tags Curve
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param brand query string true "品牌名称"
|
|
|
|
|
// @Param model query string true "型号名称"
|
|
|
|
|
// @Param form query string true "耳机类型 (in-ear/over-ear)"
|
|
|
|
|
// @Param base64Resp query string false "是否返回 base64 编码响应"
|
|
|
|
|
// @Success 200 {object} map[string]any "成功返回 frequency 和 raw 数组"
|
|
|
|
|
// @Failure 400 {object} map[string]any "参数校验失败"
|
|
|
|
|
// @Failure 500 {object} object
|
|
|
|
|
// @Router /audio/getModelCSV [get]
|
|
|
|
|
//
|
2026-06-05 20:15:30 +08:00
|
|
|
// 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()
|
|
|
|
|
|
2026-06-08 14:52:23 +08:00
|
|
|
h.log.Info("reading csv from s3", zap.String("key", key))
|
2026-06-05 20:15:30 +08:00
|
|
|
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"],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 14:52:23 +08:00
|
|
|
func targetCSVKey(file string) string {
|
|
|
|
|
file = strings.TrimPrefix(strings.TrimSpace(file), "/")
|
|
|
|
|
return "autoeq/targets/" + file
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-05 20:15:30 +08:00
|
|
|
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)
|
|
|
|
|
}
|