import random
import time

def generate_radar_data_lines(count=100):
    """生成模拟雷达数据行"""
    lines = []
    
    for i in range(count):
        # 生成8个随机数值字段
        # 字段含义参考代码: presence, heart_rate, breath_rate, motion, rssi, heartbeat_waveform, breathing_waveform, raw_signal
        presence = random.choice([0, 1])  # 0=无人, 1=有人
        heart_rate = random.uniform(0, 200) if presence else 0  # 心率 (bpm)
        breath_rate = random.uniform(0, 60) if presence else 0   # 呼吸率 (bpm)
        motion = random.choice([0, 1]) if presence else 0       # 运动状态
        rssi = random.randint(-100, -30)                       # 信号强度
        heartbeat_waveform = random.randint(-1000, 1000)       # 心跳波形
        breathing_waveform = random.randint(-1000, 1000)       # 呼吸波形
        raw_signal = random.randint(-100, 100)                 # 原始信号
        
        # 格式化为HBR01数据行
        line = f"HBR01:{presence},{heart_rate:.0f},{breath_rate:.0f},{motion},{rssi},{heartbeat_waveform},{breathing_waveform},{raw_signal}"
        lines.append(line)
    
    return lines

def save_to_file(lines, filename="generated_sensor_data.txt"):
    """保存数据到文件"""
    with open(filename, "w", encoding="utf-8") as f:
        for line in lines:
            f.write(line + "\n")
    print(f"已生成 {len(lines)} 行雷达数据并保存到 {filename}")

def main():
    # 生成1000行测试数据
    lines = generate_radar_data_lines(1000)
    save_to_file(lines, "generated_sensor_data.txt")
    
    # 显示前10行作为示例
    print("\n前10行数据示例:")
    for i, line in enumerate(lines[:10]):
        print(f"{i+1:2d}: {line}")

if __name__ == "__main__":
    main()