mqtt_face.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package mqtt_device
  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. )
  14. func MqttFaceGate(ctx context.Context, req *pb_v1.MqttFaceGateRequest) (reply *pb_v1.MqttFaceGateReply, err error) {
  15. reply = &pb_v1.MqttFaceGateReply{}
  16. // 捕获各个task中的异常并返回给调用者
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err = fmt.Errorf("%+v", r)
  20. e := &status.Status{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. logger.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. p := &dbmodel.TGate{}
  29. where := [][2]interface{}{}
  30. where = dbmodel.WhereAdd(where, "garden_id >", 1)
  31. where = dbmodel.WhereAdd(where, "protocol in", []int{gate_utils.GateProtocolSaiboMqttV1})
  32. where = dbmodel.WhereAdd(where, "enable", 1)
  33. type IdObj struct {
  34. Sn string
  35. }
  36. ids := []IdObj{}
  37. err = p.SelectList(database.DB(), where, "sn", &ids)
  38. if err != nil {
  39. return nil, err
  40. }
  41. reply.Sns = make([]string, len(ids))
  42. for i, v := range ids {
  43. reply.Sns[i] = v.Sn
  44. }
  45. return reply, nil
  46. }