page2_handler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. w.Header().Set("Content-Type", "application/json")
  45. w.Write([]byte(`{"status":"restarting..."}`))
  46. go func() {
  47. cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service")
  48. out, err := cmd.CombinedOutput()
  49. if err != nil {
  50. baseapp.Logger.Errorf("[重启yfkj-networkd失败!!]: %v, output=%s", err, string(out))
  51. }
  52. }()
  53. }
  54. func networkStatusHandler(w http.ResponseWriter, r *http.Request) {
  55. var modem struct {
  56. ICCID string `json:"iccid"`
  57. RSSI string `json:"rssi"`
  58. }
  59. var net struct {
  60. Status string `json:"net_ok"`
  61. Type string `json:"net_type"`
  62. }
  63. err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net)
  64. err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem)
  65. netType := map[string]string{
  66. "有线": "🌐有线",
  67. "蜂窝": "📶蜂窝",
  68. }[net.Type]
  69. if netType == "" {
  70. netType = noValue
  71. }
  72. rssi := modem.RSSI
  73. if rssi == "" {
  74. rssi = noValue
  75. } else {
  76. rssi = "📶" + rssi
  77. }
  78. iccid := modem.ICCID
  79. if iccid == "" {
  80. iccid = noValue
  81. } else {
  82. iccid = "🔖" + iccid
  83. }
  84. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  85. json.NewEncoder(w).Encode(map[string]string{
  86. "var1": map[bool]string{true: "🟢正常", false: noValue}[err1 == nil || err2 == nil],
  87. "var2": map[bool]string{true: "🟢正常", false: noValue}[net.Status == "true"],
  88. "var3": netType,
  89. "var4": rssi,
  90. "var5": iccid,
  91. })
  92. }