完善客流数据接入

parent 42295692
......@@ -97,29 +97,30 @@ export async function processDeviceData(message) {
}
}
/** 客流触发的单条线路数据 */
interface CustomerTriggerItem {
/** 线路编号 */
triggerKey: number;
/** 线路名称 */
triggerName: string;
/** 线路UUID */
triggerUuid: string;
/** 统计对象 { in: number, out: number } */
total: any;
}
/**
* 处理接收到的客流设备数据,并存入数据库
*
* 消息示例
* 当前格式(line_trigger_data)
* {
* device_info: {
* device_mac: 'C0:BA:1F:02:98:F6',
* device_name: 'People Counter',
* device_sn: '6537F53875140008',
* firmware_version: 'V_125-LW.1.0.3-r4',
* hardware_version: 'V1.1',
* ip_address: '192.168.125.114',
* running_time: 4764787,
* wlan_mac: 'C0:BA:1F:02:98:F7'
* },
* region_trigger_data: { region_count_data: [ { region: 1, region_name: 'Region1', region_uuid: 'b63ffafc-857f-4d12-9dc7-d7fddfeb5f0f', total: { current_total: 9 } } ] },
* time_info: {
* dst_status: false,
* enable_dst: false,
* time: '2026-06-18T05:19:23-00:00',
* time_zone: 'UTC-0:00 Western European Time (WET), Greenwich Mean Time (GMT)'
* }
* device_info: { ... },
* line_trigger_data: [ { line: 1, line_name: 'Line1', line_uuid: '...', total: { in: 2, out: 2 } } ],
* time_info: { ... }
* }
*
* 旧格式(已弃用,注释保留备用):
* // region_trigger_data: { region_count_data: [ { region: 1, region_name: 'Region1', region_uuid: '...', total: { current_total: 9 } } ] }
*/
export async function customerDeviceData(message) {
try {
......@@ -152,14 +153,42 @@ export async function customerDeviceData(message) {
const timeInfo = data.time_info;
const deviceTime = timeInfo && timeInfo.time ? new Date(timeInfo.time) : new Date();
// ===== 提取客流数据 =====
const regionTriggerData = data.region_trigger_data;
if (!regionTriggerData || !regionTriggerData.region_count_data || regionTriggerData.region_count_data.length === 0) {
console.warn('消息缺少客流区域数据,跳过');
// ===== 提取客流数据(当前使用新格式 line_trigger_data) =====
let triggerItems: CustomerTriggerItem[] = [];
if (data.line_trigger_data && Array.isArray(data.line_trigger_data)) {
// 新格式:line_trigger_data,直接是数组,total: { in, out }
for (const item of data.line_trigger_data) {
triggerItems.push({
triggerKey: item.line,
triggerName: item.line_name || '',
triggerUuid: item.line_uuid || '',
total: item.total,
});
}
console.log(`[客流] 使用新格式 line_trigger_data,共 ${triggerItems.length} 条线路`);
}
/*
// ====== 旧格式(已弃用,保留备用) ======
else if (data.region_trigger_data?.region_count_data?.length > 0) {
// 旧格式:region_trigger_data.region_count_data,total: { current_total }
for (const item of data.region_trigger_data.region_count_data) {
triggerItems.push({
triggerKey: item.region,
triggerName: item.region_name || '',
triggerUuid: item.region_uuid || '',
total: item.total,
});
}
console.log(`[客流] 使用旧格式 region_trigger_data,共 ${triggerItems.length} 条区域`);
}
*/
if (triggerItems.length === 0) {
console.warn('消息缺少客流线路数据,跳过');
return;
}
const regionCountData = regionTriggerData.region_count_data;
const now = new Date();
// ===== 设备校验/新增 =====
......@@ -167,13 +196,13 @@ export async function customerDeviceData(message) {
TABLENAME.设备表, { device_id: deviceSn }
);
if (!deviceRow) {
// 设备不存在,为每个区域创建一条设备记录
for (const regionData of regionCountData) {
// 设备不存在,为每条线路创建一条设备记录
for (const item of triggerItems) {
await addData(
TABLENAME.设备表,
{
device_id: deviceSn,
region_key: regionData.region,
region_key: item.triggerKey,
device_type: '客流传感器',
device_name: deviceName,
control_params: data,
......@@ -181,32 +210,30 @@ export async function customerDeviceData(message) {
updated_at: now,
}
);
console.log(`客流设备已创建: ${deviceSn}, 区域 ${regionData.region}`);
console.log(`客流设备已创建: ${deviceSn}, 线路 ${item.triggerKey}`);
}
}
// 遍历每个区域的客流数据,逐条入库
for (const regionData of regionCountData) {
const regionUuid = regionData.region_uuid;
const regionName = regionData.region_name || '';
const regionNumber = regionData.region;
const currentTotal = regionData.total?.current_total ?? 0;
// 遍历每条线路的客流数据,逐条入库
for (const item of triggerItems) {
const inCount = item.total?.in ?? 0;
const outCount = item.total?.out ?? 0;
await addData(
TABLENAME.设备数据表,
{
device_id: deviceSn,
data: regionData.total,
data: item.total,
received_time: now,
device_time: deviceTime,
created_at: now,
}
);
console.log(`客流数据入库成功: 设备 ${deviceSn}, 区域 ${regionName || regionUuid}, 当前人数 ${currentTotal}`);
console.log(`客流数据入库成功: 设备 ${deviceSn}, ${item.triggerName || item.triggerUuid}, 进 ${inCount}${outCount}`);
}
console.log(`客流数据处理完成: 设备 ${deviceSn}, 共入库 ${regionCountData.length} 条区域数据`);
console.log(`客流数据处理完成: 设备 ${deviceSn}, 共入库 ${triggerItems.length}数据`);
} catch (error) {
console.error('客流数据处理失败:', error);
......
......@@ -181,16 +181,23 @@ function calcEnvDeviceDaily(records: any[]): { tempSum: number; tempCount: numbe
/**
* 从客流传感器数据中计算当日平均在馆人数
* current_total 为当时在场人数(瞬时值),取当日所有采集值的算术平均
* 新格式使用 data.in(进)和 data.out(出)
* // 旧格式(已弃用,保留备用):current_total 为当时在场人数(瞬时值),取当日所有采集值的算术平均
*/
function calcCustomerDeviceDaily(records: any[]): { visitorSum: number; visitorCount: number } {
let sum = 0, count = 0;
for (const rec of records) {
const d = rec.data || {};
if (d.current_total != null) {
sum += Number(d.current_total);
// ====== 新格式使用 in ======
if (d.in != null) {
sum += Number(d.in);
count++;
}
// ====== 旧格式(已弃用,保留备用) ======
// if (d.current_total != null) {
// sum += Number(d.current_total);
// count++;
// }
}
return { visitorSum: sum, visitorCount: count };
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment