white_add.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 checkFaceWhiteAddParam(req *pb_v1.FaceWhiteAddRequest) error {
  15. if req.Sn == "" {
  16. return status.Error(10003, "设备sn不能为空")
  17. }
  18. for _, v := range req.List {
  19. if v.Name == "" || v.Pic == "" || v.CustomId == "" {
  20. return status.Error(10003, "姓名,图片,用户不能为空")
  21. }
  22. }
  23. return nil
  24. }
  25. func FaceWhiteAdd(ctx context.Context, req *pb_v1.FaceWhiteAddRequest) (reply *pb_v1.FaceWhiteAddReply, err error) {
  26. reply = &pb_v1.FaceWhiteAddReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. err = checkFaceWhiteAddParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if !mqtt_utils.Wait(req.Sn) {
  44. return nil, status.Error(10003, "系统繁忙")
  45. }
  46. list := make([]mqtt_utils.PersonInfo, len(req.List))
  47. for i, v := range req.List {
  48. list[i].Name = v.Name
  49. list[i].CustomId = v.CustomId
  50. list[i].PersonType = 0
  51. list[i].TempCardType = 0
  52. list[i].PicURI = v.Pic
  53. }
  54. messageId := mqtt_utils.GenerateMsgId(req.Sn)
  55. mreq := mqtt_utils.EditPersonsNew{
  56. Operator: "EditPersonsNew",
  57. MessageId: messageId,
  58. DataBegin: "BeginFlag",
  59. DataEnd: "EndFlag",
  60. PersonNum: len(list),
  61. Info: list,
  62. }
  63. bytes, _ := json.Marshal(&mreq)
  64. // 先缓存命令,mqtt收到响应后会做响应处理
  65. //mqtt_utils.CacheCommand(req.Sn, string(bytes), messageId)
  66. // 向mqtt发送命令
  67. err = mqtt.Publish(mqtt.MqttCli, mqtt_utils.GetTopic(req.Sn), bytes)
  68. if err != nil {
  69. logger.Error("func",
  70. zap.String("call", "mqtt.Publish"),
  71. zap.String("error", err.Error()))
  72. // 失败删除命令
  73. //mqtt_utils.DelCommand(req.DeviceId, messageId)
  74. return nil, errors.MqttError
  75. }
  76. return reply, nil
  77. }