page9_handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 cbdUnlockHandler(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. if !checkOperationPassword(w, r) {
  49. return
  50. }
  51. w.WriteHeader(http.StatusOK)
  52. }
  53. func cbdWriteMCUDataHandler(w http.ResponseWriter, r *http.Request) {
  54. if r.Method != http.MethodPost {
  55. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  56. return
  57. }
  58. var req struct {
  59. Data string `json:"data"`
  60. }
  61. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  62. http.Error(w, "参数错误: "+err.Error(), http.StatusBadRequest)
  63. return
  64. }
  65. data := strings.TrimSpace(req.Data)
  66. if data == "" || len(data) > 512 {
  67. http.Error(w, "参数错误: data 不能为空或超过512字节", http.StatusBadRequest)
  68. return
  69. }
  70. if err := callRPCResult(r.Context(), cbdLocalRPCPort, "core.writeMCUData",
  71. map[string]string{"data": data}, nil); err != nil {
  72. http.Error(w, "数据下发失败: "+err.Error(), http.StatusInternalServerError)
  73. return
  74. }
  75. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  76. _ = json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  77. }
  78. func cbdUploadFirmwareHandler(w http.ResponseWriter, r *http.Request) {
  79. if r.Method != http.MethodPost {
  80. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  81. return
  82. }
  83. hwType := strings.ToLower(r.URL.Query().Get("type"))
  84. if hwType != "hmi" && hwType != "mcu" {
  85. http.Error(w, "无效的升级类型, 只支持 hmi 或 mcu", http.StatusBadRequest)
  86. return
  87. }
  88. r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB
  89. mr, err := r.MultipartReader()
  90. if err != nil {
  91. http.Error(w, "解析上传请求失败: "+err.Error(), http.StatusBadRequest)
  92. return
  93. }
  94. for {
  95. part, err := mr.NextPart()
  96. if err == io.EOF {
  97. break
  98. }
  99. if err != nil {
  100. http.Error(w, "读取上传数据失败: "+err.Error(), http.StatusBadRequest)
  101. return
  102. }
  103. if part.FormName() != "file" {
  104. part.Close()
  105. continue
  106. }
  107. filename := filepath.Base(part.FileName())
  108. if filename == "" || filename == "." {
  109. part.Close()
  110. http.Error(w, "文件名为空", http.StatusBadRequest)
  111. return
  112. }
  113. suffix := ".rbl"
  114. rpcMethod := "core.doMCUUpgrade"
  115. if hwType == "hmi" {
  116. suffix = ".zip"
  117. rpcMethod = "core.doMCUUpgrade"
  118. }
  119. if !strings.HasSuffix(strings.ToLower(filename), suffix) {
  120. part.Close()
  121. http.Error(w, fmt.Sprintf("固件类型错误: %s 升级只允许上传 %s 类型的文件",
  122. strings.ToUpper(hwType), suffix), http.StatusBadRequest)
  123. return
  124. }
  125. path := filepath.Join("/tmp", filename)
  126. err = saveUploadFile(part, path)
  127. part.Close()
  128. if err != nil {
  129. http.Error(w, "保存上传文件失败: "+err.Error(), http.StatusInternalServerError)
  130. return
  131. }
  132. size := int64(0)
  133. if stat, err := os.Stat(path); err == nil {
  134. size = stat.Size()
  135. }
  136. baseapp.Logger.Infof(
  137. "[上传文件成功] 上传文件名: %s, 上传文件大小: %d 字节, 本地存储路径: %s",
  138. filename, size, path)
  139. if err := callRPCResult(r.Context(), cbdLocalRPCPort, rpcMethod,
  140. map[string]string{"filePath": path}, nil); err != nil {
  141. http.Error(w, "执行固件升级失败: "+err.Error(), http.StatusInternalServerError)
  142. return
  143. }
  144. break
  145. }
  146. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  147. json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  148. }
  149. func cbdServerCfgHandler(w http.ResponseWriter, r *http.Request) {
  150. switch r.Method {
  151. case http.MethodGet:
  152. if !checkOperationPassword(w, r) {
  153. return
  154. }
  155. callRPCResponse(w, r, cbdLocalRPCPort, "core.getServerCfg", nil)
  156. case http.MethodPost:
  157. if !checkOperationPassword(w, r) {
  158. return
  159. }
  160. var params json.RawMessage
  161. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  162. http.Error(w, "服务器配置无效: "+err.Error(), http.StatusBadRequest)
  163. return
  164. }
  165. callRPCResponse(w, r, cbdLocalRPCPort, "core.setServerCfg", params)
  166. default:
  167. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  168. }
  169. }
  170. func cbdRunStatusHandler(w http.ResponseWriter, r *http.Request) {
  171. if r.Method != http.MethodGet {
  172. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  173. return
  174. }
  175. callRPCResponse(w, r, cbdLocalRPCPort, "core.getRunStatus", nil)
  176. }