page4_handler.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  56. callErr := callRPCResult(r.Context(), 7002, "core.getPositionInfo", nil, &pos)
  57. status := noValue
  58. lat := noValue
  59. lon := noValue
  60. if callErr == nil {
  61. status = "正常"
  62. lat = pos.Lat
  63. lon = pos.Lon
  64. }
  65. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  66. json.NewEncoder(w).Encode(map[string]string{
  67. "var1": status,
  68. "var2": lat,
  69. "var3": lon,
  70. })
  71. }