const { Client } = require('pg'); require('dotenv').config(); // 使用DATABASE_URL如果可用,否则回退到个别配置 const client = new Client( process.env.DATABASE_URL || { host: process.env.DATABASE_HOST || 'localhost', port: process.env.DATABASE_PORT || 5432, database: process.env.DATABASE_NAME || 'meteor_db', user: process.env.DATABASE_USER || 'postgres', password: process.env.DATABASE_PASSWORD || 'password', } ); const cameraDevices = [ { device_id: 'CAM-001', name: '北京站主相机', location: '北京', status: 'active', last_seen_at: new Date(Date.now() - 2 * 60 * 1000), // 2分钟前 temperature: -15.2, cooler_power: 85.0, gain: 2800, exposure_count: 1247, uptime: 168.5, firmware_version: 'v2.3.1', serial_number: 'BJ001-2024' }, { device_id: 'CAM-002', name: '东京站主相机', location: '东京', status: 'active', last_seen_at: new Date(Date.now() - 4 * 60 * 1000), // 4分钟前 temperature: -18.7, cooler_power: 92.0, gain: 3200, exposure_count: 1186, uptime: 145.2, firmware_version: 'v2.3.1', serial_number: 'TK001-2024' }, { device_id: 'CAM-003', name: '伦敦站主相机', location: '伦敦', status: 'maintenance', last_seen_at: new Date(Date.now() - 3 * 60 * 60 * 1000), // 3小时前 temperature: -12.1, cooler_power: 0.0, gain: 2400, exposure_count: 892, uptime: 98.3, firmware_version: 'v2.2.8', serial_number: 'LD001-2024' }, { device_id: 'CAM-004', name: '纽约站主相机', location: '纽约', status: 'active', last_seen_at: new Date(Date.now() - 1 * 60 * 1000), // 1分钟前 temperature: -16.8, cooler_power: 88.5, gain: 3100, exposure_count: 1584, uptime: 203.7, firmware_version: 'v2.3.1', serial_number: 'NY001-2024' }, { device_id: 'CAM-005', name: '悉尼站主相机', location: '悉尼', status: 'offline', last_seen_at: new Date(Date.now() - 24 * 60 * 60 * 1000), // 24小时前 temperature: null, cooler_power: null, gain: 2600, exposure_count: 756, uptime: 67.2, firmware_version: 'v2.2.5', serial_number: 'SY001-2024' } ]; // 生成相机历史记录数据 function generateCameraHistory(cameraId, days = 30) { const history = []; const now = new Date(); for (let i = 0; i < days; i++) { const recordTime = new Date(now - i * 24 * 60 * 60 * 1000); // 模拟不同的状态变化 let status = 'active'; if (Math.random() < 0.05) status = 'maintenance'; if (Math.random() < 0.02) status = 'offline'; history.push({ camera_device_id: cameraId, recorded_at: recordTime, status: status, temperature: -20 + Math.random() * 10, // -20到-10度 cooler_power: status === 'active' ? 80 + Math.random() * 20 : 0, // 活跃时80-100% gain: Math.floor(2000 + Math.random() * 2000), // 2000-4000 exposure_count: Math.floor(800 + Math.random() * 1000), // 800-1800 uptime: Math.floor(50 + Math.random() * 200) // 50-250小时 }); } return history; } async function seedCameraData() { try { await client.connect(); console.log('Connected to database'); // 清空现有数据 console.log('Clearing existing camera data...'); await client.query('DELETE FROM camera_history_records'); await client.query('DELETE FROM camera_devices'); // 插入相机设备数据 console.log('Inserting camera devices...'); for (const camera of cameraDevices) { const query = ` INSERT INTO camera_devices ( device_id, name, location, status, last_seen_at, temperature, cooler_power, gain, exposure_count, uptime, firmware_version, serial_number ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING id `; const values = [ camera.device_id, camera.name, camera.location, camera.status, camera.last_seen_at, camera.temperature, camera.cooler_power, camera.gain, camera.exposure_count, camera.uptime, camera.firmware_version, camera.serial_number ]; const result = await client.query(query, values); const cameraId = result.rows[0].id; console.log(`Inserted camera: ${camera.name} (ID: ${cameraId})`); // 为每个相机生成历史记录 console.log(`Generating history for camera ${camera.name}...`); const history = generateCameraHistory(cameraId); for (const record of history) { const historyQuery = ` INSERT INTO camera_history_records ( camera_device_id, recorded_at, status, temperature, cooler_power, gain, exposure_count, uptime ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) `; const historyValues = [ record.camera_device_id, record.recorded_at, record.status, record.temperature, record.cooler_power, record.gain, record.exposure_count, record.uptime ]; await client.query(historyQuery, historyValues); } console.log(`Generated ${history.length} history records for ${camera.name}`); } console.log('Camera data seeding completed successfully!'); // 显示统计信息 const deviceCount = await client.query('SELECT COUNT(*) FROM camera_devices'); const historyCount = await client.query('SELECT COUNT(*) FROM camera_history_records'); console.log(`Total camera devices: ${deviceCount.rows[0].count}`); console.log(`Total history records: ${historyCount.rows[0].count}`); } catch (error) { console.error('Error seeding camera data:', error); process.exit(1); } finally { await client.end(); console.log('Database connection closed'); } } // 如果直接运行此脚本 if (require.main === module) { seedCameraData(); } module.exports = { seedCameraData };