page2_handler.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "time"
  9. "hnyfkj.com.cn/rtu/linux/baseapp"
  10. )
  11. func downloadNetworkLog(w http.ResponseWriter, r *http.Request) {
  12. if r.Method != http.MethodGet {
  13. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  14. return
  15. }
  16. filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405"))
  17. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  18. defer os.Remove(tmpFile)
  19. cmd := exec.Command(
  20. "tar",
  21. "--warning=no-file-changed",
  22. "-czf",
  23. tmpFile,
  24. "-C",
  25. "/opt/yfkj/networkd.service/log",
  26. ".",
  27. )
  28. out, err := cmd.CombinedOutput()
  29. if err != nil {
  30. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  31. http.Error(w, string(out), http.StatusInternalServerError)
  32. return
  33. }
  34. }
  35. w.Header().Set("Content-Type", "application/gzip")
  36. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  37. http.ServeFile(w, r, tmpFile)
  38. }
  39. func restartNetworkService(w http.ResponseWriter, r *http.Request) {
  40. if r.Method != http.MethodPost {
  41. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  42. return
  43. }
  44. st, err := serviceStatus("yfkj-networkd.service")
  45. if err != nil || !st.Loaded {
  46. http.Error(w, "Failed to restart yfkj-networkd.service: Unit yfkj-networkd.service not found.\nexit status 5", http.StatusBadRequest)
  47. return
  48. }
  49. w.Header().Set("Content-Type", "application/json")
  50. w.Write([]byte(`{"status":"restarting"}`))
  51. if f, ok := w.(http.Flusher); ok {
  52. f.Flush()
  53. }
  54. go func() {
  55. time.Sleep(1 * time.Second)
  56. cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service")
  57. if out, err := cmd.CombinedOutput(); err != nil {
  58. baseapp.Logger.Errorf("[重启yfkj-networkd失败!!]: %v, output=%s", err, string(out))
  59. }
  60. }()
  61. }
  62. func networkStatusHandler(w http.ResponseWriter, r *http.Request) {
  63. var modem struct {
  64. ICCID string `json:"iccid"`
  65. RSSI string `json:"rssi"`
  66. }
  67. var net struct {
  68. Status string `json:"net_ok"`
  69. Type string `json:"net_type"`
  70. }
  71. err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net)
  72. err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem)
  73. netType := map[string]string{
  74. "有线": "🌐有线",
  75. "蜂窝": "📶蜂窝",
  76. }[net.Type]
  77. if netType == "" {
  78. netType = noValue
  79. }
  80. rssi := modem.RSSI
  81. if rssi == "" {
  82. rssi = noValue
  83. } else {
  84. rssi = "📶" + rssi
  85. }
  86. iccid := modem.ICCID
  87. if iccid == "" {
  88. iccid = noValue
  89. } else {
  90. iccid = "🔖" + iccid
  91. }
  92. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  93. json.NewEncoder(w).Encode(map[string]string{
  94. "var1": map[bool]string{true: "🟢正常", false: noValue}[err1 == nil || err2 == nil],
  95. "var2": map[bool]string{true: "🟢正常", false: noValue}[net.Status == "true"],
  96. "var3": netType,
  97. "var4": rssi,
  98. "var5": iccid,
  99. })
  100. }