package gate_utils import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/cache" "git.getensh.com/common/gopkgs/database" "gorm.io/gorm" "property-device/errors" dbmodel "property-device/model" "property-device/pb" pb_v1 "property-device/pb/v1" ) const ( GateProtocolSaiboHttpV1 = 1 GateProtocolSaiboMqttV1 = 2 GateProtocolYufanHttpV1 = 3 ) var GateProtocolNameMap = map[int32]string{ GateProtocolSaiboHttpV1: "赛伯罗斯 http v1", GateProtocolSaiboMqttV1: "赛伯罗斯 mqtt v1", GateProtocolYufanHttpV1: "宇泛 http v1", } // 数组种的元素依次代表是否支持二维码、人脸、卡号 1 支持 2 不支持 var GateProtocolFuntionMap = map[int32][]int32{ GateProtocolSaiboHttpV1: []int32{1, 2, 1}, GateProtocolSaiboMqttV1: []int32{1, 1, 1}, //GateProtocolYufanHttpV1: []int32{1, 1, 1}, } const ( WhiteAddStatusWait = 1 WhiteAddStatusPersonSuc = 2 WhiteAddStatusAllSuc = 3 WhiteAddStatusPersonFail = 4 WhiteAddStatusPicFail = 5 ) const ( CommandStatusWait = 1 CommandStatusRuning = 2 CommandStatusOver = 3 ) /* var commandCodeMap = map[int32]string{ 1: "远程开门", 2: "重启设备", 4: "获取设备参数", 5: "设置设备参数", 6: "下发白名单", 7: "清空设备白名单", 8: "清空设备刷卡记录", 9: "扫码显示参数", 10: "查询是否存在白名单", 99: "恢复出厂", } */ const ( OpenCommand = 1 RebootCommand = 2 DownCommand = 3 DelCommand = 4 QueryCommand = 5 UpdatePicCommand = 6 AddPicCommand = 7 ) var CommandCodeMap = map[int32]string{ 1: "远程开门", 2: "重启设备", 3: "下发白名单", 4: "删除白名单", 5: "查询是否存在", 6: "更新照片", 7: "下发照片", } const CommandCacheKeyPrefix = "gate_command_" func CommandCacheIncrease(sn string, protocol int32) error { key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol) _, err := cache.Redis().Set(key, "1") if err != nil { return errors.RedisError } return nil } func CommandCacheClear(sn string, protocol int32) error { key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol) _, err := cache.Redis().Del(key) if err != nil { return errors.RedisError } return nil } func CommandCacheDecrease(sn string, protocol int32) error { key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol) _, err := cache.Redis().Set(key, "0") if err != nil { return errors.RedisError } return nil } func HasCommand(sn string, protocol int32) bool { key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol) str, _ := cache.Redis().Get(key) if str == "1" { return true } return false } func getGateGardenRedisKey(sn string, protocol int32) string { return fmt.Sprintf("gate_garden_%s_%d", sn, protocol) } //TODO type GateCommonInfo struct { DeviceId int64 `json:"device_id"` GardenId int64 `json:"garden_id"` Location string `json:"location"` Direction int32 `json:"direction"` DeviceName string `json:"device_name"` } func getGateGardenFromRedis(sn string, protocol int32) GateCommonInfo { ret := GateCommonInfo{} key := getGateGardenRedisKey(sn, protocol) str, _ := cache.Redis().Get(key) if str == "" { return ret } json.Unmarshal([]byte(str), &ret) return ret } func setGateGardenToRedis(sn string, protocol int32, info GateCommonInfo) { key := getGateGardenRedisKey(sn, protocol) bytes, _ := json.Marshal(info) cache.Redis().SetEx(key, 600, string(bytes)) } func getGateGardenFromDb(sn string, protocol int32) (GateCommonInfo, error) { p := dbmodel.TGate{} ret := GateCommonInfo{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "sn", sn) where = dbmodel.WhereAdd(where, "protocol", protocol) err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return ret, errors.DataBaseError } if p.ID == 0 { return ret, errors.ErrRecordNotFound } ret = GateCommonInfo{ GardenId: p.GardenId, DeviceId: p.ID, Location: p.Location, Direction: p.Direction, DeviceName: p.DeviceName, } return ret, nil } func GetGateGarden(sn string, protocol int32) (GateCommonInfo, error) { info := getGateGardenFromRedis(sn, protocol) var err error if info.GardenId == 0 { info, err = getGateGardenFromDb(sn, protocol) if err == nil { setGateGardenToRedis(sn, protocol, info) } } return info, err } func GetUserUnitIds(gardenId int64, uids []int64) (map[int64][]int64, map[int64]string, int32, error) { mreq := pb_v1.GardenHouseholdUnitIdsRequest{GardenId: gardenId, Uids: uids} mreply, err := pb.Garden.GardenHouseholdUnitIds(context.Background(), &mreq) if err != nil { return nil, nil, 0, err } ret := map[int64][]int64{} housenames := map[int64]string{} userType := int32(1) for _, v := range mreply.List { ret[v.Uid] = v.UnitIds housenames[v.Uid] = v.HouseNames userType = v.UserType } return ret, housenames, userType, nil }