page5_handler.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "strconv"
  9. "time"
  10. )
  11. func downloadCameraLog(w http.ResponseWriter, r *http.Request) {
  12. if r.Method != http.MethodGet {
  13. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  14. return
  15. }
  16. filename := fmt.Sprintf("camera.log.%s.tar.gz", time.Now().Format("20060102150405"))
  17. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  18. defer os.Remove(tmpFile)
  19. cmd := exec.Command(
  20. "tar",
  21. "--warning=no-file-changed",
  22. "-czf",
  23. tmpFile,
  24. "-C",
  25. "/opt/yfkj/camera-capture.service/log",
  26. ".",
  27. )
  28. out, err := cmd.CombinedOutput()
  29. if err != nil {
  30. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  31. http.Error(w, string(out), http.StatusInternalServerError)
  32. return
  33. }
  34. }
  35. w.Header().Set("Content-Type", "application/gzip")
  36. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  37. http.ServeFile(w, r, tmpFile)
  38. }
  39. func restartCameraService(w http.ResponseWriter, r *http.Request) {
  40. if r.Method != http.MethodPost {
  41. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  42. return
  43. }
  44. cmd := exec.Command("systemctl", "restart", "yfkj-camera-capture.service")
  45. if out, err := cmd.CombinedOutput(); err != nil {
  46. http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
  47. return
  48. }
  49. w.Header().Set("Content-Type", "application/json")
  50. w.Write([]byte(`{"status":"ok"}`))
  51. }
  52. func cameraStatusHandler(w http.ResponseWriter, r *http.Request) {
  53. var countHK, countDH string
  54. var power struct {
  55. Enable string `json:"enable"`
  56. }
  57. var check string
  58. avar1, avar2, avar3, avar4, avar5, avar6 := noValue, noValue, noValue, noValue, noValue, noValue
  59. if callRPCResult(r.Context(), 7003, "core.getSupportedCameraCount", map[string]string{"type": "hk"}, &countHK) == nil {
  60. avar3 = countHK
  61. }
  62. if callRPCResult(r.Context(), 7003, "core.getSupportedCameraCount", map[string]string{"type": "dh"}, &countDH) == nil {
  63. avar4 = countDH
  64. }
  65. if avar3 != noValue && avar4 != noValue {
  66. hk, _ := strconv.Atoi(avar3)
  67. dh, _ := strconv.Atoi(avar4)
  68. avar2 = strconv.Itoa(hk + dh)
  69. avar1 = "🟢正常"
  70. }
  71. callRPCResult(r.Context(), 7003, "core.getGigeCameraPower", nil, &power)
  72. if power.Enable == "true" {
  73. avar5 = "🟢打开"
  74. }
  75. callRPCResult(r.Context(), 7003, "core.checkGigeCamera", map[string]string{"ip": "192.168.100.100", "prefix": "24"}, &check)
  76. if check == "success" {
  77. avar6 = "🟢正常"
  78. }
  79. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  80. json.NewEncoder(w).Encode(map[string]string{
  81. "avar1": avar1, "avar2": avar2,
  82. "avar3": avar3, "avar4": avar4,
  83. "avar5": avar5, "avar6": avar6,
  84. })
  85. }
  86. func cameraPowerHandler(w http.ResponseWriter, r *http.Request) {
  87. if r.Method != http.MethodPost {
  88. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  89. return
  90. }
  91. var req struct {
  92. Enable string `json:"enable"`
  93. }
  94. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  95. http.Error(w, err.Error(), http.StatusBadRequest)
  96. return
  97. }
  98. var result any
  99. err := callRPCResult(r.Context(), 7003, "core.setGigeCameraPower", map[string]string{"enable": req.Enable}, &result)
  100. if err != nil {
  101. http.Error(w, err.Error(), http.StatusInternalServerError)
  102. return
  103. }
  104. w.Header().Set("Content-Type", "application/json")
  105. json.NewEncoder(w).Encode(map[string]bool{
  106. "ok": true,
  107. })
  108. }
  109. func cameraPingHandler(w http.ResponseWriter, r *http.Request) {
  110. if r.Method != http.MethodPost {
  111. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  112. return
  113. }
  114. avar6 := noValue
  115. check := ""
  116. callRPCResult(r.Context(), 7003, "core.checkGigeCamera",
  117. map[string]string{"ip": "192.168.100.100", "prefix": "24"}, &check)
  118. if check == "success" {
  119. avar6 = "🟢正常"
  120. }
  121. w.Header().Set("Content-Type", "application/json")
  122. json.NewEncoder(w).Encode(map[string]string{"avar6": avar6})
  123. }
  124. func cameraTakePhotoHandler(w http.ResponseWriter, r *http.Request) {
  125. if r.Method != http.MethodPost {
  126. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  127. return
  128. }
  129. var req struct {
  130. Filename string `json:"filename"`
  131. }
  132. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  133. http.Error(w, err.Error(), http.StatusBadRequest)
  134. return
  135. }
  136. if req.Filename == "" {
  137. http.Error(w, "missing filename", http.StatusBadRequest)
  138. return
  139. }
  140. params := map[string]string{
  141. "format": "jpg",
  142. "outFilename": req.Filename,
  143. "timeoutMs": "300",
  144. }
  145. var result struct {
  146. Mark struct {
  147. CamModelName string `json:"CamModelName"`
  148. CamSerialNum string `json:"CamSerialNum"`
  149. ImgExposureTime float64 `json:"ImgExposureTime"`
  150. ImgWidth uint `json:"ImgWidth"`
  151. ImgHeight uint `json:"ImgHeight"`
  152. } `json:"mark"`
  153. Status string `json:"status"`
  154. }
  155. if err := callRPCResult(r.Context(), 7003, "core.takePhoto", params, &result); err != nil {
  156. http.Error(w, err.Error(), http.StatusInternalServerError)
  157. return
  158. }
  159. w.Header().Set("Content-Type", "application/json")
  160. json.NewEncoder(w).Encode(map[string]any{
  161. "ok": result.Status == "success",
  162. "var1": result.Mark.CamModelName,
  163. "var2": result.Mark.CamSerialNum,
  164. "var3": fmt.Sprintf("%.0f us", result.Mark.ImgExposureTime),
  165. "var4": result.Mark.ImgWidth,
  166. "var5": result.Mark.ImgHeight,
  167. })
  168. }
  169. func cameraGetImageHandler(w http.ResponseWriter, r *http.Request) {
  170. file := r.URL.Query().Get("file")
  171. if file == "" {
  172. http.Error(w, "missing file", http.StatusBadRequest)
  173. return
  174. }
  175. if file != "/tmp/camera_preview_0.jpg" && file != "/tmp/camera_preview_1.jpg" {
  176. http.Error(w, "invalid file", http.StatusBadRequest)
  177. return
  178. }
  179. if _, err := os.Stat(file); err != nil {
  180. http.Error(w, err.Error(), http.StatusNotFound)
  181. return
  182. }
  183. w.Header().Set("Content-Type", "image/jpeg")
  184. if r.URL.Query().Get("download") == "1" {
  185. w.Header().Set(
  186. "Content-Disposition",
  187. `attachment; filename="camera.jpg"`,
  188. )
  189. }
  190. http.ServeFile(w, r, file)
  191. }
  192. func setWaitAEDoneForTakePhoto(w http.ResponseWriter, r *http.Request) {
  193. if r.Method != http.MethodPost {
  194. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  195. return
  196. }
  197. var req struct {
  198. WaitAE string `json:"waitAE"`
  199. }
  200. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  201. http.Error(w, err.Error(), http.StatusBadRequest)
  202. return
  203. }
  204. var result any
  205. if err := callRPCResult(r.Context(), 7003, "core.setWaitAEDoneForTakePhoto",
  206. map[string]string{"waitAE": req.WaitAE}, &result); err != nil {
  207. http.Error(w, err.Error(), http.StatusInternalServerError)
  208. return
  209. }
  210. w.Header().Set("Content-Type", "application/json")
  211. json.NewEncoder(w).Encode(map[string]bool{
  212. "ok": true,
  213. })
  214. }