From 175ff36bce77e156da20af30a5689ff1776623f3 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Tue, 9 Apr 2024 20:08:16 +0800 Subject: [PATCH] Update --- Healthbeat.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Healthbeat.py b/Healthbeat.py index 0c9c5ea..5004bca 100644 --- a/Healthbeat.py +++ b/Healthbeat.py @@ -13,26 +13,29 @@ class Camera: self.status = status def healthbeat(self): - cap = cv2.VideoCapture(self.rtsp) - ret, _ = cap.read() - cap.release() + try: + cap = cv2.VideoCapture(self.rtsp) + ret, _ = cap.read() + cap.release() - # 摄像头状态: 1 在线, 2 离线 - status = 1 if ret == True else 2 + # 摄像头状态: 1 在线, 2 离线 + status = 1 if ret == True else 2 - if status == 1: - logging.info("摄像头ID: %s, 状态: 在线" % (self.id)) - else: - logging.warning("摄像头ID: %s, 状态: 离线" % (self.id)) + if status == 1: + logging.info("摄像头ID: %s, 状态: 在线" % (self.id)) + else: + logging.warning("摄像头ID: %s, 状态: 离线" % (self.id)) - if (status == 1 and self.status == 2) or (status == 2 and self.status == 1): - conn = get_connection() + if (status == 1 and self.status == 2) or (status == 2 and self.status == 1): + conn = get_connection() - with conn: - with conn.cursor() as cursor: - cursor.execute("update devices set status = %s where id = %s", (status, self.id)) + with conn: + with conn.cursor() as cursor: + cursor.execute("update devices set status = %s, updated_at = %s where id = %s", (status, self.id, datetime.datetime.now())) - conn.close() + conn.close() + except Exception as e: + logging.error(f"摄像头ID: {self.id}, 检测状态时发生错误: {str(e)}") def init_logger(): logging.basicConfig(filename='healthbeat.log', level=logging.INFO, format='[%(asctime)s] %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') @@ -60,10 +63,15 @@ if __name__ == '__main__': cursor.execute("SELECT id, status, extends FROM devices WHERE type = 1 AND status in (1, 2) AND supplier_key not in ('device-supplier-biang', 'device-supplier-yunfei') ORDER BY id ASC") rows = cursor.fetchall() + threads = [] for row in rows: c = Camera(row[0], row[2]["rtsp_url"], row[1]) t = threading.Thread(target=c.healthbeat) + threads.append(t) t.start() + for t in threads: + t.join() + conn.close() \ No newline at end of file