12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package face
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/mqtt"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-mqtt/errors"
- "property-mqtt/mqtt_utils"
- pb_v1 "property-mqtt/pb/v1"
- )
- func checkFaceWhiteAddParam(req *pb_v1.FaceWhiteAddRequest) error {
- if req.Sn == "" {
- return status.Error(10003, "设备sn不能为空")
- }
- for _, v := range req.List {
- if v.Name == "" || v.Pic == "" || v.CustomId == "" {
- return status.Error(10003, "姓名,图片,用户不能为空")
- }
- }
- return nil
- }
- func FaceWhiteAdd(ctx context.Context, req *pb_v1.FaceWhiteAddRequest) (reply *pb_v1.FaceWhiteAddReply, err error) {
- reply = &pb_v1.FaceWhiteAddReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkFaceWhiteAddParam(req)
- if err != nil {
- return nil, err
- }
- if !mqtt_utils.Wait(req.Sn) {
- return nil, status.Error(10003, "系统繁忙")
- }
- list := make([]mqtt_utils.PersonInfo, len(req.List))
- for i, v := range req.List {
- list[i].Name = v.Name
- list[i].CustomId = v.CustomId
- list[i].PersonType = 0
- list[i].TempCardType = 0
- list[i].PicURI = v.Pic
- }
- messageId := mqtt_utils.GenerateMsgId(req.Sn)
- mreq := mqtt_utils.EditPersonsNew{
- Operator: "EditPersonsNew",
- MessageId: messageId,
- DataBegin: "BeginFlag",
- DataEnd: "EndFlag",
- PersonNum: len(list),
- Info: list,
- }
- bytes, _ := json.Marshal(&mreq)
- // 先缓存命令,mqtt收到响应后会做响应处理
- //mqtt_utils.CacheCommand(req.Sn, string(bytes), messageId)
- // 向mqtt发送命令
- err = mqtt.Publish(mqtt.MqttCli, mqtt_utils.GetTopic(req.Sn), bytes)
- if err != nil {
- logger.Error("func",
- zap.String("call", "mqtt.Publish"),
- zap.String("error", err.Error()))
- // 失败删除命令
- //mqtt_utils.DelCommand(req.DeviceId, messageId)
- return nil, errors.MqttError
- }
- return reply, nil
- }
|