This commit is contained in:
2025-10-31 13:46:24 +08:00
parent b807b0f1b4
commit efc6a92059
5 changed files with 375 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ VIDEO_SAVE_PATH = "/mnt/save/video"
IMAGE_SAVE_PATH = "/mnt/save/warning"
MODBUS_BIN_PATH = "/home/orangepi/RKApp/GPIOSignal/bin/sendGpioSignal"
GPIO_CONFIG_FILE = "/home/orangepi/RKApp/InitAuth/conf/.env"
Camera_Config_File = "/opt/rknn-yolov11/.env"
os.makedirs(VIDEO_SAVE_PATH, exist_ok=True)
os.makedirs(IMAGE_SAVE_PATH, exist_ok=True)
@@ -143,24 +144,43 @@ async def websocket_distance(websocket: WebSocket):
with gpio_state_lock:
current_time = time.time()
# 更新最后检测到人的时间
last_person_time = current_time
print(f"[GPIO] 🔄 更新最后检测时间")
# 读取配置文件中的危险距离阈值
danger_distance = 3.0 # 默认值
try:
with open(GPIO_CONFIG_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line.strip().startswith('MAX_DISTANCE'):
danger_distance = float(line.strip().split(':')[1])
break
except Exception as e:
print(f"[Config] ⚠️ 读取危险距离失败使用默认值3.0m: {e}")
# 如果当前是高电平,切换到低电平
if current_gpio_state == 'HIGH':
print(f"[GPIO] 🔻 当前为高电平,准备切换到低电平")
set_gpio_low()
print(f"[Config] 📏 危险距离阈值: {danger_distance}m, 当前距离: {distance}m")
# 判断是否在危险区域内
if distance is not None and distance < danger_distance:
# 人在危险区域内,更新时间戳
last_person_time = current_time
print(f"[GPIO] ⚠️ 危险!距离 {distance:.2f}m < {danger_distance:.2f}m")
# 如果当前是高电平,切换到低电平
if current_gpio_state == 'HIGH':
print(f"[GPIO] 🔻 当前为高电平,准备切换到低电平")
set_gpio_low()
else:
print(f"[GPIO] ⚡ 当前已是低电平,保持状态")
else:
print(f"[GPIO] ⚡ 当前已是低电平,保持状态")
# 人在安全区域不触发GPIO
print(f"[GPIO] ✅ 安全距离 {distance:.2f}m >= {danger_distance:.2f}m不触发GPIO")
# 发送响应给客户端
await websocket.send_json({
"status": "success",
"distance": distance,
"gpio_state": current_gpio_state,
"message": f"已检测到人GPIO状态: {current_gpio_state}"
})
# # 发送响应给客户端
# await websocket.send_json({
# "status": "success",
# "distance": distance,
# "gpio_state": current_gpio_state,
# "message": f"已检测到人GPIO状态: {current_gpio_state}"
# })
except json.JSONDecodeError:
await websocket.send_text("invalid JSON")