page9_handler.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "hnyfkj.com.cn/rtu/linux/baseapp"
  13. )
  14. const cbdLocalRPCPort = 7200
  15. func downloadCBDAppLog(w http.ResponseWriter, r *http.Request) {
  16. if r.Method != http.MethodGet {
  17. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  18. return
  19. }
  20. filename := fmt.Sprintf("cbd.log.%s.tar.gz", time.Now().Format("20060102150405"))
  21. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  22. defer os.Remove(tmpFile)
  23. cmd := exec.Command(
  24. "tar",
  25. "--warning=no-file-changed",
  26. "-czf",
  27. tmpFile,
  28. "-C",
  29. "/home/root/yfkj-user-app/log",
  30. ".",
  31. )
  32. out, err := cmd.CombinedOutput()
  33. if err != nil {
  34. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  35. http.Error(w, string(out), http.StatusInternalServerError)
  36. return
  37. }
  38. }
  39. w.Header().Set("Content-Type", "application/gzip")
  40. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  41. http.ServeFile(w, r, tmpFile)
  42. }
  43. func cbdWriteMCUDataHandler(w http.ResponseWriter, r *http.Request) {
  44. if r.Method != http.MethodPost {
  45. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  46. return
  47. }
  48. var req struct {
  49. Data string `json:"data"`
  50. }
  51. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  52. http.Error(w, "参数错误: "+err.Error(), http.StatusBadRequest)
  53. return
  54. }
  55. data := strings.TrimSpace(req.Data)
  56. if data == "" || len(data) > 512 {
  57. http.Error(w, "参数错误: data 不能为空或超过512字节", http.StatusBadRequest)
  58. return
  59. }
  60. if err := callRPCResult(r.Context(), cbdLocalRPCPort, "core.writeMCUData",
  61. map[string]string{"data": data}, nil); err != nil {
  62. http.Error(w, "数据下发失败: "+err.Error(), http.StatusInternalServerError)
  63. return
  64. }
  65. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  66. _ = json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  67. }
  68. func cbdUploadFirmwareHandler(w http.ResponseWriter, r *http.Request) {
  69. if r.Method != http.MethodPost {
  70. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  71. return
  72. }
  73. hwType := strings.ToLower(r.URL.Query().Get("type"))
  74. if hwType != "hmi" && hwType != "mcu" {
  75. http.Error(w, "无效的升级类型, 只支持 hmi 或 mcu", http.StatusBadRequest)
  76. return
  77. }
  78. r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB
  79. mr, err := r.MultipartReader()
  80. if err != nil {
  81. http.Error(w, "解析上传请求失败: "+err.Error(), http.StatusBadRequest)
  82. return
  83. }
  84. for {
  85. part, err := mr.NextPart()
  86. if err == io.EOF {
  87. break
  88. }
  89. if err != nil {
  90. http.Error(w, "读取上传数据失败: "+err.Error(), http.StatusBadRequest)
  91. return
  92. }
  93. if part.FormName() != "file" {
  94. part.Close()
  95. continue
  96. }
  97. filename := filepath.Base(part.FileName())
  98. if filename == "" || filename == "." {
  99. part.Close()
  100. http.Error(w, "文件名为空", http.StatusBadRequest)
  101. return
  102. }
  103. suffix := ".rbl"
  104. rpcMethod := "core.doMCUUpgrade"
  105. if hwType == "hmi" {
  106. suffix = ".zip"
  107. rpcMethod = "core.doMCUUpgrade"
  108. }
  109. if !strings.HasSuffix(strings.ToLower(filename), suffix) {
  110. part.Close()
  111. http.Error(w, fmt.Sprintf("固件类型错误: %s 升级只允许上传 %s 类型的文件",
  112. strings.ToUpper(hwType), suffix), http.StatusBadRequest)
  113. return
  114. }
  115. path := filepath.Join("/tmp", filename)
  116. err = saveUploadFile(part, path)
  117. part.Close()
  118. if err != nil {
  119. http.Error(w, "保存上传文件失败: "+err.Error(), http.StatusInternalServerError)
  120. return
  121. }
  122. size := int64(0)
  123. if stat, err := os.Stat(path); err == nil {
  124. size = stat.Size()
  125. }
  126. baseapp.Logger.Infof(
  127. "[上传文件成功] 上传文件名: %s, 上传文件大小: %d 字节, 本地存储路径: %s",
  128. filename, size, path)
  129. if err := callRPCResult(r.Context(), cbdLocalRPCPort, rpcMethod,
  130. map[string]string{"filePath": path}, nil); err != nil {
  131. http.Error(w, "执行固件升级失败: "+err.Error(), http.StatusInternalServerError)
  132. return
  133. }
  134. break
  135. }
  136. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  137. json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  138. }