| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package main
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "os"
- "os/exec"
- "strconv"
- "time"
- )
- func downloadCameraLog(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- filename := fmt.Sprintf("camera.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/camera-capture.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 restartCameraService(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-camera-capture.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 cameraStatusHandler(w http.ResponseWriter, r *http.Request) {
- var countHK, countDH string
- var power struct {
- Enable string `json:"enable"`
- }
- var check string
- avar1, avar2, avar3, avar4, avar5, avar6 := noValue, noValue, noValue, noValue, noValue, noValue
- if callRPCResult(r.Context(), 7003, "core.getSupportedCameraCount", map[string]string{"type": "hk"}, &countHK) == nil {
- avar3 = countHK
- }
- if callRPCResult(r.Context(), 7003, "core.getSupportedCameraCount", map[string]string{"type": "dh"}, &countDH) == nil {
- avar4 = countDH
- }
- if avar3 != noValue && avar4 != noValue {
- hk, _ := strconv.Atoi(avar3)
- dh, _ := strconv.Atoi(avar4)
- avar2 = strconv.Itoa(hk + dh)
- avar1 = "🟢正常"
- }
- callRPCResult(r.Context(), 7003, "core.getGigeCameraPower", nil, &power)
- if power.Enable == "true" {
- avar5 = "🟢打开"
- }
- callRPCResult(r.Context(), 7003, "core.checkGigeCamera", map[string]string{"ip": "192.168.100.100", "prefix": "24"}, &check)
- if check == "success" {
- avar6 = "🟢正常"
- }
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
- json.NewEncoder(w).Encode(map[string]string{
- "avar1": avar1, "avar2": avar2,
- "avar3": avar3, "avar4": avar4,
- "avar5": avar5, "avar6": avar6,
- })
- }
- func cameraPowerHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- var req struct {
- Enable string `json:"enable"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- var result any
- err := callRPCResult(r.Context(), 7003, "core.setGigeCameraPower", map[string]string{"enable": req.Enable}, &result)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(map[string]bool{
- "ok": true,
- })
- }
- func cameraPingHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- avar6 := noValue
- check := ""
- callRPCResult(r.Context(), 7003, "core.checkGigeCamera",
- map[string]string{"ip": "192.168.100.100", "prefix": "24"}, &check)
- if check == "success" {
- avar6 = "🟢正常"
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(map[string]string{"avar6": avar6})
- }
|