white_del.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package face
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/mqtt"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-mqtt/errors"
  11. "property-mqtt/mqtt_utils"
  12. pb_v1 "property-mqtt/pb/v1"
  13. )
  14. func checkFaceWhiteDelParam(req *pb_v1.FaceWhiteDelRequest) error {
  15. if req.Sn == "" {
  16. return status.Error(10003, "设备sn不能为空")
  17. }
  18. if len(req.CustomIds) == 0 {
  19. return status.Error(10003, "用户不能为空")
  20. }
  21. for _, v := range req.CustomIds {
  22. if v == "" {
  23. return status.Error(10003, "用户不能为空")
  24. }
  25. }
  26. return nil
  27. }
  28. func FaceWhiteDel(ctx context.Context, req *pb_v1.FaceWhiteDelRequest) (reply *pb_v1.FaceWhiteDelReply, err error) {
  29. reply = &pb_v1.FaceWhiteDelReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. err = checkFaceWhiteDelParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if !mqtt_utils.Wait(req.Sn) {
  47. return nil, status.Error(10003, "系统繁忙")
  48. }
  49. messageId := mqtt_utils.GenerateMsgId(req.Sn)
  50. mreq := mqtt_utils.DeletePersonsCmd{
  51. Operator: "DeletePersons",
  52. MessageId: messageId,
  53. DataBegin: "BeginFlag",
  54. DataEnd: "EndFlag",
  55. PersonNum: len(req.CustomIds),
  56. Info: mqtt_utils.DeletePersonsInfo{CustomId: req.CustomIds},
  57. }
  58. bytes, _ := json.Marshal(&mreq)
  59. // 先缓存命令,mqtt收到响应后会做响应处理
  60. //mqtt_utils.CacheCommand(req.Sn, string(bytes), messageId)
  61. // 向mqtt发送命令
  62. err = mqtt.Publish(mqtt.MqttCli, mqtt_utils.GetTopic(req.Sn), bytes)
  63. if err != nil {
  64. logger.Error("func",
  65. zap.String("call", "mqtt.Publish"),
  66. zap.String("error", err.Error()))
  67. // 失败删除命令
  68. //mqtt_utils.DelCommand(req.DeviceId, messageId)
  69. return nil, errors.MqttError
  70. }
  71. return reply, nil
  72. }