ota_upgrade.go 2.2 KB

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