page9_handler.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "hnyfkj.com.cn/rtu/linux/baseapp"
  14. )
  15. const cbdLocalRPCPort = 7200
  16. func downloadCBDAppLog(w http.ResponseWriter, r *http.Request) {
  17. if r.Method != http.MethodGet {
  18. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  19. return
  20. }
  21. filename := fmt.Sprintf("cbd.log.%s.tar.gz", time.Now().Format("20060102150405"))
  22. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  23. defer os.Remove(tmpFile)
  24. cmd := exec.Command(
  25. "tar",
  26. "--warning=no-file-changed",
  27. "-czf",
  28. tmpFile,
  29. "-C",
  30. "/home/root/yfkj-user-app/log",
  31. ".",
  32. )
  33. out, err := cmd.CombinedOutput()
  34. if err != nil {
  35. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  36. http.Error(w, string(out), http.StatusInternalServerError)
  37. return
  38. }
  39. }
  40. w.Header().Set("Content-Type", "application/gzip")
  41. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  42. http.ServeFile(w, r, tmpFile)
  43. }
  44. func cbdUnlockHandler(w http.ResponseWriter, r *http.Request) {
  45. if r.Method != http.MethodPost {
  46. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  47. return
  48. }
  49. if !checkOperationPassword(w, r) {
  50. return
  51. }
  52. w.WriteHeader(http.StatusOK)
  53. }
  54. func cbdWriteMCUDataHandler(w http.ResponseWriter, r *http.Request) {
  55. if r.Method != http.MethodPost {
  56. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  57. return
  58. }
  59. var data json.RawMessage
  60. if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
  61. http.Error(w, "参数错误: "+err.Error(), http.StatusBadRequest)
  62. return
  63. }
  64. data = json.RawMessage(bytes.TrimSpace(data))
  65. if len(data) == 0 || len(data) > 512 || !json.Valid(data) {
  66. http.Error(w, "参数错误: 指令不能为空或超过512字节", http.StatusBadRequest)
  67. return
  68. }
  69. if err := callRPCResult(r.Context(), cbdLocalRPCPort, "core.writeMCUData", data, nil); err != nil {
  70. http.Error(w, "数据下发失败: "+err.Error(), http.StatusInternalServerError)
  71. return
  72. }
  73. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  74. _ = json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  75. }
  76. func cbdUploadFirmwareHandler(w http.ResponseWriter, r *http.Request) {
  77. if r.Method != http.MethodPost {
  78. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  79. return
  80. }
  81. hwType := strings.ToLower(r.URL.Query().Get("type"))
  82. if hwType != "hmi" && hwType != "mcu" {
  83. http.Error(w, "无效的升级类型, 只支持 hmi 或 mcu", http.StatusBadRequest)
  84. return
  85. }
  86. r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB
  87. mr, err := r.MultipartReader()
  88. if err != nil {
  89. http.Error(w, "解析上传请求失败: "+err.Error(), http.StatusBadRequest)
  90. return
  91. }
  92. foundFile := false
  93. for {
  94. part, err := mr.NextPart()
  95. if err == io.EOF {
  96. break
  97. }
  98. if err != nil {
  99. http.Error(w, "读取上传数据失败: "+err.Error(), http.StatusBadRequest)
  100. return
  101. }
  102. if part.FormName() != "file" {
  103. part.Close()
  104. continue
  105. }
  106. foundFile = true
  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. if !foundFile {
  147. http.Error(w, "未找到上传文件", http.StatusBadRequest)
  148. return
  149. }
  150. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  151. json.NewEncoder(w).Encode(map[string]string{"result": "success"})
  152. }
  153. func cbdServerCfgHandler(w http.ResponseWriter, r *http.Request) {
  154. switch r.Method {
  155. case http.MethodGet:
  156. if !checkOperationPassword(w, r) {
  157. return
  158. }
  159. callRPCResponse(w, r, cbdLocalRPCPort, "core.getServerCfg", nil)
  160. case http.MethodPost:
  161. if !checkOperationPassword(w, r) {
  162. return
  163. }
  164. var params json.RawMessage
  165. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  166. http.Error(w, "服务器配置无效: "+err.Error(), http.StatusBadRequest)
  167. return
  168. }
  169. callRPCResponse(w, r, cbdLocalRPCPort, "core.setServerCfg", params)
  170. default:
  171. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  172. }
  173. }
  174. func cbdRunStatusHandler(w http.ResponseWriter, r *http.Request) {
  175. if r.Method != http.MethodGet {
  176. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  177. return
  178. }
  179. callRPCResponse(w, r, cbdLocalRPCPort, "core.getRunStatus", nil)
  180. }