# test_latency.py
import pymysql
import time
from datetime import datetime

def inject_mock_data():
    # 建立連線
    db = pymysql.connect(
        host='localhost',
        user='webuser',
        password='U1224006',
        database='mmwave_v3',
        charset='utf8mb4'
    )
    
    try:
        with db.cursor() as cursor:
            # 準備假資料 (模擬雷達判斷出 'walk')
            sql = """
                INSERT INTO `activity_logs` (`device_id`, `x_coord`, `y_coord`, `activity_type`, `confidence`, `record_time`) 
                VALUES (1, NULL, NULL, 'sit', 0.59, CONVERT_TZ(NOW(), @@session.time_zone, '+08:00') );
            """
            
            # 記錄寫入前一刻的精確時間
            start_time = time.time()
            start_dt = datetime.fromtimestamp(start_time).strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
            
            cursor.execute(sql)
            db.commit()
            
            print(f"✅ [測試] 假資料已成功寫入資料庫！")
            print(f"⏱️ 寫入精確時間 (T0): {start_dt}")
            print(f"👉 請立刻切換到網頁查看 F12 開發者工具的 Console 輸出！")
            
    except Exception as e:
        print(f"寫入失敗: {e}")
    finally:
        db.close()

if __name__ == '__main__':
    inject_mock_data()