command_use.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package gate_command
  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. "time"
  17. )
  18. func checkGateCommandUseParam(req *pb_v1.GateCommandUseRequest) error {
  19. switch {
  20. case req.Sn == "" || req.Protocol == 0:
  21. return status.Error(10003, "sn 和协议不能为空")
  22. }
  23. return nil
  24. }
  25. func GateWhiteLock(sn string, protocol int32) error {
  26. if cache.RedisLock("gate_white_lock_" + fmt.Sprintf("%v----%v", sn, protocol)) {
  27. return nil
  28. }
  29. return status.Error(10003, "系统繁忙,请稍后")
  30. }
  31. func GateWhiteUnlock(sn string, protocol int32) {
  32. cache.RedisUnlock("gate_white_lock_" + fmt.Sprintf("%v----%v", sn, protocol))
  33. }
  34. func GateCommandUse(ctx context.Context, req *pb_v1.GateCommandUseRequest) (reply *pb_v1.GateCommandUseReply, err error) {
  35. reply = &pb_v1.GateCommandUseReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. err = checkGateCommandUseParam(req)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if !gate_utils.HasCommand(req.Sn, req.Protocol) {
  53. return reply, nil
  54. }
  55. if err = GateWhiteLock(req.Sn, req.Protocol); err != nil {
  56. return nil, err
  57. }
  58. defer GateWhiteUnlock(req.Sn, req.Protocol)
  59. p := &dbmodel.TGateCommand{}
  60. gateInfo, err := getGateInfo(0, req.Sn, req.Protocol)
  61. if err != nil {
  62. return nil, err
  63. }
  64. where := [][2]interface{}{}
  65. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  66. where = dbmodel.WhereAdd(where, "status", gate_utils.CommandStatusWait)
  67. err = p.Find(database.DB(), where)
  68. if err != nil && err != gorm.ErrRecordNotFound {
  69. return nil, errors.DataBaseError
  70. }
  71. if p.ID == 0 {
  72. _ = gate_utils.CommandCacheDecrease(req.Sn, req.Protocol)
  73. return reply, nil
  74. }
  75. values := map[string]interface{}{
  76. "status": gate_utils.CommandStatusRuning,
  77. "updated_at": time.Now(),
  78. }
  79. db := database.DB().Begin()
  80. filter := [][2]interface{}{}
  81. filter = dbmodel.WhereAdd(filter, "id", p.ID)
  82. err = p.Update(db, filter, values)
  83. if err != nil {
  84. db.Rollback()
  85. return nil, errors.DataBaseError
  86. }
  87. count, err := p.Count(db, where, nil)
  88. if err != nil {
  89. db.Rollback()
  90. return nil, errors.DataBaseError
  91. }
  92. if count == 0 {
  93. if err = gate_utils.CommandCacheDecrease(req.Sn, req.Protocol); err != nil {
  94. db.Rollback()
  95. return nil, err
  96. }
  97. }
  98. reply.Id = p.ID
  99. reply.CmdCode = p.Code
  100. reply.CmdParams = p.Param
  101. db.Commit()
  102. return reply, nil
  103. }