176 lines
6.8 KiB
Python
176 lines
6.8 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
ESP32云端报警上报功能最终诊断
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
import time
|
||
import threading
|
||
from datetime import datetime
|
||
|
||
def test_esp32_basic_functions():
|
||
"""测试ESP32基本功能"""
|
||
print("🔍 测试ESP32基本功能...")
|
||
|
||
try:
|
||
# 测试状态API
|
||
response = requests.get("http://192.168.1.3:80/api/status", timeout=5)
|
||
if response.status_code == 200:
|
||
print("✅ ESP32状态API正常")
|
||
status = response.json()
|
||
print(f" 设备: {status.get('data', {}).get('device', 'Unknown')}")
|
||
print(f" 运行时间: {status.get('data', {}).get('uptime', 0)}秒")
|
||
else:
|
||
print(f"❌ ESP32状态API异常: {response.status_code}")
|
||
return False
|
||
|
||
# 测试设备信息API
|
||
response = requests.get("http://192.168.1.3:80/api/device/info", timeout=5)
|
||
if response.status_code == 200:
|
||
print("✅ ESP32设备信息API正常")
|
||
info = response.json()
|
||
print(f" MAC地址: {info.get('mac_address', 'Unknown')}")
|
||
print(f" 设备ID: {info.get('device_id', 'Unknown')}")
|
||
else:
|
||
print(f"❌ ESP32设备信息API异常: {response.status_code}")
|
||
|
||
# 测试报警历史API
|
||
response = requests.get("http://192.168.1.3:80/api/alarm/history", timeout=5)
|
||
if response.status_code == 200:
|
||
print("✅ ESP32报警历史API正常")
|
||
history = response.json()
|
||
alarm_count = len(history.get('alarms', []))
|
||
print(f" 当前报警数量: {alarm_count}")
|
||
return True
|
||
else:
|
||
print(f"❌ ESP32报警历史API异常: {response.status_code}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ ESP32连接失败: {e}")
|
||
return False
|
||
|
||
def monitor_esp32_for_cloud_logs():
|
||
"""监控ESP32,等待云端上报相关日志"""
|
||
print("\n📺 ESP32日志监控指导:")
|
||
print("=" * 50)
|
||
print("请手动执行以下步骤:")
|
||
print("1. 打开新的命令行窗口")
|
||
print("2. 执行: cd d:\\XinJiaPo\\Firefly_code\\smart-home\\firefly_esp32")
|
||
print("3. 执行: idf.py -p COM7 monitor")
|
||
print("4. 重启ESP32(按RESET按钮)")
|
||
print("5. 观察启动日志中是否有以下内容:")
|
||
print()
|
||
print("✅ 应该看到的初始化日志:")
|
||
print(" I CloudAlarmReporter: 🌐 开始初始化云端报警上报模块")
|
||
print(" I CloudAlarmReporter: 🔗 目标服务器: http://192.168.1.57:36988/device-alarms")
|
||
print(" I CloudAlarmReporter: 🔑 设备密钥: 39dc753bd0cb19da...")
|
||
print(" I CloudAlarmReporter: ✅ 云端报警上报模块初始化成功")
|
||
print()
|
||
print("🚨 触发报警时应该看到:")
|
||
print(" I AlarmManager: 🌐 准备上报报警到云端: [燃烧] 检测到燃烧...")
|
||
print(" I AlarmManager: ☁️ 报警已成功加入云端上报队列")
|
||
print(" I CloudAlarmReporter: 📨 开始上报报警...")
|
||
print(" I CloudAlarmReporter: 📡 HTTP响应状态码: 200")
|
||
print(" I CloudAlarmReporter: ✅ 报警上报成功")
|
||
print()
|
||
print("❌ 如果看到错误:")
|
||
print(" E CloudAlarmReporter: ❌ 创建报警上报队列失败")
|
||
print(" E CloudAlarmReporter: ❌ 创建云端上报任务失败")
|
||
print(" E CloudAlarmReporter: ❌ HTTP请求失败")
|
||
print(" E CloudAlarmReporter: ❌ 连接超时")
|
||
print("=" * 50)
|
||
|
||
def create_simple_test_server():
|
||
"""创建简单的测试服务器"""
|
||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||
|
||
class SimpleHandler(BaseHTTPRequestHandler):
|
||
def do_POST(self):
|
||
if self.path == '/device-alarms':
|
||
content_length = int(self.headers.get('Content-Length', 0))
|
||
post_data = self.rfile.read(content_length)
|
||
|
||
print(f"\n🎉 收到ESP32报警上报!")
|
||
print(f"时间: {datetime.now().strftime('%H:%M:%S')}")
|
||
print(f"数据: {post_data.decode('utf-8')}")
|
||
|
||
self.send_response(200)
|
||
self.send_header('Content-Type', 'application/json')
|
||
self.end_headers()
|
||
response = {"code": 200, "message": "success"}
|
||
self.wfile.write(json.dumps(response).encode('utf-8'))
|
||
else:
|
||
self.send_response(404)
|
||
self.end_headers()
|
||
|
||
def do_GET(self):
|
||
if self.path == '/health':
|
||
self.send_response(200)
|
||
self.send_header('Content-Type', 'application/json')
|
||
self.end_headers()
|
||
response = {"status": "ok"}
|
||
self.wfile.write(json.dumps(response).encode('utf-8'))
|
||
else:
|
||
self.send_response(404)
|
||
self.end_headers()
|
||
|
||
def log_message(self, format, *args):
|
||
pass # 禁用默认日志
|
||
|
||
try:
|
||
server = HTTPServer(('0.0.0.0', 36988), SimpleHandler)
|
||
print("🌐 简单测试服务器启动成功 (http://0.0.0.0:36988)")
|
||
server.serve_forever()
|
||
except Exception as e:
|
||
print(f"❌ 启动测试服务器失败: {e}")
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("🔧 ESP32云端报警上报功能最终诊断")
|
||
print("=" * 60)
|
||
print(f"诊断时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||
print("=" * 60)
|
||
|
||
# 1. 测试ESP32基本功能
|
||
esp32_ok = test_esp32_basic_functions()
|
||
|
||
if not esp32_ok:
|
||
print("\n❌ ESP32基本功能测试失败,请检查:")
|
||
print("1. ESP32是否正常启动")
|
||
print("2. WiFi连接是否正常")
|
||
print("3. HTTP服务器是否运行")
|
||
return
|
||
|
||
# 2. 启动简单测试服务器
|
||
print(f"\n🌐 启动简单测试服务器...")
|
||
server_thread = threading.Thread(target=create_simple_test_server, daemon=True)
|
||
server_thread.start()
|
||
time.sleep(2)
|
||
|
||
# 3. 显示监控指导
|
||
monitor_esp32_for_cloud_logs()
|
||
|
||
# 4. 等待用户操作
|
||
print(f"\n⏳ 等待ESP32报警上报...")
|
||
print("请按以下步骤操作:")
|
||
print("1. 重启ESP32并观察串口日志")
|
||
print("2. 用手接触热成像传感器触发报警")
|
||
print("3. 观察此窗口是否收到上报数据")
|
||
print("4. 按 Ctrl+C 结束测试")
|
||
|
||
try:
|
||
while True:
|
||
time.sleep(1)
|
||
except KeyboardInterrupt:
|
||
print(f"\n\n🎯 诊断结束")
|
||
print("如果没有收到上报数据,可能的原因:")
|
||
print("1. CloudAlarmReporter模块未正确初始化")
|
||
print("2. ESP32无法访问192.168.1.57:36988")
|
||
print("3. 报警触发但上报队列有问题")
|
||
print("4. HTTP客户端配置错误")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|