feat: 添加 Kafka 消费者和消息处理功能

- 新增 Kafka 消费者实现,支持消息处理和错误处理。
- 实现 OffsetTracker 类,用于跟踪消息偏移量。
- 新增消息解析和数据库插入逻辑,支持从 Kafka 消息构建数据库行。
- 实现 UDP 数据包解析功能,支持不同类型的 UDP 消息。
- 新增 Redis 错误队列处理,支持错误重试机制。
- 实现 Redis 客户端和集成类,支持日志记录和心跳机制。
- 添加 Zod 验证模式,确保 Kafka 消息有效性。
- 新增日志记录和指标收集工具,支持系统监控。
- 添加 UUID 生成工具,支持唯一标识符生成。
- 编写处理器逻辑的单元测试,确保功能正确性。
- 配置 Vite 构建工具,支持 Node.js 环境下的构建。
This commit is contained in:
2026-03-14 17:33:19 +08:00
parent d62f83b4a4
commit 677dda80b9
101 changed files with 14904 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
import { describe, it, expect } from 'vitest';
import { buildRowsFromPayload } from '../src/processor/index.js';
describe('Processor Logic', () => {
const basePayload = {
HotelCode: '1085',
MAC: '00:1A:2B:3C:4D:5E',
HostNumber: '091123987456',
RoomNumber: '8888房',
EndPoint: '50.2.60.1:6543',
CurrentStatus: 'off',
CurrentTime: '2026-02-02T10:30:00Z',
UnixTime: 1770000235000,
LauncherVersion: '1.0.0'
};
it('should validate required fields', () => {
expect(() => buildRowsFromPayload({})).toThrow();
expect(() => buildRowsFromPayload({ ...basePayload, HotelCode: undefined })).toThrow();
});
it('should use current_status from payload for non-reboot data', () => {
const rows = buildRowsFromPayload({ ...basePayload, RebootReason: null });
expect(rows).toHaveLength(1);
expect(rows[0].current_status).toBe('off');
expect(rows[0].reboot_reason).toBeNull();
});
it('should override current_status to on for reboot data', () => {
const rows = buildRowsFromPayload({ ...basePayload, CurrentStatus: 'off', RebootReason: '0x01' });
expect(rows).toHaveLength(1);
expect(rows[0].current_status).toBe('on');
expect(rows[0].reboot_reason).toBe('0x01');
});
it('should keep empty optional fields as empty strings', () => {
const rows = buildRowsFromPayload({
...basePayload,
LauncherVersion: '',
RebootReason: ''
});
expect(rows[0].launcher_version).toBe('');
expect(rows[0].reboot_reason).toBe('');
});
});