package gate import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-device/errors" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" "strings" "time" ) func checkGateInParam(req *pb_v1.GateInRequest) error { switch { case req.AuthKey == "": return status.Error(10003, "授权key不能为空") case req.Manufactor == "": return status.Error(10003, "厂商不能为空") case gate_utils.GateProtocolNameMap[req.Protocol] == "": return status.Error(10003, "协议不能为空") case req.Sn == "": return status.Error(10003, "设备序列号不能为空") } return nil } func GateIn(ctx context.Context, req *pb_v1.GateInRequest) (reply *pb_v1.GateInReply, err error) { reply = &pb_v1.GateInReply{} // 捕获各个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 = checkGateInParam(req) if err != nil { return nil, err } now := time.Now() if err != nil { return nil, err } p := &dbmodel.TGate{ DeviceName: req.DeviceName, Sn: req.Sn, Manufactor: req.Manufactor, AuthKey: req.AuthKey, GardenId: 0, OutUser: "", OutTime: 0, Status: 2, Enable: 1, Location: "", Protocol: req.Protocol, Direction: 0, CreatedAt: now, UpdatedAt: now, QcodeSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][0], PicSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][1], CardSupport: gate_utils.GateProtocolFuntionMap[req.Protocol][2], } err = p.Insert(database.DB()) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "设备id或设备序列号已存在") } return nil, errors.DataBaseError } return reply, nil }