124 lines
3.9 KiB
JavaScript
124 lines
3.9 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
require('dotenv').config();
|
|
|
|
async function insertEnvironmentData() {
|
|
let connection;
|
|
|
|
try {
|
|
// 创建数据库连接
|
|
connection = await mysql.createConnection({
|
|
host: process.env.DB_HOST,
|
|
port: process.env.DB_PORT,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME
|
|
});
|
|
|
|
console.log('数据库连接成功');
|
|
|
|
// 创建环境监测时刻表
|
|
const createTableSQL = `
|
|
CREATE TABLE IF NOT EXISTS environment_schedules (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
farm_id INT NOT NULL,
|
|
device_id VARCHAR(50),
|
|
schedule_time TIME NOT NULL,
|
|
temperature DECIMAL(5,2),
|
|
humidity DECIMAL(5,2),
|
|
monitoring_date DATE NOT NULL,
|
|
status ENUM('active', 'inactive', 'maintenance') DEFAULT 'active',
|
|
notes TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
INDEX idx_farm_date (farm_id, monitoring_date),
|
|
INDEX idx_schedule_time (schedule_time)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
`;
|
|
|
|
await connection.execute(createTableSQL);
|
|
console.log('环境监测时刻表创建成功');
|
|
|
|
// 生成过去7天的数据
|
|
const scheduleData = [];
|
|
const currentDate = new Date();
|
|
|
|
// 每天的监测时间点
|
|
const scheduleTimes = ['06:00:00', '12:00:00', '18:00:00', '22:00:00'];
|
|
|
|
for (let day = 0; day < 7; day++) {
|
|
const date = new Date(currentDate);
|
|
date.setDate(date.getDate() - day);
|
|
const dateStr = date.toISOString().split('T')[0];
|
|
|
|
for (const time of scheduleTimes) {
|
|
// 农场1的数据
|
|
scheduleData.push([
|
|
1, // farm_id
|
|
'TEMP_001', // device_id
|
|
time,
|
|
(20 + Math.random() * 15).toFixed(2), // 温度 20-35°C
|
|
(40 + Math.random() * 40).toFixed(2), // 湿度 40-80%
|
|
dateStr,
|
|
'active',
|
|
`农场1 ${dateStr} ${time} 环境监测数据`
|
|
]);
|
|
|
|
// 农场2的数据
|
|
scheduleData.push([
|
|
2, // farm_id
|
|
'TEMP_002', // device_id
|
|
time,
|
|
(18 + Math.random() * 17).toFixed(2), // 温度 18-35°C
|
|
(35 + Math.random() * 45).toFixed(2), // 湿度 35-80%
|
|
dateStr,
|
|
'active',
|
|
`农场2 ${dateStr} ${time} 环境监测数据`
|
|
]);
|
|
}
|
|
}
|
|
|
|
// 批量插入数据
|
|
const insertSQL = `
|
|
INSERT INTO environment_schedules
|
|
(farm_id, device_id, schedule_time, temperature, humidity, monitoring_date, status, notes)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
`;
|
|
|
|
for (const data of scheduleData) {
|
|
await connection.execute(insertSQL, data);
|
|
}
|
|
|
|
console.log(`成功插入 ${scheduleData.length} 条环境监测数据`);
|
|
|
|
// 验证插入的数据
|
|
const [rows] = await connection.execute(
|
|
'SELECT COUNT(*) as count FROM environment_schedules'
|
|
);
|
|
console.log(`环境监测表总记录数: ${rows[0].count}`);
|
|
|
|
// 显示最近的几条记录
|
|
const [recentRows] = await connection.execute(
|
|
`SELECT farm_id, device_id, schedule_time, temperature, humidity,
|
|
monitoring_date, status
|
|
FROM environment_schedules
|
|
ORDER BY monitoring_date DESC, schedule_time DESC
|
|
LIMIT 5`
|
|
);
|
|
|
|
console.log('\n最近的环境监测记录:');
|
|
recentRows.forEach(row => {
|
|
console.log(`农场${row.farm_id} | ${row.device_id} | ${row.monitoring_date} ${row.schedule_time} | 温度:${row.temperature}°C | 湿度:${row.humidity}% | 状态:${row.status}`);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('操作失败:', error.message);
|
|
} finally {
|
|
if (connection) {
|
|
await connection.end();
|
|
console.log('\n数据库连接已关闭');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 运行脚本
|
|
insertEnvironmentData(); |