deviceStatus.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package deviceStatus
  2. import (
  3. "github.com/gogf/gf/database/gredis"
  4. )
  5. const (
  6. splitStatusKeyPrefix = "device:split:status:"
  7. mainStatusKeyPrefix = "device:main:status:"
  8. ffxStatusKeyPrefix = "device:ffx:status:"
  9. newfanStatusKeyPrefix = "device:newfan:status:"
  10. ffxInfoKeyPrefix = "device:ffx:info:"
  11. dataExpires = 7200
  12. )
  13. type SplitStatus struct {
  14. Power int `json:"power"`
  15. Mode int `json:"mode"`
  16. FanSpeed int `json:"fan_speed"`
  17. SetTemp int `json:"set_temp"`
  18. EnvTemp int `json:"env_temp"`
  19. EnvCo2 int `json:"env_co2"`
  20. EnvPm25 int `json:"env_pm25"`
  21. StatusCode int `json:"status_code"`
  22. AirMode int `json:"air_mode"`
  23. AcType int `json:"ac_type"`
  24. AirType int `json:"air_type"`
  25. HumType int `json:"hum_type"`
  26. }
  27. type DevStatusManager struct {
  28. redisClient *gredis.Redis
  29. }
  30. func NewDevStatusManager(host string, port int) *DevStatusManager {
  31. red := gredis.New(&gredis.Config{
  32. Host: host,
  33. Port: port,
  34. Db: 14,
  35. MaxActive: 100,
  36. })
  37. mgr := &DevStatusManager{
  38. redisClient: red,
  39. }
  40. return mgr
  41. }
  42. func (mgr *DevStatusManager) GetDeviceStatus(id, productId string) (string, error) {
  43. var key string
  44. switch productId {
  45. case "1pw4umsiq2hd4w7p7o68k69bb0bnlzw0":
  46. key = splitStatusKeyPrefix + id
  47. case "1pw4umsgxzlda7g4p1zcd6v2m0ukxftv":
  48. key = newfanStatusKeyPrefix + id
  49. case "1pw4ums6ty0d5pyua33f65h100yf45z2":
  50. key = ffxStatusKeyPrefix + id
  51. default:
  52. key = mainStatusKeyPrefix + id
  53. }
  54. // get status from redis
  55. result, err := mgr.redisClient.DoVar("GET", key)
  56. if err != nil {
  57. return "", err
  58. }
  59. return result.String(), nil
  60. }
  61. func (mgr *DevStatusManager) GetDeviceInfo(id, productId string) (string, error) {
  62. var key string
  63. switch productId {
  64. case "1pw4ums6ty0d5pyua33f65h100yf45z2":
  65. key = ffxInfoKeyPrefix + id
  66. }
  67. // get status from redis
  68. result, err := mgr.redisClient.DoVar("GET", key)
  69. if err != nil {
  70. return "", err
  71. }
  72. return result.String(), nil
  73. }