package deviceStatus

import (
	"github.com/gogf/gf/database/gredis"
)

const (
	StatusKeyPrefix = "status:device:"
	InfoKeyPrefix   = "info:device:"
	dataExpires     = 7200
)

type SplitStatus struct {
	Power      int `json:"power"`
	Mode       int `json:"mode"`
	FanSpeed   int `json:"fan_speed"`
	SetTemp    int `json:"set_temp"`
	EnvTemp    int `json:"env_temp"`
	EnvCo2     int `json:"env_co2"`
	EnvPm25    int `json:"env_pm25"`
	StatusCode int `json:"status_code"`
	AirMode    int `json:"air_mode"`
	AcType     int `json:"ac_type"`
	AirType    int `json:"air_type"`
	HumType    int `json:"hum_type"`
}

type DevStatusManager struct {
	redisClient *gredis.Redis
}

func NewDevStatusManager(host string, port int) *DevStatusManager {
	red := gredis.New(&gredis.Config{
		Host:      host,
		Port:      port,
		Db:        14,
		MaxActive: 100,
	})
	mgr := &DevStatusManager{
		redisClient: red,
	}
	return mgr
}

func (mgr *DevStatusManager) GetDeviceStatus(id string) (string, error) {
	key := StatusKeyPrefix + id
	// get status from redis
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}

func (mgr *DevStatusManager) GetDeviceInfo(id string) (string, error) {
	key := InfoKeyPrefix + id
	// get status from redis
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}
