- find/listdir/r/sound/SendWX 及 WARN.mp3 移入 tools/(sound.py 相对路径同目录保持可用) - Version 死代码(import 自增 VER)归档 tools/Version - Main.py(生产入口候选)与 TODO 保留根目录待确认
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import time
|
|
import datetime
|
|
import requests
|
|
import pygame
|
|
import os
|
|
|
|
def PlayWarning():
|
|
try:
|
|
pygame.mixer.init()
|
|
pygame.mixer.music.load("./warn.mp3")
|
|
pygame.mixer.music.play(-1)
|
|
except Exception as e:
|
|
print(f"播放报警失败: {e}")
|
|
|
|
def StopWarning():
|
|
try:
|
|
if pygame.mixer.get_init():
|
|
pygame.mixer.music.stop()
|
|
pygame.mixer.quit()
|
|
except Exception as e:
|
|
print(f"停止报警失败: {e}")
|
|
|
|
def CheckBaidu():
|
|
try:
|
|
return requests.get("https://www.baidu.com", timeout=10).status_code == 200
|
|
except:
|
|
return False
|
|
|
|
def IsAlarmTime():
|
|
now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))
|
|
return 7 <= now.hour <= 22
|
|
|
|
def Main():
|
|
print("进程启动,开始监控...")
|
|
consecutiveFailures = 0
|
|
warningPlaying = False
|
|
|
|
while True:
|
|
success = CheckBaidu()
|
|
|
|
if success:
|
|
print("访问成功")
|
|
consecutiveFailures = 0
|
|
if warningPlaying:
|
|
StopWarning()
|
|
warningPlaying = False
|
|
time.sleep(10)
|
|
else:
|
|
consecutiveFailures += 1
|
|
print(f"访问失败,连续次数: {consecutiveFailures}")
|
|
|
|
if consecutiveFailures >= 5 and IsAlarmTime() and not warningPlaying:
|
|
print("开始播放报警音频...")
|
|
PlayWarning()
|
|
warningPlaying = True
|
|
elif consecutiveFailures >= 5 and not IsAlarmTime() and warningPlaying:
|
|
StopWarning()
|
|
warningPlaying = False
|
|
|
|
time.sleep(1 if consecutiveFailures >= 5 else 5)
|
|
|
|
if __name__ == "__main__":
|
|
if not os.path.exists("./warn.mp3"):
|
|
print("错误: 未找到 ./warn.mp3")
|
|
exit(1)
|
|
Main() |