ソースを参照

迭代优化升级代码

niujiuru 2 日 前
コミット
d3778210c3

+ 1 - 1
gnss.service/main.go

@@ -123,7 +123,7 @@ func main() {
 	baseapp.SetOptDirs(true, false, false, false)
 	baseapp.InitPath()
 
-	svcFlag := flag.String("service", "", "Control the system service.")
+	svcFlag := flag.String("service", "", "Control the yfkj-gnss service.")
 	flag.Parse()
 
 	svcConfig := &service.Config{

+ 1 - 1
networkd.service/main.go

@@ -132,7 +132,7 @@ func main() {
 	baseapp.SetOptDirs(true, true, false, false)
 	baseapp.InitPath()
 
-	svcFlag := flag.String("service", "", "Control the system service.")
+	svcFlag := flag.String("service", "", "Control the yfkj-networkd service.")
 	flag.Parse()
 
 	svcConfig := &service.Config{

+ 1 - 1
timesyncd.service/main.go

@@ -173,7 +173,7 @@ func main() {
 	baseapp.SetOptDirs(true, false, false, false)
 	baseapp.InitPath()
 
-	svcFlag := flag.String("service", "", "Control the system service.")
+	svcFlag := flag.String("service", "", "Control the yfkj-timesyncd service.")
 	flag.Parse()
 
 	svcConfig := &service.Config{

+ 67 - 6
web-ui.service/main.go

@@ -1,15 +1,76 @@
 package main
 
-import "log"
+import (
+	"flag"
+	"fmt"
+	"os"
+
+	"github.com/kardianos/service"
+)
+
+var (
+	Version   = "0.0.0.1"
+	BuildTime = ""
+)
+
+type program struct {
+	webUI *WebUI
+}
+
+// Start should not block. Do the actual work async.
+func (p *program) Start(s service.Service) error {
+	ui, err := NewWebUI(":8080")
+	if err != nil {
+		return err
+	}
+
+	p.webUI = ui
+
+	go func() {
+		if err := ui.Start(); err != nil {
+			fmt.Printf("webui stopped: %v\n", err)
+		}
+	}()
+
+	return nil
+}
+
+// Stop should not block. Return with a few seconds.
+func (p *program) Stop(s service.Service) error {
+	if p.webUI != nil {
+		return p.webUI.Stop()
+	}
+	return nil
+}
 
 func main() {
-	webUI, err := NewWebUI(":8080")
+	svcFlag := flag.String("service", "", "Control the yfkj-webui service.")
+	flag.Parse()
+
+	cfg := &service.Config{
+		Name:        "yfkj-webui",
+		DisplayName: "yfkj-webui",
+		Description: "Yunfei Device Web Management UI",
+		Option: map[string]any{
+			"Restart": "always",
+		},
+	}
+
+	svc, err := service.New(&program{}, cfg)
 	if err != nil {
-		log.Fatal(err)
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	if *svcFlag != "" {
+		if err := service.Control(svc, *svcFlag); err != nil {
+			fmt.Println(err)
+		}
+		return
 	}
 
-	log.Println("Web UI started on 0.0.0.0:8080")
-	if err := webUI.Start(); err != nil {
-		log.Fatal(err)
+	if err := svc.Run(); err != nil {
+		fmt.Println(err)
+		os.Exit(1)
 	}
 }

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

@@ -23,6 +23,9 @@
       </div>
     </div>
     <div class="actions">
+      <span id="sshd-status" class="status">
+         --
+      </span>
       <button class="btn btn-red" onclick="logout()">退出</button>
     </div>
   </header>

+ 40 - 0
web-ui.service/web/static/js/app.js

@@ -56,8 +56,48 @@ async function fetchIMEI() {
   }
 }
 
+let sshdStatusTimer = null;
+function getSshdStatus() {
+  fetch("/api/sshd/status")
+    .then(r => {
+      if (!r.ok) {
+        throw new Error("HTTP " + r.status);
+      }
+
+      return r.json();
+    })
+    .then(data => {
+      document.getElementById("sshd-status").textContent = data.status || "--";
+    })
+    .catch(e => {
+      console.error("sshd status failed:", e);
+      document.getElementById("sshd-status").textContent = "--";
+    });
+}
+
+function startSshdStatusRefresh() {
+  if (sshdStatusTimer) {
+    return;
+  }
+
+  getSshdStatus();
+
+  sshdStatusTimer = setInterval(() => {
+    getSshdStatus();
+  }, 5000);
+}
+
+function stopSshdStatusRefresh() {
+  if (sshdStatusTimer) {
+    clearInterval(sshdStatusTimer);
+    sshdStatusTimer = null;
+  }
+}
+
 function loadIMEI() {
     fetchIMEI();
 }
 
 loadIMEI();
+
+startSshdStatusRefresh();

+ 18 - 0
web-ui.service/web_handler.go

@@ -82,6 +82,24 @@ func getIMEIHandler(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+func sshdStatusHandler(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodGet {
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+		return
+	}
+
+	status := "🔴 YFKJ-SSHD"
+	cmd := exec.Command("systemctl", "is-active", "yfkj-sshd-mqtt-bridge.service")
+	if out, err := cmd.Output(); err == nil {
+		if strings.TrimSpace(string(out)) == "active" {
+			status = "🟢 YFKJ-SSHD"
+		}
+	}
+
+	w.Header().Set("Content-Type", "application/json")
+	json.NewEncoder(w).Encode(map[string]string{"status": status})
+}
+
 func (ui *WebUI) rootHandler(w http.ResponseWriter, r *http.Request) {
 	path := r.URL.Path
 

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

@@ -24,6 +24,11 @@ var routes = []Route{
 	},
 
 	{
+		"/api/sshd/status",
+		sshdStatusHandler,
+	},
+
+	{
 		"/api/service/log",
 		serviceLogHandler,
 	},

+ 2 - 2
web-ui.service/web_session.go

@@ -18,8 +18,8 @@ var (
 )
 
 const (
-	idleTimeout = 10 * time.Minute
-	gcPeriod    = 60 * time.Minute
+	idleTimeout = 30 * time.Minute
+	gcPeriod    = 90 * time.Minute
 )
 
 func createSession() string {

+ 5 - 4
web-ui.service/web_ui.go

@@ -58,9 +58,10 @@ func (ui *WebUI) Stop() error {
 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 	defer cancel()
 
-	if err := ui.server.Shutdown(ctx); err != nil {
-		ui.server.Close()
-		return err
+	err := ui.server.Shutdown(ctx)
+	if err != nil {
+		_ = ui.server.Close()
 	}
-	return nil
+
+	return err
 }