zhangzengfei
2024-05-28 6ae711d0d5517215a36d314a52309424e7bc2665
添加楼层信息到人脸id
2个文件已添加
2个文件已修改
119 ■■■■■ 已修改文件
pkg/floor.go 82 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pkg/snowflake/randString.go 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
repository/captureRepo.go 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
repository/subscribeRepo.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
pkg/floor.go
New file
@@ -0,0 +1,82 @@
package pkg
import (
    "fmt"
    "strconv"
    "strings"
    "gat1400Exchange/pkg/snowflake"
)
// 生成一个包含楼层的人脸id,解析楼层
// 新id的规则, 20位设备编码 +02 +14位时间 + 5555 + 3位楼层(第一位0表示正,1表示负) + 5位随机数
func GenerateFaceIdContainFloor(srcId, floorStr string) string {
    floorNum, _ := parseFloor(floorStr)
    newId := srcId[0:36] + "5555" + floorNum + snowflake.CreateRandomNumber(5)
    return newId
}
func ParseFloorFromId(srcId string) (string, error) {
    if len(srcId) != 48 {
        return "", fmt.Errorf("invalid id %s", srcId)
    }
    if srcId[36:40] != "5555" {
        return "", fmt.Errorf("invalid flag %s", srcId[36:40])
    }
    return restoreFloor(srcId[40:43])
}
// parseFloor parses the floor string and returns a three-character string
func parseFloor(floor string) (string, error) {
    var sign string
    var number string
    // Check if the floor is negative
    if strings.HasPrefix(floor, "-") {
        sign = "1"
        number = floor[1 : len(floor)-1]
    } else {
        sign = "0"
        number = floor[:len(floor)-1]
    }
    // Convert the number to an integer to validate and ensure it is a valid number
    if _, err := strconv.Atoi(number); err != nil {
        return "", err
    }
    // Format the number to be two digits
    formattedNumber := fmt.Sprintf("%02s", number)
    return sign + formattedNumber, nil
}
// restoreFloor restores the three-character string back to the original floor string
func restoreFloor(encoded string) (string, error) {
    if len(encoded) != 3 {
        return "", fmt.Errorf("encoded string must be 3 characters long")
    }
    sign := encoded[0]
    number := encoded[1:]
    // Convert the number back to integer to remove any leading zeros
    parsedNumber, err := strconv.Atoi(number)
    if err != nil {
        return "", err
    }
    var restoredFloor string
    if sign == '1' {
        restoredFloor = fmt.Sprintf("-%dF", parsedNumber)
    } else if sign == '0' {
        restoredFloor = fmt.Sprintf("%dF", parsedNumber)
    } else {
        return "", fmt.Errorf("invalid sign character in encoded string")
    }
    return restoredFloor, nil
}
pkg/snowflake/randString.go
New file
@@ -0,0 +1,26 @@
package snowflake
import (
    "bytes"
    "crypto/rand"
    "fmt"
    "math/big"
    rand2 "math/rand"
)
func CreateRandomNumber(len int) string {
    var numbers = []byte{1, 2, 3, 4, 5, 7, 8, 9}
    var container string
    length := bytes.NewReader(numbers).Len()
    for i := 1; i <= len; i++ {
        random, err := rand.Int(rand.Reader, big.NewInt(int64(length)))
        if err != nil {
            container += fmt.Sprintf("%d", rand2.Int())
        } else {
            container += fmt.Sprintf("%d", numbers[random.Int64()])
        }
    }
    return container
}
repository/captureRepo.go
@@ -3,6 +3,7 @@
import (
    "encoding/base64"
    "encoding/json"
    "gat1400Exchange/pkg"
    "time"
    "gat1400Exchange/client"
@@ -94,6 +95,11 @@
            if face.OtherFeature != "" {
                pd.CameraFloor = face.OtherFeature
            }
            // 尝试从faceId提取楼层
            if pd.CameraFloor == "" {
                pd.CameraFloor, _ = pkg.ParseFloorFromId(face.FaceID)
            }
            //logger.Debug("device %s, CameraFloor:%s", deviceId, pd.CameraFloor)
            payload, err := json.Marshal(pd)
@@ -174,8 +180,9 @@
        devPos.Pos = "1F"
    }
    for idx, _ := range msg.FaceListObject.FaceObject {
    for idx, face := range msg.FaceListObject.FaceObject {
        msg.FaceListObject.FaceObject[idx].OtherFeature = devPos.Pos
        msg.FaceListObject.FaceObject[idx].FaceID = pkg.GenerateFaceIdContainFloor(face.FaceID, devPos.Pos)
    }
    b, _ := json.Marshal(msg)
repository/subscribeRepo.go
@@ -163,7 +163,7 @@
    sub.Status = subscribe.SubscribeStatus
    sub.Ext = *subscribe
    service.UpdateTaskProcs(subscribe.SubscribeID, vo.Msg_Type_Update_Subscribe, nil)
    service.UpdateTaskProcs(subscribe.SubscribeID, vo.Msg_Type_Update_Subscribe, &sub)
    return sub.Save()
}