| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- 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"
- )
- //GetDeviceState 获取设备状态 1在线 2离线
- 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
- }
|