package gate import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/cache" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" "property-device/errors" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" ) const ( GateInfoKeyPrefix = "gate_info_key" ) func checkGateInfoParam(req *pb_v1.GateInfoRequest) error { if req.DeviceId == 0 && (req.Sn == "" || req.Protocol == 0) { return status.Error(10003, "设备id和sn不能同时") } return nil } func gateInfoFromRedis(deviceId int64, sn string, protocol int32) pb_v1.GateItem { key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol) str, _ := cache.Redis().Get(key) if str == "" { return pb_v1.GateItem{} } ret := pb_v1.GateItem{} json.Unmarshal([]byte(str), &ret) return ret } func gateInfoToRedis(deviceId int64, sn string, protocol int32, info pb_v1.GateItem) { key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol) bytes, _ := json.Marshal(info) cache.Redis().SetEx(key, 600, bytes) } func delGateInfoFromRedis(deviceId int64, sn string, protocol int32) { key := GateInfoKeyPrefix + fmt.Sprintf("%v-%v-%v", deviceId, sn, protocol) cache.Redis().Del(key) } func GateInfo(ctx context.Context, req *pb_v1.GateInfoRequest) (reply *pb_v1.GateInfoReply, err error) { reply = &pb_v1.GateInfoReply{} // 捕获各个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 = checkGateInfoParam(req) if err != nil { return nil, err } info := gateInfoFromRedis(req.DeviceId, req.Sn, req.Protocol) if info.DeviceId != 0 { reply.Info = &info return reply, nil } p := &dbmodel.TGate{} where := [][2]interface{}{} if req.DeviceId > 0 { req.Sn = "" req.Protocol = 0 where = dbmodel.WhereAdd(where, "id", req.DeviceId) } else { req.DeviceId = 0 where = dbmodel.WhereAdd(where, "sn", req.Sn) where = dbmodel.WhereAdd(where, "protocol", req.Protocol) } err = p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } reply.Info = &pb_v1.GateItem{ // 设备id DeviceId: p.ID, // 设备名 DeviceName: p.DeviceName, // 序列号 Sn: p.Sn, // 厂商 Manufactor: p.Manufactor, // 授权key AuthKey: p.AuthKey, // 协议 Protocol: p.Protocol, // 小区id GardenId: p.GardenId, // 小区名 GardenName: "", // 出库人 OutUser: p.OutUser, // 出库时间 OutTime: p.OutTime, // 1 在线 2 离线 Status: p.Status, Enable: false, // 1 进场 2 出场 3 进出场 Direction: p.Direction, Location: p.Location, QcodeSupport: p.QcodeSupport, PicSupport: p.PicSupport, CardSupport: p.CardSupport, Ip: p.Ip, Mac: p.Mac, UserName: p.UserName, Password: p.Password, ProtocolDesc: gate_utils.GateProtocolNameMap[p.Protocol], Port: p.Port, } if p.Enable == 1 { reply.Info.Enable = true } gateInfoToRedis(req.DeviceId, req.Sn, req.Protocol, *reply.Info) return reply, nil }