45 lines
1.7 KiB
JavaScript
45 lines
1.7 KiB
JavaScript
import http from 'http';
|
|
const PORT = 17311;
|
|
|
|
async function callApi(path, options = {}) {
|
|
const method = options.method || 'GET';
|
|
const body = options.body ? JSON.stringify(options.body) : null;
|
|
const headers = body ? { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } : {};
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(`http://127.0.0.1:${PORT}${path}`, { method, headers }, (res) => {
|
|
const chunks = [];
|
|
res.on('data', c => chunks.push(c));
|
|
res.on('end', () => { try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf-8'))); } catch { resolve({}); } });
|
|
});
|
|
req.on('error', reject);
|
|
if (body) req.write(body);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
const body = {
|
|
ip: '192.168.8.241', port: 7681, httpPort: 80,
|
|
user: 'admin', pass: 'ky666888', channel: 33,
|
|
start: '2026-07-08T10:00:00', end: '2026-07-08T10:04:00',
|
|
speed: 4, outputPrefix: 'test_4min'
|
|
};
|
|
|
|
const dlRes = await callApi('/api/hikvision/download', { method: 'POST', body });
|
|
console.log('taskId:', dlRes.taskId, dlRes.status);
|
|
|
|
const start = Date.now();
|
|
for (let i = 0; i < 300; i++) {
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
const s = await callApi(`/api/hikvision/status?taskId=${dlRes.taskId}`);
|
|
const t = ((Date.now() - start) / 1000).toFixed(0);
|
|
if (s.status === 'done' || s.status === 'completed') {
|
|
console.log(`[${t}s] ✅ ${(s.result?.size/1024/1024).toFixed(1)}MB ${s.result?.output}`);
|
|
break;
|
|
}
|
|
if (s.status === 'failed' || s.status === 'timeout') {
|
|
console.log(`[${t}s] ❌ ${s.status}: ${s.message}`);
|
|
break;
|
|
}
|
|
console.log(`[${t}s] ${s.status} chunks=${s.progress?.chunks || 0}`);
|
|
}
|