restful.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package gb28181
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "m7s.live/engine/v4/util"
  9. )
  10. var (
  11. playScaleValues = map[float32]bool{0.25: true, 0.5: true, 1: true, 2: true, 4: true}
  12. )
  13. func (c *GB28181Config) API_list(w http.ResponseWriter, r *http.Request) {
  14. query := r.URL.Query()
  15. if query.Get("interval") == "" {
  16. query.Set("interval", "5s")
  17. }
  18. util.ReturnFetchValue(func() (list []*Device) {
  19. list = make([]*Device, 0)
  20. Devices.Range(func(key, value interface{}) bool {
  21. list = append(list, value.(*Device))
  22. return true
  23. })
  24. return
  25. }, w, r)
  26. }
  27. func (c *GB28181Config) API_records(w http.ResponseWriter, r *http.Request) {
  28. query := r.URL.Query()
  29. id := query.Get("id")
  30. channel := query.Get("channel")
  31. startTime := query.Get("startTime")
  32. endTime := query.Get("endTime")
  33. trange := strings.Split(query.Get("range"), "-")
  34. if len(trange) == 2 {
  35. startTime = trange[0]
  36. endTime = trange[1]
  37. }
  38. if c := FindChannel(id, channel); c != nil {
  39. res, err := c.QueryRecord(startTime, endTime)
  40. if err == nil {
  41. util.ReturnValue(res, w, r)
  42. } else {
  43. util.ReturnError(util.APIErrorInternal, err.Error(), w, r)
  44. }
  45. } else {
  46. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  47. }
  48. }
  49. func (c *GB28181Config) API_control(w http.ResponseWriter, r *http.Request) {
  50. id := r.URL.Query().Get("id")
  51. channel := r.URL.Query().Get("channel")
  52. ptzcmd := r.URL.Query().Get("ptzcmd")
  53. if c := FindChannel(id, channel); c != nil {
  54. util.ReturnError(0, fmt.Sprintf("control code:%d", c.Control(ptzcmd)), w, r)
  55. } else {
  56. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  57. }
  58. }
  59. func (c *GB28181Config) API_ptz(w http.ResponseWriter, r *http.Request) {
  60. q := r.URL.Query()
  61. id := q.Get("id")
  62. channel := q.Get("channel")
  63. cmd := q.Get("cmd") // 命令名称,见 ptz.go name2code 定义
  64. hs := q.Get("hSpeed") // 水平速度
  65. vs := q.Get("vSpeed") // 垂直速度
  66. zs := q.Get("zSpeed") // 缩放速度
  67. hsN, err := strconv.ParseUint(hs, 10, 8)
  68. if err != nil {
  69. util.ReturnError(util.APIErrorQueryParse, "hSpeed parameter is invalid", w, r)
  70. return
  71. }
  72. vsN, err := strconv.ParseUint(vs, 10, 8)
  73. if err != nil {
  74. util.ReturnError(util.APIErrorQueryParse, "vSpeed parameter is invalid", w, r)
  75. return
  76. }
  77. zsN, err := strconv.ParseUint(zs, 10, 8)
  78. if err != nil {
  79. util.ReturnError(util.APIErrorQueryParse, "zSpeed parameter is invalid", w, r)
  80. return
  81. }
  82. ptzcmd, err := toPtzStrByCmdName(cmd, uint8(hsN), uint8(vsN), uint8(zsN))
  83. if err != nil {
  84. util.ReturnError(util.APIErrorQueryParse, err.Error(), w, r)
  85. return
  86. }
  87. if c := FindChannel(id, channel); c != nil {
  88. code := c.Control(ptzcmd)
  89. util.ReturnError(code, "device received", w, r)
  90. } else {
  91. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  92. }
  93. }
  94. func (c *GB28181Config) API_invite(w http.ResponseWriter, r *http.Request) {
  95. query := r.URL.Query()
  96. id := query.Get("id")
  97. channel := query.Get("channel")
  98. streamPath := query.Get("streamPath")
  99. port, _ := strconv.Atoi(query.Get("mediaPort"))
  100. opt := InviteOptions{
  101. dump: query.Get("dump"),
  102. MediaPort: uint16(port),
  103. StreamPath: streamPath,
  104. }
  105. startTime := query.Get("startTime")
  106. endTime := query.Get("endTime")
  107. trange := strings.Split(query.Get("range"), "-")
  108. if len(trange) == 2 {
  109. startTime = trange[0]
  110. endTime = trange[1]
  111. }
  112. opt.Validate(startTime, endTime)
  113. if c := FindChannel(id, channel); c == nil {
  114. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  115. } else if opt.IsLive() && c.State.Load() > 0 {
  116. util.ReturnError(util.APIErrorQueryParse, "live stream already exists", w, r)
  117. } else if code, err := c.Invite(&opt); err == nil {
  118. if code == 200 {
  119. util.ReturnOK(w, r)
  120. } else {
  121. util.ReturnError(util.APIErrorInternal, fmt.Sprintf("invite return code %d", code), w, r)
  122. }
  123. } else {
  124. util.ReturnError(util.APIErrorInternal, err.Error(), w, r)
  125. }
  126. }
  127. func (c *GB28181Config) API_bye(w http.ResponseWriter, r *http.Request) {
  128. id := r.URL.Query().Get("id")
  129. channel := r.URL.Query().Get("channel")
  130. streamPath := r.URL.Query().Get("streamPath")
  131. if c := FindChannel(id, channel); c != nil {
  132. util.ReturnError(0, fmt.Sprintf("bye code:%d", c.Bye(streamPath)), w, r)
  133. } else {
  134. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  135. }
  136. }
  137. func (c *GB28181Config) API_play_pause(w http.ResponseWriter, r *http.Request) {
  138. id := r.URL.Query().Get("id")
  139. channel := r.URL.Query().Get("channel")
  140. streamPath := r.URL.Query().Get("streamPath")
  141. if c := FindChannel(id, channel); c != nil {
  142. util.ReturnError(0, fmt.Sprintf("pause code:%d", c.Pause(streamPath)), w, r)
  143. } else {
  144. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  145. }
  146. }
  147. func (c *GB28181Config) API_play_resume(w http.ResponseWriter, r *http.Request) {
  148. id := r.URL.Query().Get("id")
  149. channel := r.URL.Query().Get("channel")
  150. streamPath := r.URL.Query().Get("streamPath")
  151. if c := FindChannel(id, channel); c != nil {
  152. util.ReturnError(0, fmt.Sprintf("resume code:%d", c.Resume(streamPath)), w, r)
  153. } else {
  154. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  155. }
  156. }
  157. func (c *GB28181Config) API_play_seek(w http.ResponseWriter, r *http.Request) {
  158. id := r.URL.Query().Get("id")
  159. channel := r.URL.Query().Get("channel")
  160. streamPath := r.URL.Query().Get("streamPath")
  161. secStr := r.URL.Query().Get("second")
  162. sec, err := strconv.ParseUint(secStr, 10, 32)
  163. if err != nil {
  164. util.ReturnError(util.APIErrorQueryParse, "second parameter is invalid: "+err.Error(), w, r)
  165. return
  166. }
  167. if c := FindChannel(id, channel); c != nil {
  168. util.ReturnError(0, fmt.Sprintf("play code:%d", c.PlayAt(streamPath, uint(sec))), w, r)
  169. } else {
  170. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  171. }
  172. }
  173. func (c *GB28181Config) API_play_forward(w http.ResponseWriter, r *http.Request) {
  174. id := r.URL.Query().Get("id")
  175. channel := r.URL.Query().Get("channel")
  176. streamPath := r.URL.Query().Get("streamPath")
  177. speedStr := r.URL.Query().Get("speed")
  178. speed, err := strconv.ParseFloat(speedStr, 32)
  179. secondErrMsg := "speed parameter is invalid, should be one of 0.25,0.5,1,2,4"
  180. if err != nil || !playScaleValues[float32(speed)] {
  181. util.ReturnError(util.APIErrorQueryParse, secondErrMsg, w, r)
  182. return
  183. }
  184. if c := FindChannel(id, channel); c != nil {
  185. util.ReturnError(0, fmt.Sprintf("playforward code:%d", c.PlayForward(streamPath, float32(speed))), w, r)
  186. } else {
  187. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q channel %q not found", id, channel), w, r)
  188. }
  189. }
  190. func (c *GB28181Config) API_position(w http.ResponseWriter, r *http.Request) {
  191. //CORS(w, r)
  192. query := r.URL.Query()
  193. //设备id
  194. id := query.Get("id")
  195. //订阅周期(单位:秒)
  196. expires := query.Get("expires")
  197. //订阅间隔(单位:秒)
  198. interval := query.Get("interval")
  199. expiresInt, err := time.ParseDuration(expires)
  200. if expires == "" || err != nil {
  201. expiresInt = c.Position.Expires
  202. }
  203. intervalInt, err := time.ParseDuration(interval)
  204. if interval == "" || err != nil {
  205. intervalInt = c.Position.Interval
  206. }
  207. if v, ok := Devices.Load(id); ok {
  208. d := v.(*Device)
  209. util.ReturnError(0, fmt.Sprintf("mobileposition code:%d", d.MobilePositionSubscribe(id, expiresInt, intervalInt)), w, r)
  210. } else {
  211. util.ReturnError(util.APIErrorNotFound, fmt.Sprintf("device %q not found", id), w, r)
  212. }
  213. }
  214. type DevicePosition struct {
  215. ID string
  216. GpsTime time.Time //gps时间
  217. Longitude string //经度
  218. Latitude string //纬度
  219. }
  220. func (c *GB28181Config) API_get_position(w http.ResponseWriter, r *http.Request) {
  221. query := r.URL.Query()
  222. //设备id
  223. id := query.Get("id")
  224. if query.Get("interval") == "" {
  225. query.Set("interval", fmt.Sprintf("%ds", c.Position.Interval.Seconds()))
  226. }
  227. util.ReturnFetchValue(func() (list []*DevicePosition) {
  228. if id == "" {
  229. Devices.Range(func(key, value interface{}) bool {
  230. d := value.(*Device)
  231. if time.Since(d.GpsTime) <= c.Position.Interval {
  232. list = append(list, &DevicePosition{ID: d.ID, GpsTime: d.GpsTime, Longitude: d.Longitude, Latitude: d.Latitude})
  233. }
  234. return true
  235. })
  236. } else if v, ok := Devices.Load(id); ok {
  237. d := v.(*Device)
  238. list = append(list, &DevicePosition{ID: d.ID, GpsTime: d.GpsTime, Longitude: d.Longitude, Latitude: d.Latitude})
  239. }
  240. return
  241. }, w, r)
  242. }