Просмотр исходного кода

1,开始编码page9测报灯业务页面;2,上传制作符合RTU_BSP_FRAMEWORK格式的应用安装包脚本和资源文件

niujiuru 6 дней назад
Родитель
Сommit
9b8261480c

+ 8 - 0
mk_app_pkg/app.json

@@ -0,0 +1,8 @@
+{
+  "name"        : "虫情测报灯",
+  "version"     : "0.0.0.1",
+  "executable"  : "cbd_go_app",
+  "libraryPaths": [],
+  "description" : "基于“YFKJ_RTU_BSP_FRAMEWORK”全新架构开发,当前版本为 v0.0.0.1,目前处于测试验证阶段",
+  "readmeFile"  : "readme.txt"
+}

+ 23 - 0
mk_app_pkg/build.sh

@@ -0,0 +1,23 @@
+#!/bin/sh
+
+set -e
+
+PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
+
+make -C "$PROJECT_DIR" clean
+make -C "$PROJECT_DIR"
+
+mkdir -p tmp/a tmp/b tmp/c
+
+cp "$PROJECT_DIR/cbd_go_app" tmp/a/
+ln -sfn a/cbd_go_app tmp/cbd_go_app
+
+cp app.json readme.txt tmp/
+
+cp -rf "$PROJECT_DIR/voices" tmp/
+
+tar -czvf cbd_go_app.tar.gz -C tmp .
+
+rm -rf tmp
+
+make -C "$PROJECT_DIR" clean

+ 3 - 0
mk_app_pkg/readme.txt

@@ -0,0 +1,3 @@
+1,虫情测报灯RTU程序,负责设备网络接入与通信管理,建立设备与云端平台之间的数据交互通道;承接MCU控制板及云端平台下发的业务指令,完成指令处理、虫情图像采集、数据汇集与上传,并与MCU协同完成虫情测报业务。
+
+2,基于“YFKJ_RTU_BSP_FRAMEWORK”全新架构开发,当前版本为 v0.0.0.1,目前处于测试验证阶段。

+ 170 - 0
web-ui.service/page9_handler.go

@@ -1 +1,171 @@
 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"})
+}

+ 4 - 0
web-ui.service/web/app.html

@@ -18,6 +18,7 @@
 <link rel="stylesheet" href="static/css/page6.css?v={{V}}">
 <link rel="stylesheet" href="static/css/page7.css?v={{V}}">
 <link rel="stylesheet" href="static/css/page8.css?v={{V}}">
+<link rel="stylesheet" href="static/css/page9.css?v={{V}}">
 </head>
 
 <body>
@@ -40,6 +41,7 @@
   <symbol id="i-disk" viewBox="0 0 24 24"><line x1="22" y1="12" x2="2" y2="12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/><line x1="6" y1="16" x2="6.01" y2="16"/><line x1="10" y1="16" x2="10.01" y2="16"/></symbol>
   <symbol id="i-trend" viewBox="0 0 24 24"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></symbol>
   <symbol id="i-refresh" viewBox="0 0 24 24"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></symbol>
+  <symbol id="i-cbd" viewBox="0 0 24 24"><rect x="8" y="7" width="8" height="13" rx="4"/><path d="M9 7a3 3 0 0 1 6 0"/><line x1="12" y1="2" x2="12" y2="4"/><line x1="8.5" y1="11" x2="3" y2="11"/><line x1="8.5" y1="15" x2="3" y2="15"/><line x1="8.5" y1="18" x2="4" y2="20"/><line x1="15.5" y1="11" x2="21" y2="11"/><line x1="15.5" y1="15" x2="21" y2="15"/><line x1="15.5" y1="18" x2="20" y2="20"/><line x1="12" y1="11" x2="12" y2="20"/></symbol>
 </svg>
 
 <div id="loading-mask">
@@ -75,6 +77,7 @@
       <div class="menu-item" data-page="page4"><svg class="icon"><use href="#i-pin"/></svg>定位服务</div>
       <div class="menu-item" data-page="page5"><svg class="icon"><use href="#i-camera"/></svg>相机操作</div>
       <div class="menu-item" data-page="page6"><svg class="icon"><use href="#i-box"/></svg>应用管理</div>
+      <div class="menu-item" data-page="page9"><svg class="icon"><use href="#i-cbd"/></svg>虫情测报</div>
       <div class="menu-item" data-page="page7"><svg class="icon"><use href="#i-flask"/></svg>一键自检</div>
       <div class="menu-item" data-page="page8"><svg class="icon"><use href="#i-term"/></svg>命令终端</div>
 
@@ -113,6 +116,7 @@
 <script src="static/js/page6.js?v={{V}}"></script>
 <script src="static/js/page7.js?v={{V}}"></script>
 <script src="static/js/page8.js?v={{V}}"></script>
+<script src="static/js/page9.js?v={{V}}"></script>
 </body>
 
 </html>

+ 62 - 0
web-ui.service/web/pages/page9.html

@@ -0,0 +1,62 @@
+<!doctype html>
+<html lang="zh-CN">
+
+<head>
+<meta charset="utf-8">
+<title>虫情测报</title>
+</head>
+
+<body>
+<div class="app">
+  <main class="view page-cbd">
+    <div class="panes">
+      <!-- 左栏 -->
+      <div class="left-pane">
+        <div class="card command-card">
+          <div class="card-header">
+            <h2>常用指令</h2>
+          </div>
+          <div class="command-content">
+          </div>
+        </div>
+        <div class="card firmware-card">
+          <div class="card-header">
+            <h2>固件升级</h2>
+          </div>
+          <div class="firmware-content">
+          </div>
+        </div>
+      </div>
+      <!-- 右栏 -->
+      <div class="right-pane">
+        <div class="card log-card">
+          <div class="card-header">
+            <h2>应用日志</h2>
+            <div class="log-actions">
+              <select class="log-level-select" id="logLevel" title="当前服务日志级别" onchange="setCBDLogLevel()">
+                <option value="trace">trace</option>
+                <option value="debug">debug</option>
+                <option value="info" selected>info</option>
+                <option value="error">error</option>
+                <option value="fatal">fatal</option>
+              </select>
+              <span style="margin:0 1px;"></span>
+              <button class="btn" title="复制当前屏显日志" onclick="copyLog('text4')">复制</button>
+              <span style="margin:0 1px;"></span>
+              <button class="btn" title="清除当前屏显日志" onclick="clearLog('text4')">清除</button>
+              <span style="margin:0 1px;"></span>
+              <button class="btn" title="下载服务日志文件" onclick="downloadCBDLogFile(event)">下载</button>
+              <span style="margin:0 1px;"></span>
+              <button class="btn" title="点击重启用户应用" onclick="restartCBDService(event)">重启</button>
+            </div>
+          </div>
+          <div class="output" id="text4">自动实时获取数据...</div>
+        </div>
+      </div>
+      <!-- 结束 -->
+    </div>
+  </main>
+</div>
+</body>
+
+</html>

+ 105 - 0
web-ui.service/web/static/css/page9.css

@@ -0,0 +1,105 @@
+
+/* 页面整体布局 */
+.page-cbd {
+    display: flex;
+    flex-direction: column;
+    gap: 12px;
+    overflow: hidden;
+}
+
+/* 左右拆分的栏 */
+.page-cbd .panes {
+    display: flex;
+    gap: 12px;
+    flex: 1;
+    min-height: 0;
+    overflow: hidden;
+}
+
+/* 左栏 */
+.page-cbd .left-pane {
+    flex: 1;
+    display: flex;
+    flex-direction: column;
+    gap: 12px;
+    min-width: 0;
+    min-height: 0;
+    overflow: hidden;
+}
+
+.page-cbd .left-pane .card {
+    display: flex;
+    flex-direction: column;
+    min-height: 0;
+    overflow: hidden;
+}
+
+/* 常用指令:占2/3 */
+.page-cbd .command-card {
+    flex: 2;
+}
+
+/* 固件升级:占1/3 */
+.page-cbd .firmware-card {
+    flex: 1;
+}
+
+/* 左侧内容区域 */
+.page-cbd .command-content,
+.page-cbd .firmware-content {
+    flex: 1;
+    min-height: 0;
+    overflow: auto;
+}
+
+/* 右栏:应用日志 */
+.page-cbd .right-pane {
+    flex: 1;
+    display: flex;
+    flex-direction: column;
+    min-width: 0;
+    min-height: 0;
+}
+
+.page-cbd .right-pane .log-card {
+    flex: 1;
+    display: flex;
+    flex-direction: column;
+    min-height: 0;
+    overflow: hidden;
+}
+
+.page-cbd .right-pane .output {
+    flex: 1;
+    min-height: 0;
+}
+
+/* ================= 移动端适配 (≤768px) ================= */
+@media (max-width: 768px) {
+    .page-cbd .panes {
+        flex-direction: column;
+        flex: none;
+        overflow: visible;
+    }
+
+    .page-cbd .left-pane,
+    .page-cbd .right-pane {
+        width: 100%;
+        flex: none;
+        overflow: visible;
+    }
+
+    .page-cbd .command-card {
+        flex: none;
+        min-height: 240px;
+    }
+
+    .page-cbd .firmware-card {
+        flex: none;
+        min-height: 180px;
+    }
+
+    .page-cbd .right-pane .log-card {
+        min-height: 400px;
+    }
+}

+ 110 - 0
web-ui.service/web/static/js/page9.js

@@ -0,0 +1,110 @@
+var cbdLogLevel = "";
+
+function setCBDLogLevel() {
+    var level = logLevel.value;
+    fetch("/api/cbd/loglevel", {
+        method: "POST",
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({ level: level }),
+    })
+        .then(function (r) {
+            if (!r.ok) {
+                return r.text().then(function (t) {
+                    throw new Error(t);
+                });
+            }
+            cbdLogLevel = level;
+        })
+        .catch(function (e) {
+            logLevel.value = cbdLogLevel;
+        });
+}
+
+function getCBDLogLevel() {
+    fetch("/api/cbd/loglevel")
+        .then(function (r) {
+            if (!r.ok) {
+                return r.text().then(function (t) {
+                    throw new Error(t);
+                });
+            }
+            return r.json();
+        })
+        .then(function (d) {
+            cbdLogLevel = d.log_level;
+            logLevel.value = d.log_level;
+        })
+        .catch(function (e) {
+            cbdLogLevel = "";
+            logLevel.value = "";
+        });
+}
+
+function downloadCBDLogFile(event) {
+    var btn = event.currentTarget;
+    btn.disabled = true;
+    fetch("/api/cbd/logfile")
+        .then(function (r) {
+            if (!r.ok) {
+                throw new Error("HTTP " + r.status);
+            }
+            var disposition = r.headers.get("Content-Disposition");
+            var filename = "cbd.log.tar.gz";
+            if (disposition) {
+                var match = disposition.match(/filename="?([^";]+)"?/);
+                if (match) {
+                    filename = match[1];
+                }
+            }
+            return r.blob().then(function (blob) {
+                return { blob: blob, filename: filename };
+            });
+        })
+        .then(function (res) {
+            downloadBlobFile(res.blob, res.filename);
+            alert("下载成功");
+        })
+        .catch(function (e) {
+            console.error("download failed:", e);
+            alert("下载失败: " + e.message);
+        })
+        .finally(function () {
+            btn.disabled = false;
+            btn.textContent = "下载";
+        });
+}
+
+function restartCBDService(event) {
+    if (!confirm("确定重启用户应用吗?")) {
+        return;
+    }
+    var btn = event.currentTarget;
+    btn.disabled = true;
+    fetch("/api/user-app/restart", {
+        method: "POST",
+    })
+        .then(function (r) {
+            if (!r.ok) {
+                return r.text().then(function (t) {
+                    throw new Error(t);
+                });
+            }
+            alert("重启成功");
+        })
+        .catch(function (e) {
+            console.error("restart failed:", e);
+            alert("重启失败: " + e.message);
+        })
+        .finally(function () {
+            btn.disabled = false;
+        });
+}
+
+function initPage9() {
+  connectLog("yfkj-user-app.service", "text4");
+  getCBDLogLevel()
+}
+
+function exitPage9() {
+  disconnectLog();
+}

+ 9 - 0
web-ui.service/web/static/js/router.js

@@ -7,6 +7,7 @@ var routes = {
     page6: "pages/page6.html",
     page7: "pages/page7.html",
     page8: "pages/page8.html",
+    page9: "pages/page9.html",
 };
 var currentPage = null;
 var loadingPage = false;
@@ -75,6 +76,14 @@ var pageHandlers = {
             if (window.exitPage8) window.exitPage8();
         },
     },
+    page9: {
+        init: function () {
+            if (window.initPage9) window.initPage9();
+        },
+        exit: function () {
+            if (window.exitPage9) window.exitPage9();
+        },
+    },
 };
 
 function clearOldScripts() {

+ 20 - 0
web-ui.service/web_route.go

@@ -279,6 +279,26 @@ var routes = []Route{
 		"/api/ssh/ws",
 		sshWSHandler,
 	},
+
+	{
+		"/api/cbd/loglevel",
+		serviceLogLevelHandler(7200),
+	},
+
+	{
+		"/api/cbd/logfile",
+		downloadCBDAppLog,
+	},
+
+	{
+		"/api/cbd/writeMCUData",
+		cbdWriteMCUDataHandler,
+	},
+
+	{
+		"/api/cbd/uploadFirmware",
+		cbdUploadFirmwareHandler,
+	},
 }
 
 func registerRoutes(mux *http.ServeMux) {