smart-home/test_port_30088.py
2026-02-26 09:16:34 +08:00

96 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试ESP32云端报警上报到30088端口
"""
import requests
import json
import time
from datetime import datetime
def test_port_30088():
"""测试30088端口的云端服务器"""
print("☁️ 测试30088端口云端服务器...")
try:
# 测试健康检查端点
response = requests.get("http://115.190.167.176:30088/health", timeout=10)
if response.status_code == 200:
print("✅ 30088端口服务器正常")
return True
else:
print(f"⚠️ 30088端口服务器响应异常: {response.status_code}")
return False
except Exception as e:
print(f"❌ 30088端口服务器连接失败: {e}")
return False
def test_device_alarms_endpoint():
"""测试30088端口的device-alarms端点"""
print("🧪 测试30088端口device-alarms端点...")
try:
# 模拟ESP32发送的报警数据
test_alarm = {
"physicalUid": "206EF1B3AD74",
"sourceEventId": f"206EF1B3AD74:{int(time.time() * 1000)}",
"title": "测试报警",
"level": "warning",
"message": "测试ESP32到30088端口的上报功能",
"temp": 35.8,
"occurredAtMs": int(time.time() * 1000)
}
headers = {
"Content-Type": "application/json",
"X-Device-Key": "39dc753bd0cb19da5050a2f0edf932f851f5b21b90a6d06ecf9a28d81079a0a6"
}
response = requests.post("http://115.190.167.176:30088/device-alarms",
json=test_alarm, headers=headers, timeout=15)
if response.status_code == 200:
result = response.json()
print(f"✅ 30088端口报警接收成功: {result}")
return True
else:
print(f"❌ 30088端口报警接收失败: {response.status_code}")
print(f"响应内容: {response.text}")
return False
except Exception as e:
print(f"❌ 30088端口报警测试异常: {e}")
return False
def main():
"""主函数"""
print("🚀 ESP32端口30088云端报警上报测试")
print("=" * 60)
print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"新服务器: http://115.190.167.176:30088/device-alarms")
print(f"ESP32地址: http://192.168.1.3:80")
print("=" * 60)
# 1. 测试30088端口服务器
server_ok = test_port_30088()
# 2. 测试device-alarms端点
endpoint_ok = test_device_alarms_endpoint()
print(f"\n🎯 测试结果:")
print("=" * 30)
print(f"30088端口服务器: {'✅ 正常' if server_ok else '❌ 异常'}")
print(f"device-alarms端点: {'✅ 正常' if endpoint_ok else '❌ 异常'}")
if server_ok and endpoint_ok:
print(f"\n🎉 30088端口服务器准备就绪")
print(f"\n📋 现在可以验证ESP32上报:")
print("1. 重启ESP32并观察串口日志")
print("2. 确认看到: 🔗 目标服务器: http://115.190.167.176:30088/device-alarms")
print("3. 触发报警测试")
print("4. 观察HTTP响应状态码是否为200")
else:
print(f"\n❌ 30088端口存在问题请检查服务器部署")
if __name__ == "__main__":
main()