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 checkFaceOpenParam(req *pb_v1.FaceOpenRequest) error { if req.Sn == "" || req.CmdId == "" { return status.Error(10003, "设备sn不能为空") } return nil } func FaceOpen(ctx context.Context, req *pb_v1.FaceOpenRequest) (reply *pb_v1.FaceOpenReply, err error) { reply = &pb_v1.FaceOpenReply{} // 捕获各个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 = checkFaceOpenParam(req) if err != nil { return nil, err } messageId := req.CmdId mreq := mqtt_utils.UnlockCmd{ Operator: "Unlock", MessageId: messageId, Info: mqtt_utils.UnlockInfo{OpenDoor: 1}, } 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.Sn, messageId) return nil, errors.MqttError } return reply, nil }