Initial commit: 宁夏智慧养殖监管平台
This commit is contained in:
69
backend/create-sensor-data.js
Normal file
69
backend/create-sensor-data.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const { SensorData } = require('./models');
|
||||
const sequelize = require('./config/database');
|
||||
|
||||
async function createSensorData() {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('数据库连接成功');
|
||||
|
||||
// 创建过去24小时的温度和湿度数据
|
||||
const now = new Date();
|
||||
const sensorDataList = [];
|
||||
|
||||
// 生成过去24小时的数据,每小时一条记录
|
||||
for (let i = 23; i >= 0; i--) {
|
||||
const timestamp = new Date(now.getTime() - i * 60 * 60 * 1000);
|
||||
|
||||
// 温度数据 (20-30度之间随机)
|
||||
const temperature = 20 + Math.random() * 10;
|
||||
sensorDataList.push({
|
||||
device_id: 1,
|
||||
farm_id: 1,
|
||||
sensor_type: 'temperature',
|
||||
value: parseFloat(temperature.toFixed(1)),
|
||||
unit: '°C',
|
||||
timestamp: timestamp,
|
||||
created_at: timestamp,
|
||||
updated_at: timestamp
|
||||
});
|
||||
|
||||
// 湿度数据 (50-80%之间随机)
|
||||
const humidity = 50 + Math.random() * 30;
|
||||
sensorDataList.push({
|
||||
device_id: 1,
|
||||
farm_id: 1,
|
||||
sensor_type: 'humidity',
|
||||
value: parseFloat(humidity.toFixed(1)),
|
||||
unit: '%',
|
||||
timestamp: timestamp,
|
||||
created_at: timestamp,
|
||||
updated_at: timestamp
|
||||
});
|
||||
}
|
||||
|
||||
// 批量插入数据
|
||||
await SensorData.bulkCreate(sensorDataList);
|
||||
console.log(`成功创建 ${sensorDataList.length} 条传感器数据`);
|
||||
|
||||
// 验证数据
|
||||
const count = await SensorData.count();
|
||||
console.log(`传感器数据总数: ${count}`);
|
||||
|
||||
const temperatureCount = await SensorData.count({
|
||||
where: { sensor_type: 'temperature' }
|
||||
});
|
||||
console.log(`温度数据条数: ${temperatureCount}`);
|
||||
|
||||
const humidityCount = await SensorData.count({
|
||||
where: { sensor_type: 'humidity' }
|
||||
});
|
||||
console.log(`湿度数据条数: ${humidityCount}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('创建传感器数据失败:', error);
|
||||
} finally {
|
||||
await sequelize.close();
|
||||
}
|
||||
}
|
||||
|
||||
createSensorData();
|
||||
Reference in New Issue
Block a user