| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package main
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "os"
- "os/exec"
- "time"
- )
- func downloadGnssLog(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- filename := fmt.Sprintf("gnss.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/gnss.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 restartGnssService(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-gnss.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 gnssStatusHandler(w http.ResponseWriter, r *http.Request) {
- var pos struct {
- Lat string `json:"lat"`
- Lon string `json:"lon"`
- }
- callErr := callRPCResult(r.Context(), 7002, "core.getPositionInfo", nil, &pos)
- status := noValue
- lat := noValue
- lon := noValue
- if callErr == nil {
- status = "正常"
- lat = pos.Lat
- lon = pos.Lon
- }
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
- json.NewEncoder(w).Encode(map[string]string{
- "var1": status,
- "var2": lat,
- "var3": lon,
- })
- }
|