forked from Zive-IT-2025/Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
156 lines (134 loc) · 5.64 KB
/
test_api.py
File metadata and controls
156 lines (134 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Simple test script to verify the Smart Lighting Control system
"""
import requests
import time
import json
BASE_URL = "http://localhost:8000"
def print_section(title):
"""Print a section header"""
print("\n" + "="*60)
print(f" {title}")
print("="*60)
def test_api():
"""Test the API endpoints"""
print_section("Testing Smart Lighting Control System")
# Test 1: Check if API is running
print("\n1. Testing API connection...")
try:
response = requests.get(f"{BASE_URL}/")
print(f" ✓ API is running!")
print(f" Response: {json.dumps(response.json(), indent=2)}")
except Exception as e:
print(f" ✗ API connection failed: {e}")
print(" Make sure the server is running: python main.py")
return
# Test 2: Check health
print("\n2. Checking system health...")
try:
response = requests.get(f"{BASE_URL}/health")
health = response.json()
print(f" Status: {health['status']}")
print(f" Camera: {'✓' if health['camera'] else '✗'}")
print(f" Detector: {'✓' if health['detector'] else '✗'}")
print(f" Light Controller: {'✓' if health['light_controller'] else '✗'}")
except Exception as e:
print(f" ✗ Health check failed: {e}")
# Test 3: Get initial status
print("\n3. Getting initial system status...")
try:
response = requests.get(f"{BASE_URL}/status")
status = response.json()
print(f" Processing: {'Running' if status['is_running'] else 'Stopped'}")
print(f" Light Status: {status['lights']['state']}")
print(f" Light Brightness: {status['lights']['current_brightness']}%")
except Exception as e:
print(f" ✗ Status check failed: {e}")
# Test 4: Start video processing
print("\n4. Starting video processing...")
try:
response = requests.post(f"{BASE_URL}/start")
result = response.json()
print(f" ✓ {result['message']}")
time.sleep(2) # Give it time to start
except Exception as e:
print(f" Note: {e}")
# Test 5: Monitor for a few seconds
print("\n5. Monitoring detections for 10 seconds...")
print(" (Watch the main terminal for detection logs)")
for i in range(10):
try:
response = requests.get(f"{BASE_URL}/status")
status = response.json()
stats = status['stats']
lights = status['lights']
print(f" [{i+1}/10] "
f"Frames: {stats['frames_processed']} | "
f"Detections: {stats['trigger_detections']} | "
f"Light: {lights['state']} ({lights['current_brightness']}%) | "
f"FPS: {stats['fps']:.1f}")
time.sleep(1)
except Exception as e:
print(f" ✗ Monitoring error: {e}")
break
# Test 6: Get detection history
print("\n6. Getting recent detection history...")
try:
response = requests.get(f"{BASE_URL}/detections/history?limit=5")
history = response.json()
print(f" Total detections logged: {history['total_count']}")
print(f" Recent detections:")
for detection in history['history'][-5:]:
objects = ", ".join([f"{obj['class']}({obj['confidence']:.2f})"
for obj in detection['objects']])
print(f" - {detection['timestamp']}: {objects}")
except Exception as e:
print(f" ✗ History check failed: {e}")
# Test 7: Get light status
print("\n7. Checking light controller status...")
try:
response = requests.get(f"{BASE_URL}/lights/status")
light_status = response.json()
print(f" Mode: {light_status['mode']}")
print(f" State: {light_status['state']}")
print(f" Brightness: {light_status['current_brightness']}%")
print(f" Last Update: {time.ctime(light_status['last_update'])}")
except Exception as e:
print(f" ✗ Light status check failed: {e}")
# Test 8: Manual light control
print("\n8. Testing manual light control...")
try:
# Turn on to 50%
response = requests.post(f"{BASE_URL}/lights/manual",
json={"brightness": 50})
result = response.json()
print(f" ✓ Set brightness to 50%")
time.sleep(1)
# Turn off
response = requests.post(f"{BASE_URL}/lights/manual",
json={"brightness": 0})
result = response.json()
print(f" ✓ Turned lights off")
except Exception as e:
print(f" ✗ Manual control failed: {e}")
# Test 9: Camera info
print("\n9. Camera information...")
try:
response = requests.get(f"{BASE_URL}/status")
status = response.json()
camera = status['camera']
print(f" Source: {camera['source']}")
print(f" Resolution: {camera['resolution'][0]}x{camera['resolution'][1]}")
print(f" FPS: {camera['fps']:.1f}")
print(f" Frames captured: {camera['frame_count']}")
except Exception as e:
print(f" ✗ Camera info failed: {e}")
print_section("Testing Complete!")
print("\n✓ The system is working correctly!")
print("\nNext steps:")
print(" 1. Open http://localhost:8000/docs in your browser")
print(" 2. Open http://localhost:8000/stream to see live video")
print(" 3. Configure your own camera in config.yaml")
print(" 4. Connect real lights by changing 'mode' in config.yaml\n")
if __name__ == "__main__":
test_api()