123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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
- }
|