handlefile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "os"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "lc/common/protocol"
  8. "lc/common/util"
  9. )
  10. func HandleFile(topic string, obj *protocol.Pack_SeqFileObject) error {
  11. switch topic {
  12. case protocol.TP_GW_SET_APP:
  13. var app protocol.AppConfig
  14. if err := json.UnmarshalFromString(obj.Data.Content, &app); err != nil {
  15. return err
  16. }
  17. return SaveFile(obj.Data.File, obj.Data.Content, 0)
  18. case protocol.TP_GW_SET_SERIAL:
  19. var sc protocol.SerialConfig
  20. if err := json.UnmarshalFromString(obj.Data.Content, &sc); err != nil {
  21. return err
  22. }
  23. return SaveFile(obj.Data.File, obj.Data.Content, 0)
  24. case protocol.TP_GW_SET_RTU:
  25. var rtu protocol.MapDevConfig
  26. if err := json.UnmarshalFromString(obj.Data.Content, &rtu); err != nil {
  27. return err
  28. }
  29. return SaveFile(obj.Data.File, obj.Data.Content, 1)
  30. case protocol.TP_GW_SET_MODEL:
  31. var iot protocol.IotModel
  32. if err := json.UnmarshalFromString(obj.Data.Content, &iot); err != nil {
  33. return err
  34. }
  35. return SaveFile(obj.Data.File, obj.Data.Content, 2)
  36. }
  37. return nil
  38. }
  39. func SaveFile(fname, content string, flag uint8) error {
  40. var SavePath string
  41. switch flag {
  42. case 0:
  43. SavePath = util.GetPath(0)
  44. case 1:
  45. SavePath = util.GetPath(1)
  46. case 2:
  47. SavePath = util.GetPath(2)
  48. }
  49. //存成临时文件
  50. if err := os.WriteFile(util.GetPath(4)+fname, []byte(content), os.ModePerm); err != nil {
  51. return err
  52. }
  53. //备份原文件
  54. if err := os.Rename(SavePath+fname, SavePath+fname+".bak"); err != nil {
  55. return err
  56. }
  57. //拷贝新文件
  58. if err := os.Rename(util.GetPath(4)+fname, SavePath+fname); err != nil {
  59. //拷贝失败,则还原文件
  60. if err := os.Rename(SavePath+fname+".bak", SavePath+fname); err != nil {
  61. return err
  62. }
  63. return err
  64. }
  65. return nil
  66. }
  67. func ReadFileContent(topic string, o *protocol.Pack_SeqFileObject) {
  68. switch topic {
  69. case protocol.TP_GW_APP:
  70. if buf, err := os.ReadFile(util.GetPath(0) + "app.json"); err == nil {
  71. o.Data.File = "app.json"
  72. o.Data.Content = string(buf)
  73. }
  74. case protocol.TP_GW_SERIAL:
  75. if buf, err := os.ReadFile(util.GetPath(0) + "serial.json"); err == nil {
  76. o.Data.File = "serial.json"
  77. o.Data.Content = string(buf)
  78. }
  79. }
  80. }
  81. func ReadMutilFileContent(topic string, id uint, o *protocol.Pack_MutilFileObject) {
  82. var FileDir string
  83. if topic == protocol.TP_GW_RTU {
  84. FileDir = util.GetPath(1)
  85. } else if topic == protocol.TP_GW_MODEL {
  86. FileDir = util.GetPath(2)
  87. } else if topic == protocol.TP_GW_LOG {
  88. FileDir = util.GetPath(3)
  89. } else if topic == protocol.TP_GW_APP || topic == protocol.TP_GW_SERIAL {
  90. FileDir = util.GetPath(0)
  91. }
  92. //RTU或模型文件
  93. if topic == protocol.TP_GW_RTU || topic == protocol.TP_GW_MODEL {
  94. if id > 0 {
  95. fname := strconv.Itoa(int(id)) + ".json"
  96. AddFileObject(FileDir, fname, o)
  97. } else {
  98. rd, _ := os.ReadDir(FileDir)
  99. for _, fi := range rd {
  100. if !fi.IsDir() {
  101. ok, _ := regexp.MatchString(`[1-9].*json`, fi.Name())
  102. if ok {
  103. AddFileObject(FileDir, fi.Name(), o)
  104. }
  105. }
  106. }
  107. }
  108. } else if topic == protocol.TP_GW_LOG { //日志
  109. rd, _ := os.ReadDir(FileDir)
  110. for _, fi := range rd {
  111. if !fi.IsDir() {
  112. ok := strings.HasSuffix(fi.Name(), ".log")
  113. if ok {
  114. AddFileObject(FileDir, fi.Name(), o)
  115. }
  116. }
  117. }
  118. } else if topic == protocol.TP_GW_APP {
  119. AddFileObject(FileDir, "app.json", o)
  120. } else if topic == protocol.TP_GW_SERIAL {
  121. AddFileObject(FileDir, "serial.json", o)
  122. }
  123. }
  124. func AddFileObject(fpath, fname string, o *protocol.Pack_MutilFileObject) {
  125. if buf, err := os.ReadFile(fpath + fname); err == nil {
  126. var fo protocol.FileObject
  127. fo.File = fname
  128. fo.Content = string(buf)
  129. o.Data.Files = append(o.Data.Files, fo)
  130. }
  131. }