123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- 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)
- // start or stop mcs eq
- 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
- }
- // param has value empid, deviceid use to query mcs server
- // use param upkeepid to query video from s.d.DB
- 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
- }
- // get the last record file from mcs server
- 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
- }
- // param has value empid, upkeepid, deviceid,createdAt
- 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)
- }
|