1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package cache
- import (
- "fmt"
- "github.com/go-redis/redis"
- "iot_manager_service/config"
- "iot_manager_service/util/common"
- "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 (
- DeviceStateKey = "dev_stat_"
- ONLINE = "online"
- TLast = "tlast"
- )
- //GetDeviceState 获取设备状态 1在线 2离线
- func GetDeviceState(id string) (retTime time.Time, retState string) {
- defer func() {
- if err1 := recover(); err1 != nil {
- fmt.Println("GetDeviceState err = ", err1)
- }
- }()
- retTime = time.Time{}
- retState = "2"
- //redis中 0在线 1离线
- //todo 需要统一
- list, err := Redis.HMGet(DeviceStateKey+id, TLast, ONLINE).Result()
- if err == nil && list[0] != nil || list[1] != nil {
- t, err1 := common.MlParseTime(list[0].(string))
- s, err0 := strconv.Atoi(list[1].(string))
- if err0 == nil && err1 == nil {
- if s == 0 {
- retState = "1"
- }
- retTime = t
- }
- }
- return
- }
|