ota_upgrade.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package otaUpgrade
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/database/gredis"
  5. )
  6. const (
  7. FileKeyPrefix = "ota:file:"
  8. ProgressKeyPrefix = "ota:progress:"
  9. dataExpires = 7200
  10. )
  11. type File struct {
  12. FileId int
  13. FileData []byte
  14. }
  15. type Progress struct {
  16. DeviceId string
  17. Progress int
  18. }
  19. type OtaManager struct {
  20. redisClient *gredis.Redis
  21. }
  22. func NewOtaManager(host string, port, db int) *OtaManager {
  23. red := gredis.New(&gredis.Config{
  24. Host: host,
  25. Port: port,
  26. Db: db,
  27. MaxActive: 100,
  28. })
  29. mgr := &OtaManager{
  30. redisClient: red,
  31. }
  32. return mgr
  33. }
  34. func (mgr *OtaManager) SavaFile(id int, fileData []byte) error {
  35. key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
  36. file := new(File)
  37. file.FileId = id
  38. file.FileData = fileData
  39. _, err := mgr.redisClient.Do("SET", key, file)
  40. if err != nil {
  41. return err
  42. }
  43. _, err = mgr.redisClient.Do("EXPIRE", key, dataExpires)
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func (mgr *OtaManager) GetFile(id int) (*File, error) {
  50. key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
  51. file := new(File)
  52. // get status from redis
  53. result, err := mgr.redisClient.DoVar("GET", key)
  54. if err != nil {
  55. return nil, err
  56. }
  57. err = result.Struct(file)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return file, nil
  62. }
  63. func (mgr *OtaManager) DelFile(id int) error {
  64. key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
  65. _, err := mgr.redisClient.Do("DEL", key)
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. func (mgr *OtaManager) GetProgress(id string) (*Progress, error) {
  72. key := ProgressKeyPrefix + id
  73. progress := new(Progress)
  74. result, err := mgr.redisClient.DoVar("GET", key)
  75. if err != nil {
  76. return nil, err
  77. }
  78. _, err = mgr.redisClient.Do("EXPIRE", key, dataExpires)
  79. if err != nil {
  80. return nil, err
  81. }
  82. err = result.Struct(progress)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return progress, nil
  87. }
  88. func (mgr *OtaManager) UpdateProgress(id string, progress int) error {
  89. key := ProgressKeyPrefix + id
  90. _, err := mgr.redisClient.Do("SET", key, progress)
  91. if err != nil {
  92. return err
  93. }
  94. _, err = mgr.redisClient.Do("EXPIRE", key, dataExpires)
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func (mgr *OtaManager) DelProgress(id string) error {
  101. key := ProgressKeyPrefix + id
  102. _, err := mgr.redisClient.Do("DEL", key)
  103. if err != nil {
  104. return err
  105. }
  106. return nil
  107. }