handlefile.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. sc := protocol.SerialConfig{Serial: make(map[uint8]*protocol.SerialPort)}
  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. tmpPath := util.GetPath(4)
  50. util.GetTagLog().Infof("sys", "SaveFile:开始,fname=%s,flag=%d,tmpPath=%s,savePath=%s", fname, flag, tmpPath, SavePath)
  51. //存成临时文件
  52. if err := os.MkdirAll(tmpPath, os.ModePerm); err != nil {
  53. util.GetTagLog().Errorf("sys", "SaveFile:MkdirAll失败,path=%s,err=%v", tmpPath, err)
  54. return err
  55. }
  56. if err := os.WriteFile(tmpPath+fname, []byte(content), os.ModePerm); err != nil {
  57. util.GetTagLog().Errorf("sys", "SaveFile:写tmp文件失败,path=%s,err=%v", tmpPath+fname, err)
  58. return err
  59. }
  60. util.GetTagLog().Infof("sys", "SaveFile:tmp文件写入成功,path=%s,len=%d", tmpPath+fname, len(content))
  61. //备份原文件(不存在则跳过)
  62. if _, err := os.Stat(SavePath + fname); err == nil {
  63. if err := os.Rename(SavePath+fname, SavePath+fname+".bak"); err != nil {
  64. util.GetTagLog().Errorf("sys", "SaveFile:备份失败,src=%s,err=%v", SavePath+fname, err)
  65. return err
  66. }
  67. util.GetTagLog().Infof("sys", "SaveFile:备份成功,src=%s", SavePath+fname+".bak")
  68. } else {
  69. util.GetTagLog().Infof("sys", "SaveFile:原文件不存在,跳过备份,path=%s", SavePath+fname)
  70. }
  71. //拷贝新文件
  72. if err := os.Rename(tmpPath+fname, SavePath+fname); err != nil {
  73. util.GetTagLog().Errorf("sys", "SaveFile:替换失败,src=%s,dst=%s,err=%v", tmpPath+fname, SavePath+fname, err)
  74. //拷贝失败,则还原文件
  75. if err := os.Rename(SavePath+fname+".bak", SavePath+fname); err != nil {
  76. util.GetTagLog().Errorf("sys", "SaveFile:还原失败,src=%s,err=%v", SavePath+fname+".bak", err)
  77. }
  78. return err
  79. }
  80. util.GetTagLog().Infof("sys", "SaveFile:替换成功,path=%s", SavePath+fname)
  81. return nil
  82. }
  83. func ReadFileContent(topic string, o *protocol.Pack_SeqFileObject) {
  84. switch topic {
  85. case protocol.TP_GW_APP:
  86. if buf, err := os.ReadFile(util.GetPath(0) + "app.json"); err == nil {
  87. o.Data.File = "app.json"
  88. o.Data.Content = string(buf)
  89. }
  90. case protocol.TP_GW_SERIAL:
  91. if buf, err := os.ReadFile(util.GetPath(0) + "serial.json"); err == nil {
  92. o.Data.File = "serial.json"
  93. o.Data.Content = string(buf)
  94. }
  95. }
  96. }
  97. func ReadMutilFileContent(topic string, id uint, o *protocol.Pack_MutilFileObject) {
  98. var FileDir string
  99. if topic == protocol.TP_GW_RTU {
  100. FileDir = util.GetPath(1)
  101. } else if topic == protocol.TP_GW_MODEL {
  102. FileDir = util.GetPath(2)
  103. } else if topic == protocol.TP_GW_LOG {
  104. FileDir = util.GetPath(3)
  105. } else if topic == protocol.TP_GW_APP || topic == protocol.TP_GW_SERIAL {
  106. FileDir = util.GetPath(0)
  107. }
  108. //RTU或模型文件
  109. if topic == protocol.TP_GW_RTU || topic == protocol.TP_GW_MODEL {
  110. if id > 0 {
  111. fname := strconv.Itoa(int(id)) + ".json"
  112. AddFileObject(FileDir, fname, o)
  113. } else {
  114. rd, _ := os.ReadDir(FileDir)
  115. for _, fi := range rd {
  116. if !fi.IsDir() {
  117. ok, _ := regexp.MatchString(`[1-9].*json`, fi.Name())
  118. if ok {
  119. AddFileObject(FileDir, fi.Name(), o)
  120. }
  121. }
  122. }
  123. }
  124. } else if topic == protocol.TP_GW_LOG { //日志
  125. rd, _ := os.ReadDir(FileDir)
  126. for _, fi := range rd {
  127. if !fi.IsDir() {
  128. ok := strings.HasSuffix(fi.Name(), ".log")
  129. if ok {
  130. AddFileObject(FileDir, fi.Name(), o)
  131. }
  132. }
  133. }
  134. } else if topic == protocol.TP_GW_APP {
  135. AddFileObject(FileDir, "app.json", o)
  136. } else if topic == protocol.TP_GW_SERIAL {
  137. AddFileObject(FileDir, "serial.json", o)
  138. }
  139. }
  140. func AddFileObject(fpath, fname string, o *protocol.Pack_MutilFileObject) {
  141. if buf, err := os.ReadFile(fpath + fname); err == nil {
  142. var fo protocol.FileObject
  143. fo.File = fname
  144. fo.Content = string(buf)
  145. o.Data.Files = append(o.Data.Files, fo)
  146. }
  147. }