|
|
@@ -10,11 +10,12 @@ import (
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"strings"
|
|
|
- "time"
|
|
|
|
|
|
"hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
|
|
|
)
|
|
|
|
|
|
+const noValue = "--"
|
|
|
+
|
|
|
type LoginReq struct {
|
|
|
Username string `json:"username"`
|
|
|
Password string `json:"password"`
|
|
|
@@ -73,8 +74,12 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
func getIMEIHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
- data, _ := os.ReadFile("/var/device_imei.txt")
|
|
|
- w.Write(data)
|
|
|
+ data, err := os.ReadFile("/var/device_imei.txt")
|
|
|
+ if err != nil {
|
|
|
+ w.Write([]byte(noValue))
|
|
|
+ } else {
|
|
|
+ w.Write(append([]byte("🆔"), data...))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func (ui *WebUI) rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
@@ -269,101 +274,3 @@ func serviceLogLevelHandler(port int) http.HandlerFunc {
|
|
|
serviceLogLevel(w, r, port)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-func DownloadNetworkLog(w http.ResponseWriter, r *http.Request) {
|
|
|
- if r.Method != http.MethodGet {
|
|
|
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- filename := fmt.Sprintf("network.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",
|
|
|
- "/opt/yfkj/networkd.service/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 RestartNetworkService(w http.ResponseWriter, r *http.Request) {
|
|
|
- if r.Method != http.MethodPost {
|
|
|
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service")
|
|
|
-
|
|
|
- if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
- http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- w.Header().Set("Content-Type", "application/json")
|
|
|
- w.Write([]byte(`{"status":"ok"}`))
|
|
|
-}
|
|
|
-
|
|
|
-func NetworkStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
- var modem struct {
|
|
|
- ICCID string `json:"iccid"`
|
|
|
- RSSI string `json:"rssi"`
|
|
|
- }
|
|
|
- var net struct {
|
|
|
- Status string `json:"net_ok"`
|
|
|
- Type string `json:"net_type"`
|
|
|
- }
|
|
|
-
|
|
|
- err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net)
|
|
|
- err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem)
|
|
|
-
|
|
|
- netType := map[string]string{
|
|
|
- "有线": "🟢有线",
|
|
|
- "蜂窝": "🟢蜂窝",
|
|
|
- }[net.Type]
|
|
|
-
|
|
|
- if netType == "" {
|
|
|
- netType = "--"
|
|
|
- }
|
|
|
-
|
|
|
- rssi := modem.RSSI
|
|
|
- if rssi == "" {
|
|
|
- rssi = "--"
|
|
|
- } else {
|
|
|
- rssi = "📶" + rssi
|
|
|
- }
|
|
|
-
|
|
|
- iccid := modem.ICCID
|
|
|
- if iccid == "" {
|
|
|
- iccid = "--"
|
|
|
- } else {
|
|
|
- iccid = "💳" + iccid
|
|
|
- }
|
|
|
-
|
|
|
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
- json.NewEncoder(w).Encode(map[string]string{
|
|
|
- "var1": map[bool]string{true: "🟢正常", false: "--"}[err1 == nil || err2 == nil],
|
|
|
- "var2": map[bool]string{true: "🟢正常", false: "--"}[net.Status == "true"],
|
|
|
- "var3": netType,
|
|
|
- "var4": rssi,
|
|
|
- "var5": iccid,
|
|
|
- })
|
|
|
-}
|