Files

109 lines
3.0 KiB
Python
Raw Normal View History

2024-04-08 17:25:13 +08:00
import time
2024-04-10 09:51:37 +08:00
from datetime import datetime, timedelta, timezone
2024-04-08 17:25:13 +08:00
from pathlib import Path
2024-04-01 12:03:18 +08:00
import pyautogui
2024-04-09 16:18:55 +08:00
import requests
2024-04-08 17:25:13 +08:00
import yaml
2024-04-01 12:03:18 +08:00
2024-04-08 17:25:13 +08:00
from hik_uptime.custom_exception import NoWindowException
2024-04-01 12:03:18 +08:00
2024-04-08 17:25:13 +08:00
def read_config():
home_dir = Path.home()
2024-04-09 16:18:55 +08:00
with open(
2024-04-10 09:51:37 +08:00
home_dir / ".config" / "hik-uptime" / "config.yaml", "r", encoding="utf-8"
2024-04-09 16:18:55 +08:00
) as f:
2024-04-08 17:25:13 +08:00
config = yaml.safe_load(f)
return config
2024-04-01 12:03:18 +08:00
2024-04-08 17:25:13 +08:00
def get_status():
# 找到窗口并最大化
2024-04-09 16:18:55 +08:00
window = pyautogui.getWindowsWithTitle("智慧园区客户端")
2024-04-08 17:25:13 +08:00
if len(window) == 0:
raise NoWindowException("No window found")
2024-04-01 12:03:18 +08:00
window = window[0]
2024-04-09 21:38:29 +08:00
2024-04-01 12:03:18 +08:00
window.maximize()
window.activate()
2024-04-08 17:25:13 +08:00
2024-04-09 16:18:55 +08:00
time.sleep(2)
pyautogui.click(x=790, y=133)
time.sleep(2)
2024-04-08 17:25:13 +08:00
2024-04-09 16:18:55 +08:00
offline_devices = []
2024-04-08 17:25:13 +08:00
# TODO: OCR 获取已离线的设备名称数量
2024-04-09 16:18:55 +08:00
# 报警主机54 online: #0cb317
color_54 = pyautogui.pixel(369, 269)
color_54_hex = "#{:02x}{:02x}{:02x}".format(*color_54)
if color_54_hex != "#0cb317":
offline_devices.append("报警主机54")
# 报警主机53 online: #0db318
color_53 = pyautogui.pixel(742, 269)
color_53_hex = "#{:02x}{:02x}{:02x}".format(*color_53)
if color_53_hex != "#0db318":
offline_devices.append("报警主机53")
# 报警主机52 online: #0db318
color_52 = pyautogui.pixel(1116, 269)
color_52_hex = "#{:02x}{:02x}{:02x}".format(*color_52)
if color_52_hex != "#0db318":
offline_devices.append("报警主机52")
# 报警主机52 online: #0cb317
color_51 = pyautogui.pixel(369, 419)
color_51_hex = "#{:02x}{:02x}{:02x}".format(*color_51)
if color_51_hex != "#0cb317":
offline_devices.append("报警主机51")
# 报警主机52 online: #0db318
color_50 = pyautogui.pixel(742, 269)
color_50_hex = "#{:02x}{:02x}{:02x}".format(*color_50)
if color_50_hex != "#0db318":
offline_devices.append("报警主机50")
2024-04-10 09:51:37 +08:00
if len(offline_devices) > 0:
print(offline_devices)
else:
print("No offline devices")
current_time = datetime.now()
tz = timezone(timedelta(hours=8))
happen_time = current_time.astimezone(tz).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
2024-04-09 16:18:55 +08:00
for offline_device in offline_devices:
push_json = {
"eventType": "报警主机离线",
2024-04-10 09:51:37 +08:00
"happenTime": happen_time,
2024-04-09 16:18:55 +08:00
"deviceName": offline_device,
2024-04-10 09:51:37 +08:00
"regionName": offline_device,
2024-04-09 16:18:55 +08:00
"eventLvl": 2,
"data": {"typeName": f"{offline_device}离线"},
"userIds": ["2110"],
}
2024-04-09 21:38:29 +08:00
resp = requests.post(
2024-04-09 16:18:55 +08:00
"http://192.168.20.115:8000/ipaasuat/engine_company/anycross/trigger/callback/MmRhZTE4OTRiYjVkZDQ5YWNmOGRmZDI0NjQ1MTBlODUw",
json=push_json,
)
2024-04-09 21:44:37 +08:00
print(resp.content.decode("utf-8"))
2024-04-10 09:51:37 +08:00
time.sleep(1)
2024-04-09 16:18:55 +08:00
def capture_loop():
while True:
try:
get_status()
except Exception as e:
print(e)
time.sleep(60 * 5)
def run_app():
2024-04-10 09:51:37 +08:00
capture_loop()
2024-04-01 12:03:18 +08:00
if __name__ == "__main__":
2024-04-09 16:18:55 +08:00
run_app()