package util import ( "fmt" "github.com/go-redis/redis" device "iot_manager_service/app/device/dao" system "iot_manager_service/app/system/dao" "iot_manager_service/config" "strconv" "time" ) var Redis *redis.Client func InitRedis() error { cfg := config.Instance() addr := cfg.Redis.Host Redis = redis.NewClient(&redis.Options{ Addr: addr, DialTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, Password: cfg.Redis.Password, }) _, err := Redis.Ping().Result() if err != nil { return err } return nil } const ( UserKey = "%d:sys:user:%d" ModuleTypeKey = "%d:dict:module_type:%d" ControlTypeKey = "%d:dict:control_type:%d" TenantNameKey = "%d:sys:tenant_name:%d" RoleNameKey = "%d:sys:role_name:%d" SexNameKey = "%d:sys:sex_name:%d" DeviceStateKey = "dev_state:%s" ControlType = "control_type" ModuleType = "module_his_type" Sex = "sex" ) func GetUserName(tenantId int, id int64) string { var name string key := fmt.Sprintf(UserKey, tenantId, id) name, err := Redis.Get(key).Result() if err != nil { user := &system.User{ID: id} if err := user.GetUser(); err != nil { fmt.Printf("GetUser err = %s \n", err) } else { name = user.Name Redis.Set(key, name, time.Second*10) } } return name } func GetModuleName(tenantId int, id int) string { var name string key := fmt.Sprintf(ModuleTypeKey, tenantId, id) name, err := Redis.Get(key).Result() if err != nil { dict := &device.Dict{Code: ModuleType, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue Redis.Set(key, name, -1) } } return name } func GetControlType(tenantId int, id int) string { var name string key := fmt.Sprintf(ControlTypeKey, tenantId, id) name, err := Redis.Get(key).Result() if err != nil { dict := &device.Dict{Code: ControlType, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue Redis.Set(key, name, -1) } } return name } func GetSexName(tenantId int, id int) string { var name string key := fmt.Sprintf(SexNameKey, tenantId, id) name, err := Redis.Get(key).Result() if err != nil { dict := &device.Dict{Code: Sex, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue Redis.Set(key, name, -1) } } return name } func GetTenantName(tenantId int, id int) string { var name string key := fmt.Sprintf(TenantNameKey, tenantId, id) name, err := Redis.Get(key).Result() if err != nil { tenant := &system.Tenant{TenantId: id} if err := tenant.GetTenant(); err != nil { fmt.Printf("GetTenant err = %s \n", err) } else { name = tenant.TenantName Redis.Set(key, name, -1) } } return name } func GetRoleName(tenantId int, roleId int64) string { var name string key := fmt.Sprintf(RoleNameKey, tenantId, roleId) name, err := Redis.Get(key).Result() if err != nil { role := &system.Role{ID: roleId} if err := role.GetRole(); err != nil { fmt.Printf("GetRole err = %s \n", err) } else { name = role.RoleName Redis.Set(key, name, -1) } } return name } func GetDeviceState(sn string) int { key := fmt.Sprintf(DeviceStateKey, sn) value, err := Redis.Get(key).Int() state := 2 if err != nil { fmt.Printf("GetDeviceState err = %s \n", err) return state } if value == 1 { state = 1 } return state }