open.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package face
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/mqtt"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-mqtt/errors"
  11. "property-mqtt/mqtt_utils"
  12. pb_v1 "property-mqtt/pb/v1"
  13. )
  14. func checkFaceOpenParam(req *pb_v1.FaceOpenRequest) error {
  15. if req.Sn == "" || req.CmdId == "" {
  16. return status.Error(10003, "设备sn不能为空")
  17. }
  18. return nil
  19. }
  20. func FaceOpen(ctx context.Context, req *pb_v1.FaceOpenRequest) (reply *pb_v1.FaceOpenReply, err error) {
  21. reply = &pb_v1.FaceOpenReply{}
  22. // 捕获各个task中的异常并返回给调用者
  23. defer func() {
  24. if r := recover(); r != nil {
  25. err = fmt.Errorf("%+v", r)
  26. e := &status.Status{}
  27. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  28. logger.Error("err",
  29. zap.String("system_err", err.Error()),
  30. zap.Stack("stacktrace"))
  31. }
  32. }
  33. }()
  34. err = checkFaceOpenParam(req)
  35. if err != nil {
  36. return nil, err
  37. }
  38. messageId := req.CmdId
  39. mreq := mqtt_utils.UnlockCmd{
  40. Operator: "Unlock",
  41. MessageId: messageId,
  42. Info: mqtt_utils.UnlockInfo{OpenDoor: 1},
  43. }
  44. bytes, _ := json.Marshal(&mreq)
  45. // 先缓存命令,mqtt收到响应后会做响应处理
  46. //mqtt_utils.CacheCommand(req.Sn, string(bytes), messageId)
  47. // 向mqtt发送命令
  48. err = mqtt.Publish(mqtt.MqttCli, mqtt_utils.GetTopic(req.Sn), bytes)
  49. if err != nil {
  50. logger.Error("func",
  51. zap.String("call", "mqtt.Publish"),
  52. zap.String("error", err.Error()))
  53. // 失败删除命令
  54. //mqtt_utils.DelCommand(req.Sn, messageId)
  55. return nil, errors.MqttError
  56. }
  57. return reply, nil
  58. }