1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package util
- import (
- "fmt"
- "github.com/go-redis/redis"
- "iot_manager_service/config"
- "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 (
- DeviceStateKey = "dev_state:%s"
- )
- 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
- }
|