Initial commit: 宁夏智慧养殖监管平台
This commit is contained in:
123
backend/models/Device.js
Normal file
123
backend/models/Device.js
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Device 模型定义
|
||||
* @file Device.js
|
||||
* @description 定义设备模型,用于数据库操作
|
||||
*/
|
||||
const { DataTypes } = require('sequelize');
|
||||
const BaseModel = require('./BaseModel');
|
||||
const { sequelize } = require('../config/database-simple');
|
||||
|
||||
/**
|
||||
* 设备模型
|
||||
* @typedef {Object} Device
|
||||
* @property {number} id - 设备唯一标识
|
||||
* @property {string} type - 设备类型
|
||||
* @property {string} status - 设备状态
|
||||
* @property {number} farmId - 所属养殖场ID
|
||||
* @property {Date} created_at - 创建时间
|
||||
* @property {Date} updated_at - 更新时间
|
||||
*/
|
||||
class Device extends BaseModel {
|
||||
/**
|
||||
* 获取设备所属的养殖场
|
||||
* @returns {Promise<Object>} 养殖场信息
|
||||
*/
|
||||
async getFarm() {
|
||||
return await this.getFarm();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备的所有预警
|
||||
* @returns {Promise<Array>} 预警列表
|
||||
*/
|
||||
async getAlerts() {
|
||||
return await this.getAlerts();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备状态
|
||||
* @param {String} status 新状态
|
||||
* @returns {Promise<Boolean>} 更新结果
|
||||
*/
|
||||
async updateStatus(status) {
|
||||
try {
|
||||
this.status = status;
|
||||
await this.save();
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('更新设备状态失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录设备维护
|
||||
* @returns {Promise<Boolean>} 记录结果
|
||||
*/
|
||||
async recordMaintenance() {
|
||||
try {
|
||||
this.last_maintenance = new Date();
|
||||
this.status = 'online';
|
||||
await this.save();
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('记录设备维护失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化Device模型
|
||||
Device.init({
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING(100),
|
||||
allowNull: false
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING(50),
|
||||
allowNull: false
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('online', 'offline', 'maintenance'),
|
||||
defaultValue: 'offline'
|
||||
},
|
||||
farm_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: 'farms',
|
||||
key: 'id'
|
||||
}
|
||||
},
|
||||
last_maintenance: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
installation_date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
metrics: {
|
||||
type: DataTypes.JSON,
|
||||
allowNull: true,
|
||||
defaultValue: {}
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
tableName: 'devices',
|
||||
modelName: 'Device',
|
||||
timestamps: true,
|
||||
createdAt: 'created_at',
|
||||
updatedAt: 'updated_at'
|
||||
});
|
||||
|
||||
/**
|
||||
* 导出设备模型
|
||||
* @exports Device
|
||||
*/
|
||||
module.exports = Device;
|
||||
Reference in New Issue
Block a user