new_device.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package face
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "go.uber.org/zap"
  8. "google.golang.org/grpc/status"
  9. "property-mqtt/mqtt_utils"
  10. pb_v1 "property-mqtt/pb/v1"
  11. )
  12. func checkNewMqttDeviceParam(req *pb_v1.NewMqttDeviceRequest) error {
  13. if req.Sn == "" {
  14. return status.Error(10003, "设备sn不能为空")
  15. }
  16. return nil
  17. }
  18. func NewMqttDevice(ctx context.Context, req *pb_v1.NewMqttDeviceRequest) (reply *pb_v1.NewMqttDeviceReply, err error) {
  19. reply = &pb_v1.NewMqttDeviceReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. err = checkNewMqttDeviceParam(req)
  33. if err != nil {
  34. return nil, err
  35. }
  36. err = mqtt_utils.SubOne(req.Sn)
  37. if err != nil {
  38. return nil, status.Error(10003, "mqtt订阅失败")
  39. }
  40. return reply, nil
  41. }