112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
/**
|
|
* 测试智能项圈预警数据
|
|
* @file test-collar-alert-data.js
|
|
* @description 测试智能项圈预警API返回的数据
|
|
*/
|
|
|
|
const axios = require('axios');
|
|
|
|
const BASE_URL = 'http://localhost:5350/api/smart-alerts/public';
|
|
|
|
async function testCollarAlertData() {
|
|
console.log('🔍 测试智能项圈预警数据...\n');
|
|
|
|
try {
|
|
// 1. 测试获取预警列表
|
|
console.log('1. 获取预警列表数据...');
|
|
const listResponse = await axios.get(`${BASE_URL}/collar`, {
|
|
params: {
|
|
page: 1,
|
|
limit: 10
|
|
}
|
|
});
|
|
|
|
console.log('API响应状态:', listResponse.status);
|
|
console.log('API响应数据:', JSON.stringify(listResponse.data, null, 2));
|
|
|
|
if (listResponse.data.success) {
|
|
const data = listResponse.data.data || [];
|
|
console.log(`\n数据条数: ${data.length}`);
|
|
|
|
if (data.length > 0) {
|
|
console.log('\n第一条数据示例:');
|
|
console.log(JSON.stringify(data[0], null, 2));
|
|
|
|
// 测试判断函数
|
|
console.log('\n测试预警判断逻辑:');
|
|
const testRecord = data[0];
|
|
const alertType = determineAlertType(testRecord);
|
|
console.log('判断结果:', alertType);
|
|
|
|
// 显示各字段值
|
|
console.log('\n字段值检查:');
|
|
console.log('battery:', testRecord.battery, typeof testRecord.battery);
|
|
console.log('temperature:', testRecord.temperature, typeof testRecord.temperature);
|
|
console.log('is_connect:', testRecord.is_connect, typeof testRecord.is_connect);
|
|
console.log('bandge_status:', testRecord.bandge_status, typeof testRecord.bandge_status);
|
|
console.log('steps:', testRecord.steps, typeof testRecord.steps);
|
|
console.log('y_steps:', testRecord.y_steps, typeof testRecord.y_steps);
|
|
} else {
|
|
console.log('⚠️ 没有数据返回');
|
|
}
|
|
} else {
|
|
console.log('❌ API调用失败:', listResponse.data.message);
|
|
}
|
|
|
|
// 2. 测试获取统计数据
|
|
console.log('\n2. 获取统计数据...');
|
|
const statsResponse = await axios.get(`${BASE_URL}/collar/stats`);
|
|
console.log('统计数据:', JSON.stringify(statsResponse.data, null, 2));
|
|
|
|
} catch (error) {
|
|
console.error('❌ 测试失败:', error.message);
|
|
if (error.response) {
|
|
console.error('响应状态:', error.response.status);
|
|
console.error('响应数据:', error.response.data);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 判断预警类型函数
|
|
function determineAlertType(record) {
|
|
const alerts = []
|
|
|
|
// 检查电量预警
|
|
if (record.battery !== undefined && record.battery !== null && record.battery < 20) {
|
|
alerts.push('battery')
|
|
}
|
|
|
|
// 检查脱落预警 (bandge_status为0)
|
|
if (record.bandge_status !== undefined && record.bandge_status !== null && record.bandge_status === 0) {
|
|
alerts.push('wear')
|
|
}
|
|
|
|
// 检查离线预警 (is_connect为0)
|
|
if (record.is_connect !== undefined && record.is_connect !== null && record.is_connect === 0) {
|
|
alerts.push('offline')
|
|
}
|
|
|
|
// 检查温度预警
|
|
if (record.temperature !== undefined && record.temperature !== null) {
|
|
if (record.temperature < 20) {
|
|
alerts.push('temperature_low')
|
|
} else if (record.temperature > 40) {
|
|
alerts.push('temperature_high')
|
|
}
|
|
}
|
|
|
|
// 检查运动异常预警 (steps - y_steps为0)
|
|
if (record.steps !== undefined && record.y_steps !== undefined &&
|
|
record.steps !== null && record.y_steps !== null) {
|
|
const movementDiff = record.steps - record.y_steps
|
|
if (movementDiff === 0) {
|
|
alerts.push('movement')
|
|
}
|
|
}
|
|
|
|
return alerts.length > 0 ? alerts[0] : null
|
|
}
|
|
|
|
// 运行测试
|
|
testCollarAlertData().catch(console.error);
|