mcs.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // start or stop mcs eq
  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. // param has value empid, deviceid use to query mcs server
  61. // use param upkeepid to query video from s.d.DB
  62. func (s *Service) McsGetFiles(mv *model.McsVideo) (err error) {
  63. url := "https://www.51cow.cn:7715/api/GetRecordFileList"
  64. st := time.Now().Local().AddDate(0, 0, -1).Format("2006-01-02")
  65. et := time.Now().Local().AddDate(0, 0, 1).Format("2006-01-02")
  66. sid := "7eea4610d4f1dcc572b7351f5a5fdef131571842484b03f05fa8e5a90486f227680272d2e47d7a98738b32473e0dffb348bd1b0273079c67d5ee3d90050d78c9"
  67. mfr := &model.McsFilesRequest{
  68. SessionId: sid,
  69. Did: mv.DeviceId,
  70. Wno: mv.UpkeepId,
  71. St: st,
  72. Et: et,
  73. Ft: "3",
  74. Lt: "-1",
  75. Pagesize: "10",
  76. }
  77. //query the video record file from s.d.DB
  78. cont := &model.McsVideo{
  79. UpkeepId: mv.UpkeepId,
  80. }
  81. mvs, e := s.QueryVideo(cont)
  82. if e != nil {
  83. return e
  84. }
  85. if len(mvs) == 0 {
  86. return errors.New("no video start record")
  87. }
  88. ntime := mvs[0].NewCreatedAt
  89. t := time.NewTicker(time.Second * 2)
  90. defer t.Stop()
  91. flag := false
  92. for !flag {
  93. select {
  94. case <-t.C:
  95. resp, e := s.http.NewRequest("GET", url+"?"+mfr.Encode(), nil)
  96. if e != nil {
  97. return e
  98. }
  99. mf := new(model.McsFilesResponse)
  100. e = s.http.Do(resp, mf)
  101. if e != nil {
  102. return e
  103. }
  104. //get the last record file
  105. f := LastMCsFile(mf)
  106. if f == nil {
  107. continue
  108. }
  109. // compare the time
  110. st, e := util.ParseDate(f.StartTime)
  111. if e != nil {
  112. log.Path("service McsGetFiles")
  113. continue
  114. }
  115. if st.Sub(ntime) < 10*time.Second && st.Sub(ntime) >= 0 {
  116. mv.CreatedAt = ntime
  117. mv.Location = "https://www.51cow.cn:7715/files" + f.Path
  118. e := s.UpdateOrAddVideo(mv)
  119. if e != nil {
  120. return e
  121. }
  122. flag = true
  123. t.Stop()
  124. <-McsChan
  125. } else {
  126. continue
  127. }
  128. }
  129. }
  130. return nil
  131. }
  132. // get the last record file from mcs server
  133. func LastMCsFile(mfr *model.McsFilesResponse) (pmf *model.McsFile) {
  134. if len(mfr.Data) == 0 {
  135. return
  136. }
  137. mf := mfr.Data[0]
  138. for _, v := range mfr.Data {
  139. if v.StartTime > mf.StartTime {
  140. mf = v
  141. }
  142. }
  143. return &mf
  144. }
  145. // param has value empid, upkeepid, deviceid,createdAt
  146. func (s *Service) UpdateOrAddVideo(m *model.McsVideo) (err error) {
  147. //new condition
  148. cton := map[string]interface{}{
  149. "upkeepId": m.UpkeepId,
  150. }
  151. rt := s.d.DB.Table("video").Where(cton).Updates(m)
  152. if rt.Error != nil {
  153. err = log.Error("UpdateOrAddVideo update ", rt.Error, m)
  154. return
  155. }
  156. if rt.RowsAffected > 0 {
  157. return
  158. }
  159. //add vedio
  160. if err = s.d.DB.Table("video").Create(&m).Error; err != nil {
  161. err = log.Error("UpdateOrAddVideo create ", err, m)
  162. }
  163. return
  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. func (s *Service) mcsAccounts(c *gin.Context) {
  172. ma := []model.McsAccount{
  173. {
  174. DeviceId: "apptest2",
  175. UserID: "PC",
  176. Password: "mcs8",
  177. },
  178. }
  179. c.JSON(200, ma)
  180. }