in.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package gate
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-device/errors"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. "property-device/utils/gate_utils"
  14. "strings"
  15. "time"
  16. )
  17. func checkGateInParam(req *pb_v1.GateInRequest) error {
  18. switch {
  19. case req.AuthKey == "":
  20. return status.Error(10003, "授权key不能为空")
  21. case req.Manufactor == "":
  22. return status.Error(10003, "厂商不能为空")
  23. case gate_utils.GateProtocolNameMap[req.Protocol] == "":
  24. return status.Error(10003, "协议不能为空")
  25. case req.Sn == "":
  26. return status.Error(10003, "设备序列号不能为空")
  27. }
  28. return nil
  29. }
  30. func GateIn(ctx context.Context, req *pb_v1.GateInRequest) (reply *pb_v1.GateInReply, err error) {
  31. reply = &pb_v1.GateInReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. err = checkGateInParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. now := time.Now()
  49. if err != nil {
  50. return nil, err
  51. }
  52. p := &dbmodel.TGate{
  53. DeviceName: req.DeviceName,
  54. Sn: req.Sn,
  55. Manufactor: req.Manufactor,
  56. AuthKey: req.AuthKey,
  57. GardenId: 0,
  58. OutUser: "",
  59. OutTime: 0,
  60. Status: 2,
  61. Enable: 1,
  62. Location: "",
  63. Protocol: req.Protocol,
  64. Direction: 0,
  65. CreatedAt: now,
  66. UpdatedAt: now,
  67. QcodeSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][0],
  68. PicSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][1],
  69. CardSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][2],
  70. }
  71. err = p.Insert(database.DB())
  72. if err != nil {
  73. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  74. return nil, status.Error(10003, "设备id或设备序列号已存在")
  75. }
  76. return nil, errors.DataBaseError
  77. }
  78. return reply, nil
  79. }