handlefile.go 3.6 KB

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