open_command.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package mqtt_utils
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/cache"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "property-mqtt/pb"
  10. pb_v1 "property-mqtt/pb/v1"
  11. "strconv"
  12. "strings"
  13. )
  14. /*
  15. {
  16. "operator": "Unlock",
  17. "messageId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  18. "info":
  19. {
  20. "uid":"00021",
  21. "openDoor":"1",
  22. "showInfo":"请通行"
  23. }
  24. }
  25. 8.2 开门确认信息回复
  26. {
  27. "messageId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  28. "operator": "Unlock-Ack",
  29. info:{
  30. "uid":"00001",
  31. "facesluiceId":"0001",
  32. "result":"ok"
  33. }
  34. }
  35. */
  36. type UnlockInfo struct {
  37. OpenDoor int `json:"openDoor"`
  38. }
  39. type UnlockCmd struct {
  40. Operator string `json:"operator"`
  41. MessageId string `json:"messageId"`
  42. Info UnlockInfo `json:"info"`
  43. }
  44. type UnlockCmdResInfo struct {
  45. Result string `json:"result"`
  46. FaceId string `json:"facesluiceId"`
  47. Detail string `json:"detail"`
  48. }
  49. type UnlockCmdRes struct {
  50. Operator string `json:"operator"`
  51. MessageId string `json:"messageId"`
  52. Info UnlockCmdResInfo `json:"info"`
  53. }
  54. func OpenCommandHandle(command string, res string) {
  55. fmt.Printf("远程开门响应处理:command:%s\n res:%s\n", command, res)
  56. cmdRes := UnlockCmdRes{}
  57. json.Unmarshal([]byte(res), &cmdRes)
  58. result := 2
  59. if cmdRes.Info.Result == "ok" {
  60. result = 1
  61. }
  62. // 二维码开门或卡号开门结果单独处理
  63. if strings.Contains(cmdRes.MessageId, "qcodeopen") || strings.Contains(cmdRes.MessageId, "cardopen") {
  64. defer func() {
  65. cache.Redis().Del(cmdRes.MessageId)
  66. }()
  67. if result != 1 {
  68. logger.Error("qcode open",
  69. zap.String("error", cmdRes.Info.Detail))
  70. return
  71. }
  72. str, _ := cache.Redis().Get(cmdRes.MessageId)
  73. if str == "" {
  74. return
  75. }
  76. mreq := pb_v1.GateRecordAddRequest{}
  77. json.Unmarshal([]byte(str), &mreq)
  78. _, err := pb.Device.GateRecordAdd(context.Background(), &mreq)
  79. if err != nil {
  80. logger.Error("func",
  81. zap.String("call", "pb.Device.GateRecordAdd"),
  82. zap.String("error", err.Error()))
  83. }
  84. return
  85. }
  86. cmdId, _ := strconv.ParseInt(cmdRes.MessageId, 10, 64)
  87. mreq := pb_v1.GateCommandResultRequest{Sn: cmdRes.Info.FaceId, CmdCode: OpenCommand, Content: res, Protocol: GateProtocolSaiboMqttV1, Id: cmdId}
  88. mreq.ResultStatus = int32(result)
  89. mreq.ResultDesc = cmdRes.Info.Detail
  90. _, err := pb.Device.GateCommandResult(context.Background(), &mreq)
  91. if err != nil {
  92. logger.Error("func",
  93. zap.String("call", "pb.Device.GateCommandResult"),
  94. zap.String("error", err.Error()))
  95. }
  96. }