command_result.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package gate_command
  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. dbmodel "property-device/model"
  11. pb_v1 "property-device/pb/v1"
  12. "property-device/utils/gate_utils"
  13. "time"
  14. )
  15. func checkGateCommandResultParam(req *pb_v1.GateCommandResultRequest) error {
  16. switch {
  17. case req.Sn == "" || req.Protocol == 0:
  18. return status.Error(10003, "设备id不能为空")
  19. }
  20. return nil
  21. }
  22. func GateCommandResult(ctx context.Context, req *pb_v1.GateCommandResultRequest) (reply *pb_v1.GateCommandResultReply, err error) {
  23. reply = &pb_v1.GateCommandResultReply{}
  24. // 捕获各个task中的异常并返回给调用者
  25. defer func() {
  26. if r := recover(); r != nil {
  27. err = fmt.Errorf("%+v", r)
  28. e := &status.Status{}
  29. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  30. logger.Error("err",
  31. zap.String("system_err", err.Error()),
  32. zap.Stack("stacktrace"))
  33. }
  34. }
  35. }()
  36. err = checkGateCommandResultParam(req)
  37. if err != nil {
  38. return nil, err
  39. }
  40. gateInfo, err := getGateInfo(0, req.Sn, req.Protocol)
  41. if err != nil {
  42. return nil, err
  43. }
  44. switch req.Protocol {
  45. case gate_utils.GateProtocolSaiboHttpV1:
  46. err = CommandResultSaiboHttpV1(req, gateInfo)
  47. case gate_utils.GateProtocolSaiboMqttV1:
  48. err = CommandResultSaiboMqttV1(req, gateInfo)
  49. case gate_utils.GateProtocolYufanHttpV1:
  50. err = CommandResultYufanHttpV1(req, gateInfo)
  51. if gate_utils.HasCommand(gateInfo.Sn, gateInfo.Protocol) {
  52. reply.HasTask = true
  53. }
  54. }
  55. // 删除7天以前的数据
  56. if err != nil {
  57. return nil, err
  58. }
  59. now := time.Now()
  60. t := now.AddDate(0, 0, -7)
  61. p := &dbmodel.TGateCommand{}
  62. where := [][2]interface{}{}
  63. where = dbmodel.WhereAdd(where, "id", req.Id)
  64. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  65. where = dbmodel.WhereAdd(where, "updated_at <", t.Format("2006-01-02:15:04:05"))
  66. p.Delete(database.DB(), where)
  67. return reply, nil
  68. }