Skip to content

Commit bcb434f

Browse files
committed
feat(rpi-server): キャプションイベントのログ出力を改善
問題: キャプションが未マップイベント警告、テキスト内容が出ない 改善: - processor.py: caption専用ログ出力、MQTT送信スキップ - event_mapper.py: caption警告を抑制 - ログ形式: キャプション: t=36, text=テキスト内容 テスト: test_caption_logging.py 合格
1 parent c4434a6 commit bcb434f

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

hardware/rpi_server/src/mqtt/event_mapper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def map_event_to_mqtt(
267267
# action == "start" or "shot"
268268
mqtt_commands = cls.EVENT_MAP.get(key, [])
269269

270-
if not mqtt_commands:
270+
# キャプションイベントの場合は警告を出さない(MQTTコマンドなし)
271+
if not mqtt_commands and action != "caption":
271272
logger.warning(
272273
f"未マップイベント: effect={effect}, mode={mode}, action={action}"
273274
)

hardware/rpi_server/src/timeline/processor.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,22 @@ def _execute_event(self, event: Dict) -> None:
184184
"""
185185
try:
186186
event_time = event.get("t", 0)
187-
effect = event.get("effect", "unknown")
187+
effect = event.get("effect", "")
188188
mode = event.get("mode", "")
189189
action = event.get("action", "start")
190+
caption_text = event.get("text", "")
191+
192+
# キャプションイベントの場合は専用ログ出力
193+
if action == "caption":
194+
logger.info(
195+
f"💬 キャプション: t={event_time}, text=\"{caption_text}\""
196+
)
197+
# キャプションはMQTTコマンドに変換しないのでここで終了
198+
return
199+
200+
# effectがない場合はunknownとする
201+
if not effect:
202+
effect = "unknown"
190203

191204
# クールダウンチェック
192205
if effect in self.cooldown_durations:
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"""
2+
キャプションイベントのログ出力テスト
3+
"""
4+
5+
import sys
6+
import logging
7+
from pathlib import Path
8+
9+
# プロジェクトルートをパスに追加
10+
sys.path.insert(0, str(Path(__file__).parent))
11+
12+
from src.timeline.processor import TimelineProcessor
13+
from src.mqtt.event_mapper import EventToMQTTMapper
14+
15+
# ログ設定
16+
logging.basicConfig(
17+
level=logging.INFO,
18+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
19+
)
20+
21+
22+
def test_caption_event_logging():
23+
"""キャプションイベントのログ出力テスト"""
24+
print("=" * 70)
25+
print("キャプションイベントのログ出力テスト")
26+
print("=" * 70)
27+
28+
executed_events = []
29+
30+
def callback(event):
31+
executed_events.append(event)
32+
33+
processor = TimelineProcessor(on_event_callback=callback)
34+
35+
# タイムラインロード(キャプション + 水しぶき)
36+
processor.load_timeline({
37+
"events": [
38+
{
39+
"t": 0,
40+
"action": "caption",
41+
"text": "オープニングシーン。静寂の中、物語が始まる。"
42+
},
43+
{
44+
"t": 5.0,
45+
"effect": "vibration",
46+
"mode": "down_weak",
47+
"action": "start"
48+
},
49+
{
50+
"t": 10.0,
51+
"action": "caption",
52+
"text": "カメラが川面を捉える。穏やかな水の流れ。"
53+
},
54+
{
55+
"t": 15.0,
56+
"effect": "water",
57+
"mode": "burst",
58+
"action": "shot"
59+
},
60+
{
61+
"t": 36.0,
62+
"action": "caption",
63+
"text": "球体が川面に激突し、巨大な水しぶきを上げる。激しい衝撃音が聞こえてきそうだ。"
64+
},
65+
{
66+
"t": 36.0,
67+
"effect": "water",
68+
"mode": "burst",
69+
"action": "shot"
70+
}
71+
]
72+
})
73+
74+
processor.start_playback()
75+
76+
print("\n--- 時刻0秒: キャプションイベント ---")
77+
processor.update_current_time(0.0)
78+
79+
print("\n--- 時刻5秒: 振動イベント ---")
80+
processor.update_current_time(5.0)
81+
82+
print("\n--- 時刻10秒: キャプションイベント ---")
83+
processor.update_current_time(10.0)
84+
85+
print("\n--- 時刻15秒: 水しぶきイベント ---")
86+
processor.update_current_time(15.0)
87+
88+
print("\n--- 時刻36秒: キャプション + 水しぶきイベント ---")
89+
processor.update_current_time(36.0)
90+
91+
# キャプションはコールバックされない(MQTTコマンドなし)
92+
print(f"\n実行されたイベント数: {len(executed_events)}")
93+
print("期待値: 3イベント(振動1 + 水2)")
94+
95+
assert len(executed_events) == 3, f"Expected 3 events, got {len(executed_events)}"
96+
print("✅ キャプションはコールバックされず、エフェクトのみ実行される")
97+
98+
print("\n✅ テスト合格\n")
99+
100+
101+
def test_caption_event_mapper():
102+
"""キャプションイベントのマッピングテスト(警告が出ないこと)"""
103+
print("=" * 70)
104+
print("キャプションイベントのマッピングテスト")
105+
print("=" * 70)
106+
107+
# キャプションイベント
108+
caption_event = {
109+
"t": 10,
110+
"action": "caption",
111+
"text": "テストキャプション"
112+
}
113+
114+
print("\n--- キャプションイベントを処理 ---")
115+
mqtt_commands = EventToMQTTMapper.process_timeline_event(caption_event)
116+
117+
print(f"MQTTコマンド数: {len(mqtt_commands)}")
118+
print("期待値: 0個(キャプションはMQTTコマンドなし)")
119+
120+
assert len(mqtt_commands) == 0, f"Expected 0 MQTT commands, got {len(mqtt_commands)}"
121+
print("✅ キャプションは警告なくスキップされる")
122+
123+
print("\n✅ テスト合格\n")
124+
125+
126+
if __name__ == "__main__":
127+
try:
128+
test_caption_event_logging()
129+
test_caption_event_mapper()
130+
131+
print("=" * 70)
132+
print("🎉 全テスト合格!キャプション表示が改善されました")
133+
print("=" * 70)
134+
print("\n期待されるログ出力:")
135+
print("💬 キャプション: t=0, text=\"オープニングシーン。静寂の中、物語が始まる。\"")
136+
print("💬 キャプション: t=10, text=\"カメラが川面を捉える。穏やかな水の流れ。\"")
137+
print("💬 キャプション: t=36, text=\"球体が川面に激突し、巨大な水しぶきを上げる。激しい衝撃音が聞こえてきそうだ。\"")
138+
139+
except AssertionError as e:
140+
print(f"\n❌ テスト失敗: {e}")
141+
sys.exit(1)
142+
except Exception as e:
143+
print(f"\n❌ エラー: {e}")
144+
import traceback
145+
traceback.print_exc()
146+
sys.exit(1)

0 commit comments

Comments
 (0)