Files
nxxmdata/backend/models/IotJbqServer.js

311 lines
6.3 KiB
JavaScript
Raw Normal View History

2025-09-12 20:08:42 +08:00
/**
* 智能主机设备模型
* @file IotJbqServer.js
* @description 智能主机设备数据模型对应iot_jbq_server表
*/
const { DataTypes, Model } = require('sequelize');
const { sequelize } = require('../config/database-simple');
class IotJbqServer extends Model {
/**
* 获取设备状态文本
*/
getStatusText() {
const statusMap = {
0: '离线',
1: '在线',
2: '报警',
3: '维护'
};
return statusMap[this.state] || '未知';
}
/**
* 获取设备状态颜色
*/
getStatusColor() {
const colorMap = {
0: 'red', // 离线
1: 'green', // 在线
2: 'orange', // 报警
3: 'blue' // 维护
};
return colorMap[this.state] || 'default';
}
/**
* 获取GPS状态文本
*/
getGpsStatusText() {
const gpsMap = {
'A': '已定位',
'V': '未定位'
};
return gpsMap[this.gps_state] || '未知';
}
/**
* 获取GPS状态颜色
*/
getGpsStatusColor() {
const colorMap = {
'A': 'green', // 已定位
'V': 'red' // 未定位
};
return colorMap[this.gps_state] || 'default';
}
/**
* 获取电池电量百分比
*/
getBatteryPercent() {
const voltage = parseFloat(this.voltage) || 0;
// 假设电池电压范围是3.0V-4.2V
if (voltage >= 4.2) return 100;
if (voltage <= 3.0) return 0;
return Math.round(((voltage - 3.0) / 1.2) * 100);
}
/**
* 获取温度数值
*/
getTemperatureValue() {
return parseFloat(this.temperature) || 0;
}
/**
* 获取信号强度文本
*/
getSignalText() {
const signal = parseInt(this.signa) || 0;
if (signal >= 20) return '强';
if (signal >= 10) return '中';
if (signal >= 5) return '弱';
return '无信号';
}
/**
* 获取信号强度颜色
*/
getSignalColor() {
const signal = parseInt(this.signa) || 0;
if (signal >= 20) return 'green';
if (signal >= 10) return 'orange';
if (signal >= 5) return 'red';
return 'red';
}
/**
* 判断是否低电量
*/
isLowBattery() {
return this.getBatteryPercent() < 20;
}
/**
* 判断是否需要维护
*/
needsMaintenance() {
return this.state === 3 || this.isLowBattery() || this.gps_state === 'V';
}
/**
* 获取最后更新时间
*/
getLastUpdateTime() {
if (this.uptime) {
const date = new Date(this.uptime * 1000);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).replace(/\//g, '-');
}
return '未知';
}
/**
* 获取设备编号使用sid字段
*/
getDeviceNumber() {
return this.sid || '无编号';
}
/**
* 判断设备是否联网
* 通过 simId 字段是否为空来判断联网状态
*/
isOnline() {
return this.simId && this.simId.trim() !== '';
}
/**
* 获取联网状态文本
*/
getNetworkStatusText() {
return this.isOnline() ? '已联网' : '未联网';
}
/**
* 获取联网状态颜色
*/
getNetworkStatusColor() {
return this.isOnline() ? 'green' : 'red';
}
}
// 初始化模型
IotJbqServer.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
comment: '主机设备ID'
},
org_id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
comment: '组织ID'
},
title: {
type: DataTypes.STRING(50),
allowNull: false,
defaultValue: '',
comment: '设备标题'
},
uid: {
type: DataTypes.BIGINT,
allowNull: false,
comment: '用户ID'
},
time: {
type: DataTypes.INTEGER,
allowNull: false,
comment: '设备时间戳'
},
uptime: {
type: DataTypes.INTEGER,
allowNull: false,
comment: '上传时间戳'
},
distribute_time: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0,
comment: '分发时间'
},
state: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '设备状态'
},
sid: {
type: DataTypes.STRING(30),
allowNull: false,
comment: '设备序列号'
},
gps_state: {
type: DataTypes.STRING(5),
allowNull: false,
comment: 'GPS状态'
},
lat: {
type: DataTypes.STRING(100),
allowNull: false,
comment: '纬度'
},
lon: {
type: DataTypes.STRING(100),
allowNull: false,
comment: '经度'
},
signa: {
type: DataTypes.STRING(200),
allowNull: false,
comment: '信号强度'
},
voltage: {
type: DataTypes.STRING(10),
allowNull: false,
comment: '电池电压'
},
temperature: {
type: DataTypes.STRING(10),
allowNull: false,
comment: '设备温度'
},
ver: {
type: DataTypes.STRING(100),
allowNull: false,
comment: '固件版本'
},
macsid: {
type: DataTypes.BIGINT,
allowNull: true,
comment: 'MAC序列号'
},
ctwing: {
type: DataTypes.TEXT,
allowNull: true,
comment: 'CTWing信息'
},
bank_lanwei: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '栏位编号'
},
bank_house: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '栏舍编号'
},
bank_item_id: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '栏位项目ID'
},
fence_id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '围栏ID'
},
source_id: {
type: DataTypes.BIGINT,
allowNull: true,
comment: '数据源ID'
},
simId: {
type: DataTypes.STRING(50),
allowNull: true,
comment: 'SIM卡ID用于判断联网状态'
}
}, {
sequelize,
modelName: 'IotJbqServer',
tableName: 'iot_jbq_server',
timestamps: false, // 该表没有created_at和updated_at字段
indexes: [
{
fields: ['uid']
},
{
fields: ['sid']
},
{
fields: ['state']
},
{
fields: ['fence_id']
},
{
fields: ['source_id']
}
]
});
module.exports = IotJbqServer;