package service import ( "time" "github.com/gin-gonic/gin" "github.com/pkg/errors" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/pkg/log" "kpt.xdmy/pkg/util" ) var McsChan = make(chan string, 0) // 开启或者结束设备录像 func (s *Service) McsPostHttp(mpd *model.McsPostDataReq) (res *http.Response, err error) { url := "http://8.130.17.8:7711/api/postData" req, e := s.http.NewRequest("POST", url, mpd) if e != nil { return nil, e } var r struct { Code int `json:"error"` Message string `json:"message"` } e = s.http.Do(req, &r) if e != nil { return nil, e } //add respone res = &http.Response{ Message: r.Message, Code: r.Code, Success: false, } if res.Code == 200 { if mpd.Method == "startRecord" { mv := &model.McsVideo{ EmpId: mpd.Data.PeopleNo, UpkeepId: mpd.Data.WorkNo, DeviceId: mpd.Data.DevID, NewCreatedAt: time.Now(), EndRecord: 2, } go s.UpdateOrAddVideo(mv) } if mpd.Method == "stopRecord" { mv := &model.McsVideo{ EmpId: mpd.Data.PeopleNo, UpkeepId: mpd.Data.WorkNo, DeviceId: mpd.Data.DevID, } mv2 := &model.McsVideo{ UpkeepId: mpd.Data.WorkNo, EndRecord: 1, } go s.UpdateOrAddVideo(mv2) go s.McsGetFiles(mv) } } return } // 查询保养单对应录像文件路径 func (s *Service) McsGetFiles(mv *model.McsVideo) (err error) { url := "https://www.51cow.cn:7715/api/GetRecordFileList" st := time.Now().Local().AddDate(0, 0, -1).Format("2006-01-02") et := time.Now().Local().AddDate(0, 0, 1).Format("2006-01-02") sid := "7eea4610d4f1dcc572b7351f5a5fdef131571842484b03f05fa8e5a90486f227680272d2e47d7a98738b32473e0dffb348bd1b0273079c67d5ee3d90050d78c9" mfr := &model.McsFilesRequest{ SessionId: sid, Did: mv.DeviceId, Wno: mv.UpkeepId, St: st, Et: et, Ft: "3", Lt: "-1", Pagesize: "10", } //query the video record file from s.d.DB cont := &model.McsVideo{ UpkeepId: mv.UpkeepId, } mvs, e := s.QueryVideo(cont) if e != nil { return e } if len(mvs) == 0 { return errors.New("no video start record") } ntime := mvs[0].NewCreatedAt t := time.NewTicker(time.Second * 2) defer t.Stop() flag := false for !flag { select { case <-t.C: resp, e := s.http.NewRequest("GET", url+"?"+mfr.Encode(), nil) if e != nil { return e } mf := new(model.McsFilesResponse) e = s.http.Do(resp, mf) if e != nil { return e } //get the last record file f := LastMCsFile(mf) if f == nil { continue } // compare the time st, e := util.ParseDate(f.StartTime) if e != nil { log.Path("service McsGetFiles") continue } if st.Sub(ntime) < 10*time.Second && st.Sub(ntime) >= 0 { mv.CreatedAt = ntime mv.Location = "https://www.51cow.cn:7715/files" + f.Path e := s.UpdateOrAddVideo(mv) if e != nil { return e } flag = true t.Stop() <-McsChan } else { continue } } } return nil } // 获取最新的录像记录 func LastMCsFile(mfr *model.McsFilesResponse) (pmf *model.McsFile) { if len(mfr.Data) == 0 { return } mf := mfr.Data[0] for _, v := range mfr.Data { if v.StartTime > mf.StartTime { mf = v } } return &mf } // 更新录像文件 func (s *Service) UpdateOrAddVideo(m *model.McsVideo) (err error) { //new condition cton := map[string]interface{}{ "upkeepId": m.UpkeepId, } rt := s.d.DB.Table("video").Where(cton).Updates(m) if rt.Error != nil { err = log.Error("UpdateOrAddVideo update ", rt.Error, m) return } if rt.RowsAffected > 0 { return } //add vedio if err = s.d.DB.Table("video").Create(&m).Error; err != nil { err = log.Error("UpdateOrAddVideo create ", err, m) } return } // 查询录像文件 func (s *Service) QueryVideo(m *model.McsVideo) (res []model.McsVideo, err error) { if err = s.d.DB.Table("video").Where(m).Find(&res).Error; err != nil { err = log.Error("QueryVideo query ", err, m) } return } // 设备账号列表 func (s *Service) mcsAccounts(c *gin.Context) { ma := []model.McsAccount{ { DeviceId: "apptest2", UserID: "PC", Password: "mcs8", }, } c.JSON(200, ma) }