mcs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package service
  2. import (
  3. "time"
  4. "github.com/gin-gonic/gin"
  5. "github.com/pkg/errors"
  6. "kpt.xdmy/apiserver/model"
  7. "kpt.xdmy/apiserver/model/http"
  8. "kpt.xdmy/pkg/log"
  9. "kpt.xdmy/pkg/util"
  10. )
  11. var McsChan = make(chan string, 0)
  12. // 开启或者结束设备录像
  13. func (s *Service) McsPostHttp(mpd *model.McsPostDataReq) (res *http.Response, err error) {
  14. url := "http://8.130.17.8:7711/api/postData"
  15. req, e := s.http.NewRequest("POST", url, mpd)
  16. if e != nil {
  17. return nil, e
  18. }
  19. var r struct {
  20. Code int `json:"error"`
  21. Message string `json:"message"`
  22. }
  23. e = s.http.Do(req, &r)
  24. if e != nil {
  25. return nil, e
  26. }
  27. //add respone
  28. res = &http.Response{
  29. Message: r.Message,
  30. Code: r.Code,
  31. Success: false,
  32. }
  33. if res.Code == 200 {
  34. if mpd.Method == "startRecord" {
  35. mv := &model.McsVideo{
  36. EmpId: mpd.Data.PeopleNo,
  37. UpkeepId: mpd.Data.WorkNo,
  38. DeviceId: mpd.Data.DevID,
  39. NewCreatedAt: time.Now(),
  40. EndRecord: 2,
  41. }
  42. go s.UpdateOrAddVideo(mv)
  43. }
  44. if mpd.Method == "stopRecord" {
  45. mv := &model.McsVideo{
  46. EmpId: mpd.Data.PeopleNo,
  47. UpkeepId: mpd.Data.WorkNo,
  48. DeviceId: mpd.Data.DevID,
  49. }
  50. mv2 := &model.McsVideo{
  51. UpkeepId: mpd.Data.WorkNo,
  52. EndRecord: 1,
  53. }
  54. go s.UpdateOrAddVideo(mv2)
  55. go s.McsGetFiles(mv)
  56. }
  57. }
  58. return
  59. }
  60. // 查询保养单对应录像文件路径
  61. func (s *Service) McsGetFiles(mv *model.McsVideo) (err error) {
  62. url := "https://www.51cow.cn:7715/api/GetRecordFileList"
  63. st := time.Now().Local().AddDate(0, 0, -1).Format("2006-01-02")
  64. et := time.Now().Local().AddDate(0, 0, 1).Format("2006-01-02")
  65. sid := "7eea4610d4f1dcc572b7351f5a5fdef131571842484b03f05fa8e5a90486f227680272d2e47d7a98738b32473e0dffb348bd1b0273079c67d5ee3d90050d78c9"
  66. mfr := &model.McsFilesRequest{
  67. SessionId: sid,
  68. Did: mv.DeviceId,
  69. Wno: mv.UpkeepId,
  70. St: st,
  71. Et: et,
  72. Ft: "3",
  73. Lt: "-1",
  74. Pagesize: "10",
  75. }
  76. //query the video record file from s.d.DB
  77. cont := &model.McsVideo{
  78. UpkeepId: mv.UpkeepId,
  79. }
  80. mvs, e := s.QueryVideo(cont)
  81. if e != nil {
  82. return e
  83. }
  84. if len(mvs) == 0 {
  85. return errors.New("no video start record")
  86. }
  87. ntime := mvs[0].NewCreatedAt
  88. t := time.NewTicker(time.Second * 2)
  89. defer t.Stop()
  90. flag := false
  91. for !flag {
  92. select {
  93. case <-t.C:
  94. resp, e := s.http.NewRequest("GET", url+"?"+mfr.Encode(), nil)
  95. if e != nil {
  96. return e
  97. }
  98. mf := new(model.McsFilesResponse)
  99. e = s.http.Do(resp, mf)
  100. if e != nil {
  101. return e
  102. }
  103. //get the last record file
  104. f := LastMCsFile(mf)
  105. if f == nil {
  106. continue
  107. }
  108. // compare the time
  109. st, e := util.ParseDate(f.StartTime)
  110. if e != nil {
  111. log.Path("service McsGetFiles")
  112. continue
  113. }
  114. if st.Sub(ntime) < 10*time.Second && st.Sub(ntime) >= 0 {
  115. mv.CreatedAt = ntime
  116. mv.Location = "https://www.51cow.cn:7715/files" + f.Path
  117. e := s.UpdateOrAddVideo(mv)
  118. if e != nil {
  119. return e
  120. }
  121. flag = true
  122. t.Stop()
  123. <-McsChan
  124. } else {
  125. continue
  126. }
  127. }
  128. }
  129. return nil
  130. }
  131. // 获取最新的录像记录
  132. func LastMCsFile(mfr *model.McsFilesResponse) (pmf *model.McsFile) {
  133. if len(mfr.Data) == 0 {
  134. return
  135. }
  136. mf := mfr.Data[0]
  137. for _, v := range mfr.Data {
  138. if v.StartTime > mf.StartTime {
  139. mf = v
  140. }
  141. }
  142. return &mf
  143. }
  144. // 更新录像文件
  145. func (s *Service) UpdateOrAddVideo(m *model.McsVideo) (err error) {
  146. //new condition
  147. cton := map[string]interface{}{
  148. "upkeepId": m.UpkeepId,
  149. }
  150. rt := s.d.DB.Table("video").Where(cton).Updates(m)
  151. if rt.Error != nil {
  152. err = log.Error("UpdateOrAddVideo update ", rt.Error, m)
  153. return
  154. }
  155. if rt.RowsAffected > 0 {
  156. return
  157. }
  158. //add vedio
  159. if err = s.d.DB.Table("video").Create(&m).Error; err != nil {
  160. err = log.Error("UpdateOrAddVideo create ", err, m)
  161. }
  162. return
  163. }
  164. // 查询录像文件
  165. func (s *Service) QueryVideo(m *model.McsVideo) (res []model.McsVideo, err error) {
  166. if err = s.d.DB.Table("video").Where(m).Find(&res).Error; err != nil {
  167. err = log.Error("QueryVideo query ", err, m)
  168. }
  169. return
  170. }
  171. // 设备账号列表
  172. func (s *Service) mcsAccounts(c *gin.Context) {
  173. ma := []model.McsAccount{
  174. {
  175. DeviceId: "apptest2",
  176. UserID: "PC",
  177. Password: "mcs8",
  178. },
  179. }
  180. c.JSON(200, ma)
  181. }