page4_handler.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "time"
  9. )
  10. func downloadGnssLog(w http.ResponseWriter, r *http.Request) {
  11. if r.Method != http.MethodGet {
  12. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  13. return
  14. }
  15. filename := fmt.Sprintf("gnss.log.%s.tar.gz", time.Now().Format("20060102150405"))
  16. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  17. defer os.Remove(tmpFile)
  18. cmd := exec.Command(
  19. "tar",
  20. "--warning=no-file-changed",
  21. "-czf",
  22. tmpFile,
  23. "-C",
  24. "/opt/yfkj/gnss.service/log",
  25. ".",
  26. )
  27. out, err := cmd.CombinedOutput()
  28. if err != nil {
  29. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  30. http.Error(w, string(out), http.StatusInternalServerError)
  31. return
  32. }
  33. }
  34. w.Header().Set("Content-Type", "application/gzip")
  35. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  36. http.ServeFile(w, r, tmpFile)
  37. }
  38. func restartGnssService(w http.ResponseWriter, r *http.Request) {
  39. if r.Method != http.MethodPost {
  40. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  41. return
  42. }
  43. cmd := exec.Command("systemctl", "restart", "yfkj-gnss.service")
  44. if out, err := cmd.CombinedOutput(); err != nil {
  45. http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
  46. return
  47. }
  48. w.Header().Set("Content-Type", "application/json")
  49. w.Write([]byte(`{"status":"ok"}`))
  50. }
  51. func gnssStatusHandler(w http.ResponseWriter, r *http.Request) {
  52. var pos struct {
  53. Lat string `json:"lat"`
  54. Lon string `json:"lon"`
  55. InUse string `json:"inuse"`
  56. InView string `json:"inview"`
  57. }
  58. callErr := callRPCResult(r.Context(), 7002, "core.getPositionInfo", nil, &pos)
  59. status := noValue
  60. lat := noValue
  61. lon := noValue
  62. inuse := noValue
  63. inview := noValue
  64. if callErr == nil {
  65. status = "🟢正常"
  66. if pos.Lat != "" {
  67. lat = pos.Lat
  68. }
  69. if pos.Lon != "" {
  70. lon = pos.Lon
  71. }
  72. if pos.InUse != "" {
  73. inuse = pos.InUse
  74. }
  75. if pos.InView != "" {
  76. inview = pos.InView
  77. }
  78. }
  79. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  80. json.NewEncoder(w).Encode(map[string]string{
  81. "var1": status,
  82. "var2": lat,
  83. "var3": lon,
  84. "var5": inuse,
  85. "var6": inview,
  86. })
  87. }