package face import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-mqtt/mqtt_utils" pb_v1 "property-mqtt/pb/v1" ) func checkNewMqttDeviceParam(req *pb_v1.NewMqttDeviceRequest) error { if req.Sn == "" { return status.Error(10003, "设备sn不能为空") } return nil } func NewMqttDevice(ctx context.Context, req *pb_v1.NewMqttDeviceRequest) (reply *pb_v1.NewMqttDeviceReply, err error) { reply = &pb_v1.NewMqttDeviceReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() err = checkNewMqttDeviceParam(req) if err != nil { return nil, err } err = mqtt_utils.SubOne(req.Sn) if err != nil { return nil, status.Error(10003, "mqtt订阅失败") } return reply, nil }