| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package main
- import (
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- "time"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- )
- const cbdLocalRPCPort = 7200
- func downloadCBDAppLog(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- filename := fmt.Sprintf("cbd.log.%s.tar.gz", time.Now().Format("20060102150405"))
- tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
- defer os.Remove(tmpFile)
- cmd := exec.Command(
- "tar",
- "--warning=no-file-changed",
- "-czf",
- tmpFile,
- "-C",
- "/home/root/yfkj-user-app/log",
- ".",
- )
- out, err := cmd.CombinedOutput()
- if err != nil {
- if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
- http.Error(w, string(out), http.StatusInternalServerError)
- return
- }
- }
- w.Header().Set("Content-Type", "application/gzip")
- w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
- http.ServeFile(w, r, tmpFile)
- }
- func cbdWriteMCUDataHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- var req struct {
- Data string `json:"data"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, "参数错误: "+err.Error(), http.StatusBadRequest)
- return
- }
- data := strings.TrimSpace(req.Data)
- if data == "" || len(data) > 512 {
- http.Error(w, "参数错误: data 不能为空或超过512字节", http.StatusBadRequest)
- return
- }
- if err := callRPCResult(r.Context(), cbdLocalRPCPort, "core.writeMCUData",
- map[string]string{"data": data}, nil); err != nil {
- http.Error(w, "数据下发失败: "+err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
- _ = json.NewEncoder(w).Encode(map[string]string{"result": "success"})
- }
- func cbdUploadFirmwareHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- hwType := strings.ToLower(r.URL.Query().Get("type"))
- if hwType != "hmi" && hwType != "mcu" {
- http.Error(w, "无效的升级类型, 只支持 hmi 或 mcu", http.StatusBadRequest)
- return
- }
- r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB
- mr, err := r.MultipartReader()
- if err != nil {
- http.Error(w, "解析上传请求失败: "+err.Error(), http.StatusBadRequest)
- return
- }
- for {
- part, err := mr.NextPart()
- if err == io.EOF {
- break
- }
- if err != nil {
- http.Error(w, "读取上传数据失败: "+err.Error(), http.StatusBadRequest)
- return
- }
- if part.FormName() != "file" {
- part.Close()
- continue
- }
- filename := filepath.Base(part.FileName())
- if filename == "" || filename == "." {
- part.Close()
- http.Error(w, "文件名为空", http.StatusBadRequest)
- return
- }
- suffix := ".rbl"
- rpcMethod := "core.doMCUUpgrade"
- if hwType == "hmi" {
- suffix = ".zip"
- rpcMethod = "core.doMCUUpgrade"
- }
- if !strings.HasSuffix(strings.ToLower(filename), suffix) {
- part.Close()
- http.Error(w, fmt.Sprintf("固件类型错误: %s 升级只允许上传 %s 类型的文件",
- strings.ToUpper(hwType), suffix), http.StatusBadRequest)
- return
- }
- path := filepath.Join("/tmp", filename)
- err = saveUploadFile(part, path)
- part.Close()
- if err != nil {
- http.Error(w, "保存上传文件失败: "+err.Error(), http.StatusInternalServerError)
- return
- }
- size := int64(0)
- if stat, err := os.Stat(path); err == nil {
- size = stat.Size()
- }
- baseapp.Logger.Infof(
- "[上传文件成功] 上传文件名: %s, 上传文件大小: %d 字节, 本地存储路径: %s",
- filename, size, path)
- if err := callRPCResult(r.Context(), cbdLocalRPCPort, rpcMethod,
- map[string]string{"filePath": path}, nil); err != nil {
- http.Error(w, "执行固件升级失败: "+err.Error(), http.StatusInternalServerError)
- return
- }
- break
- }
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
- json.NewEncoder(w).Encode(map[string]string{"result": "success"})
- }
|