feat(processor): 添加循环名称自动生成的配置开关

新增环境变量 ENABLE_LOOP_NAME_AUTO_GENERATION 用于控制当缓存未命中时是否自动生成循环名称。当设置为 false 时,系统将不再生成 [类型-地址-回路] 格式的备用名称,而是直接返回 null。更新了配置文件、处理器逻辑并添加了相应的单元测试。
This commit is contained in:
2026-02-03 09:26:01 +08:00
parent 4e0f5213db
commit 339db6f95f
9 changed files with 112 additions and 5 deletions

View File

@@ -0,0 +1,47 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { buildRowsFromPayload } from '../src/processor/index.js';
import { config } from '../src/config/config.js';
describe('Feature Toggle: Loop Name Auto Generation', () => {
const basePayload = {
ts_ms: 1700000000000,
hotel_id: 1001,
room_id: '8001',
device_id: 'dev_001',
direction: '上报',
cmd_word: '0x36',
frame_id: 1,
udp_raw: '00',
sys_lock_status: 0,
report_count: 0,
fault_count: 0,
device_list: [
{ dev_type: 1, dev_addr: 10, dev_loop: 1, dev_data: 100 }
]
};
let originalConfigValue;
beforeEach(() => {
originalConfigValue = config.enableLoopNameAutoGeneration;
});
afterEach(() => {
config.enableLoopNameAutoGeneration = originalConfigValue;
});
it('should generate loop_name when flag is true', () => {
config.enableLoopNameAutoGeneration = true;
const rows = buildRowsFromPayload(basePayload);
// Expect format: [1强电继电器输出状态-10-1]
// The exact name depends on the map, but it should contain brackets and numbers
expect(rows[0].loop_name).toBeDefined();
expect(rows[0].loop_name).toMatch(/^\[1.*-10-1\]$/);
});
it('should NOT generate loop_name when flag is false', () => {
config.enableLoopNameAutoGeneration = false;
const rows = buildRowsFromPayload(basePayload);
expect(rows[0].loop_name).toBeNull();
});
});

View File

@@ -223,7 +223,7 @@ describe('Processor Logic', () => {
const rows = buildRowsFromPayload(payload);
expect(rows[0].loop_name).toBe('Main Chandelier');
// dev_type 1 is 'Dev_Host_HVout'
expect(rows[1].loop_name).toBe('[1Dev_Host_HVout-10-2]');
// dev_type 1 is '强电继电器(输出状态)'
expect(rows[1].loop_name).toBe('[1强电继电器(输出状态)-10-2]');
});
});