| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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})
- }
- func cameraTakePhotoHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- var req struct {
- Filename string `json:"filename"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- if req.Filename == "" {
- http.Error(w, "missing filename", http.StatusBadRequest)
- return
- }
- params := map[string]string{
- "format": "jpg",
- "outFilename": req.Filename,
- "timeoutMs": "300",
- }
- var result struct {
- Mark struct {
- CamModelName string `json:"CamModelName"`
- CamSerialNum string `json:"CamSerialNum"`
- ImgExposureTime float64 `json:"ImgExposureTime"`
- ImgWidth uint `json:"ImgWidth"`
- ImgHeight uint `json:"ImgHeight"`
- } `json:"mark"`
- Status string `json:"status"`
- }
- if err := callRPCResult(r.Context(), 7003, "core.takePhoto", params, &result); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(map[string]any{
- "ok": result.Status == "success",
- "var1": result.Mark.CamModelName,
- "var2": result.Mark.CamSerialNum,
- "var3": fmt.Sprintf("%.0f us", result.Mark.ImgExposureTime),
- "var4": result.Mark.ImgWidth,
- "var5": result.Mark.ImgHeight,
- })
- }
- func cameraGetImageHandler(w http.ResponseWriter, r *http.Request) {
- file := r.URL.Query().Get("file")
- if file == "" {
- http.Error(w, "missing file", http.StatusBadRequest)
- return
- }
- if file != "/tmp/camera_preview_0.jpg" && file != "/tmp/camera_preview_1.jpg" {
- http.Error(w, "invalid file", http.StatusBadRequest)
- return
- }
- if _, err := os.Stat(file); err != nil {
- http.Error(w, err.Error(), http.StatusNotFound)
- return
- }
- w.Header().Set("Content-Type", "image/jpeg")
- if r.URL.Query().Get("download") == "1" {
- w.Header().Set(
- "Content-Disposition",
- `attachment; filename="camera.jpg"`,
- )
- }
- http.ServeFile(w, r, file)
- }
- func setWaitAEDoneForTakePhoto(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- var req struct {
- WaitAE string `json:"waitAE"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- var result any
- if err := callRPCResult(r.Context(), 7003, "core.setWaitAEDoneForTakePhoto",
- map[string]string{"waitAE": req.WaitAE}, &result); 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,
- })
- }
|