master
Jing Li 2024-04-09 20:08:16 +08:00
parent 76b0f2ae21
commit 175ff36bce
1 changed files with 23 additions and 15 deletions

View File

@ -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()