index.js
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var express = require('express');
var router = express.Router();
const { spawn } = require('child_process');
const fs = require("fs");
const method = require("../config/method")
const config = require("../config/config")
const { GETCLASSURL, GETCLASSURLPARAMETER, PROJECTCATALOG, PROJECTWINCATALOG, BACKMEDIACONFIG } = config
const { YesterdayTime, getRequestClassIds } = method
const { startTime, endTime, ymd } = YesterdayTime()
let siteIds = GETCLASSURLPARAMETER.siteId
let classid = []
let parentData = []
var classobj = {};
let className = ""
let maxMedia = 2
/**
*
* @param {*} id 课堂id
*/
class MediaCreat {
constructor() { }
// 取出所有数据
async allData() {
let page = 1
className = siteIds.shift()
let result = await getRequestClassIds(GETCLASSURL, className, GETCLASSURLPARAMETER.key, startTime, endTime, page)
for (let j = 0; j < result.data.data.list.length; j++) {
classid.push(result.data.data.list[j])
}
const { siteId, list, totalPage } = result.data.data
for (let i = page += 1; i <= totalPage; i++) {
let result = await getRequestClassIds(GETCLASSURL, className, GETCLASSURLPARAMETER.key, startTime, endTime, i)
for (let j = 0; j < result.data.data.list.length; j++) {
classid.push(result.data.data.list[j])
}
}
classid = classid.reduce(function (item, next) {
classobj[next.classId] ? '' : classobj[next.classId] = true && item.push(next);
return item;
}, []);
parentData = JSON.parse(JSON.stringify(classid))
return true
}
// 取出不同的值
async notResult(data1, data2) {
new Promise((resolve, reject) => {
let data = []
for (let i = 0; i < data2.length; i++) {
// for(let j=0;j<data1.length;j++){
if (data1.indexOf(data2[i].classId+".mp4") == -1) {
console.log("不存在:"+data2[i])
data.push(data2[i])
}
}
console.log("data",data)
return resolve(data)
})
}
async mediaCreat(id, siteId) {
let mediaDir = PROJECTCATALOG + "/media/"
let classDir = PROJECTCATALOG + "/media/" + siteId
let ymdDir = PROJECTCATALOG + "/media/" + siteId + "/" + ymd
if (!fs.existsSync(mediaDir)) {
fs.mkdirSync(mediaDir);
}
if (!fs.existsSync(classDir)) {
fs.mkdirSync(classDir);
}
if (!fs.existsSync(ymdDir)) {
fs.mkdirSync(ymdDir);
}
let url = `web_capture_c -o=../media/${siteId}/${ymd}/${id}.mp4 -u="${BACKMEDIACONFIG.url}?classId=${id}&userId=${BACKMEDIACONFIG.userId}&userName=${BACKMEDIACONFIG.userName}&userRole=${BACKMEDIACONFIG.userRole}&portalIP=${BACKMEDIACONFIG.portalIP}&portalPort=${BACKMEDIACONFIG.portalPort}&channels=${BACKMEDIACONFIG.channels}&playRecord=${BACKMEDIACONFIG.playRecord}" -d=${BACKMEDIACONFIG.d} -s=${BACKMEDIACONFIG.s} -fa=${BACKMEDIACONFIG.fa} -k=${BACKMEDIACONFIG}`
var workerProcess = spawn(url, { cwd: PROJECTWINCATALOG, shell: true })
// workerProcess.stdout.on('data', async function (data) {
// });
workerProcess.on('close', async function (code) {
if (code == 0) {
if (classid.length) {
let shiftData = classid.shift()
new MediaCreat().mediaCreat(shiftData['classId'], shiftData['siteId'])
} else {
// 判断是否有缺录
// let files = fs.readdirSync(ymdDir);
// if (files.length != parentData.length) {
// let notResult = await new MediaCreat().notResult(files,parentData)
// }
if (siteIds.length) {
// 如果有多家需要录制,继续录制下一家
let isAppend = await new MediaCreat().allData()
if (isAppend) {
let shiftData = classid.shift()
new MediaCreat().mediaCreat(shiftData['classId'], shiftData['siteId'])
}
}
}
} else {
new MediaCreat().mediaCreat(id, siteId)
}
});
}
}
router.get('/', async function (req, res, next) {
let isAppend = await new MediaCreat().allData()
if (isAppend) {
if (classid.length) {
for (let i = 0; i < maxMedia; i++) {
let shiftData = classid.shift()
if (shiftData) {
new MediaCreat().mediaCreat(shiftData['classId'], shiftData['siteId'])
} else {
return false
}
}
}
}
res.render('index', { title: 'Express' });
});
module.exports = router;