info.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package gate
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/cache"
  7. "git.getensh.com/common/gopkgs/database"
  8. "git.getensh.com/common/gopkgs/logger"
  9. "go.uber.org/zap"
  10. "google.golang.org/grpc/status"
  11. "gorm.io/gorm"
  12. "property-device/errors"
  13. dbmodel "property-device/model"
  14. pb_v1 "property-device/pb/v1"
  15. "property-device/utils/gate_utils"
  16. )
  17. const (
  18. GateInfoKeyPrefix = "gate_info_key"
  19. )
  20. func checkGateInfoParam(req *pb_v1.GateInfoRequest) error {
  21. if req.DeviceId == 0 && (req.Sn == "" || req.Protocol == 0) {
  22. return status.Error(10003, "设备id和sn不能同时")
  23. }
  24. return nil
  25. }
  26. func gateInfoFromRedis(deviceId int64, sn string, protocol int32) pb_v1.GateItem {
  27. key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol)
  28. str, _ := cache.Redis().Get(key)
  29. if str == "" {
  30. return pb_v1.GateItem{}
  31. }
  32. ret := pb_v1.GateItem{}
  33. json.Unmarshal([]byte(str), &ret)
  34. return ret
  35. }
  36. func gateInfoToRedis(deviceId int64, sn string, protocol int32, info pb_v1.GateItem) {
  37. key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol)
  38. bytes, _ := json.Marshal(info)
  39. cache.Redis().SetEx(key, 600, bytes)
  40. }
  41. func delGateInfoFromRedis(deviceId int64, sn string, protocol int32) {
  42. key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol)
  43. cache.Redis().Del(key)
  44. }
  45. func GateInfo(ctx context.Context, req *pb_v1.GateInfoRequest) (reply *pb_v1.GateInfoReply, err error) {
  46. reply = &pb_v1.GateInfoReply{}
  47. // 捕获各个task中的异常并返回给调用者
  48. defer func() {
  49. if r := recover(); r != nil {
  50. err = fmt.Errorf("%+v", r)
  51. e := &status.Status{}
  52. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  53. logger.Error("err",
  54. zap.String("system_err", err.Error()),
  55. zap.Stack("stacktrace"))
  56. }
  57. }
  58. }()
  59. err = checkGateInfoParam(req)
  60. if err != nil {
  61. return nil, err
  62. }
  63. info := gateInfoFromRedis(req.DeviceId, req.Sn, req.Protocol)
  64. if info.DeviceId != 0 {
  65. reply.Info = &info
  66. return reply, nil
  67. }
  68. p := &dbmodel.TGate{}
  69. where := [][2]interface{}{}
  70. if req.DeviceId > 0 {
  71. req.Sn = ""
  72. req.Protocol = 0
  73. where = dbmodel.WhereAdd(where, "id", req.DeviceId)
  74. } else {
  75. req.DeviceId = 0
  76. where = dbmodel.WhereAdd(where, "sn", req.Sn)
  77. where = dbmodel.WhereAdd(where, "protocol", req.Protocol)
  78. }
  79. err = p.Find(database.DB(), where)
  80. if err != nil && err != gorm.ErrRecordNotFound {
  81. return nil, errors.DataBaseError
  82. }
  83. reply.Info = &pb_v1.GateItem{
  84. // 设备id
  85. DeviceId: p.ID,
  86. // 设备名
  87. DeviceName: p.DeviceName,
  88. // 序列号
  89. Sn: p.Sn,
  90. // 厂商
  91. Manufactor: p.Manufactor,
  92. // 授权key
  93. AuthKey: p.AuthKey,
  94. // 协议
  95. Protocol: p.Protocol,
  96. // 小区id
  97. GardenId: p.GardenId,
  98. // 小区名
  99. GardenName: "",
  100. // 出库人
  101. OutUser: p.OutUser,
  102. // 出库时间
  103. OutTime: p.OutTime,
  104. // 1 在线 2 离线
  105. Status: p.Status,
  106. Enable: false,
  107. // 1 进场 2 出场 3 进出场
  108. Direction: p.Direction,
  109. Location: p.Location,
  110. QcodeSupport: p.QcodeSupport,
  111. PicSupport: p.PicSupport,
  112. CardSupport: p.CardSupport,
  113. Ip: p.Ip,
  114. Mac: p.Mac,
  115. UserName: p.UserName,
  116. Password: p.Password,
  117. ProtocolDesc: gate_utils.GateProtocolNameMap[p.Protocol],
  118. Port: p.Port,
  119. }
  120. if p.Enable == 1 {
  121. reply.Info.Enable = true
  122. }
  123. gateInfoToRedis(req.DeviceId, req.Sn, req.Protocol, *reply.Info)
  124. return reply, nil
  125. }