瀏覽代碼

完成相机操作页面网口相机的GPIO22上电断电控制和PING测试

niujiuru 3 周之前
父節點
當前提交
882bdb2f97

+ 47 - 0
web-ui.service/page5_handler.go

@@ -103,3 +103,50 @@ func cameraStatusHandler(w http.ResponseWriter, r *http.Request) {
 		"avar5": avar5, "avar6": avar6,
 	})
 }
+
+func cameraPowerHandler(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodPost {
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+		return
+	}
+
+	var req struct {
+		Enable string `json:"enable"`
+	}
+
+	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+
+	var result any
+	err := callRPCResult(r.Context(), 7003, "core.setGigeCameraPower", map[string]string{"enable": req.Enable}, &result)
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	w.Header().Set("Content-Type", "application/json")
+	json.NewEncoder(w).Encode(map[string]bool{
+		"ok": true,
+	})
+}
+
+func cameraPingHandler(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodPost {
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+		return
+	}
+
+	avar6 := noValue
+	check := ""
+
+	callRPCResult(r.Context(), 7003, "core.checkGigeCamera",
+		map[string]string{"ip": "192.168.100.100", "prefix": "24"}, &check)
+	if check == "success" {
+		avar6 = "🟢正常"
+	}
+
+	w.Header().Set("Content-Type", "application/json")
+	json.NewEncoder(w).Encode(map[string]string{"avar6": avar6})
+}

+ 3 - 3
web-ui.service/web/pages/page5.html

@@ -51,11 +51,11 @@
         </div>
         <div class="status-item button-item"">
           <span style="margin:0 1px;"></span>
-          <button class="btn" title="打开网口相机供电" onclick="cameraPower('true')">上电</button>
+          <button class="btn" title="开启GPIO22供电控制" onclick="cameraPower('true')">上电</button>
           <span style="margin:0 1px;"></span>
-          <button class="btn" title="关闭网口相机供电" onclick="cameraPower('false')">断电</button>
+          <button class="btn" title="关闭GPIO22供电控制" onclick="cameraPower('false')">断电</button>
           <span style="margin:0 1px;"></span>
-          <button class="btn" title="检测网口相机是否连通(ping测试)" onclick="cameraPing()">检测</button>
+          <button class="btn" title="检测网口相机是否已连通(Ping测试)" onclick="cameraPing()">检测</button>
         </div>
       </div>
     </div>

+ 43 - 0
web-ui.service/web/static/js/page5.js

@@ -147,6 +147,49 @@ function getCameraStatus() {
   getCameraLogLevel();
 }
 
+function cameraPower(enable) {
+  fetch("/api/camera/gpio22power", {
+    method: "POST",
+    headers: {
+      "Content-Type": "application/json"
+    },
+    body: JSON.stringify({
+      enable: enable
+    })
+  })
+  .then(async r => {
+    if (!r.ok) {
+      throw new Error(await r.text());
+    }
+
+    return r.json();
+  })
+  .then(data => {
+    if (data.ok) {
+      setText("avar5", enable === "true" ? "🟢打开" : null);
+      alert(enable === "true" ? "上电成功" : "断电成功");
+    } else { // 目前后端不会这么返回,避免以后修改代码
+      alert("操作失败");
+    }
+  })
+  .catch(e => {
+    alert("操作失败: " + e.message);
+  });
+}
+
+function cameraPing() {
+  fetch("/api/camera/pingGigeCamera", {method:"POST"})
+  .then(r => r.json())
+  .then(data => {
+    setText("avar6", data.avar6);
+    alert(data.avar6 === "🟢正常" ? "网口相机Ping测试成功" : "网口相机Ping测试失败");
+  })
+  .catch(() => {
+    setText("avar6", null);
+    alert("网口相机Ping测试失败");
+  });
+}
+
 function initPage5() {
   getCameraStatus();
   connectLog("yfkj-camera-capture.service", "text1");

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

@@ -149,6 +149,16 @@ var routes = []Route{
 	},
 
 	{
+		"/api/camera/gpio22power",
+		cameraPowerHandler,
+	},
+
+	{
+		"/api/camera/pingGigeCamera",
+		cameraPingHandler,
+	},
+
+	{
 		"/api/ssh/ws",
 		sshWSHandler,
 	},