From b9e33f407ebd194052cd66ab304b7b09d4d24e5e Mon Sep 17 00:00:00 2001
From: Mikhail Sennikov <mifls@yandex.ru>
Date: Sun, 28 Jan 2024 20:23:30 +0300
Subject: [PATCH] Fix warmup

---
 Backend/node/swarm.py                    |  4 ----
 ProxyNode/node_settings/node_settings.py | 12 ++++--------
 ProxyNode/onvif_proxy/hot_cams.py        |  3 ++-
 database/api/cam_groups.py               |  8 --------
 database/api/cams.py                     |  4 ----
 database/api/nodes.py                    |  4 ----
 database/api/roles.py                    |  4 ----
 database/api/users.py                    |  4 ----
 8 files changed, 6 insertions(+), 37 deletions(-)

diff --git a/Backend/node/swarm.py b/Backend/node/swarm.py
index bd9f83a..1cb59ad 100644
--- a/Backend/node/swarm.py
+++ b/Backend/node/swarm.py
@@ -66,10 +66,6 @@ class NodeSwarm:
         async with db.get_session() as session:
             group_repo = GroupRepository(session)
             group = await group_repo.get(group_id)
-            if group is None:
-                raise HTTPException(
-                    status_code=status.HTTP_404_NOT_FOUND
-                )
             await self._set_group(group, session)
             return group
 
diff --git a/ProxyNode/node_settings/node_settings.py b/ProxyNode/node_settings/node_settings.py
index 564c6db..2bc85d1 100644
--- a/ProxyNode/node_settings/node_settings.py
+++ b/ProxyNode/node_settings/node_settings.py
@@ -18,21 +18,17 @@ hot_cams: HotCams = HotCams()
 @node_router.post("/set/{node_id}", response_model=NodeFull)
 async def set_node_id(
         node_id: int,
-        node_repo: NodeRepository = Depends(db.get_repository(NodeRepository))
 ):
     global current_node
     global hot_cams
-    node = await node_repo.get(node_id)
-    if node is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
-    current_node = node
+    with db.get_session() as session:
+        node_repo: NodeRepository = NodeRepository(session)
+        current_node = await node_repo.get(node_id)
+        cams = await node_repo.get_node_cams(current_node)
 
     # Warm up cams
     del hot_cams
     hot_cams = HotCams()
-    cams = await node_repo.get_node_cams(current_node)
     cams = [int(getattr(cam, 'id')) for cam in cams]
     await hot_cams.try_connect_cams_list(cams)
     return current_node
diff --git a/ProxyNode/onvif_proxy/hot_cams.py b/ProxyNode/onvif_proxy/hot_cams.py
index 3dd5b0f..c1f3cb8 100644
--- a/ProxyNode/onvif_proxy/hot_cams.py
+++ b/ProxyNode/onvif_proxy/hot_cams.py
@@ -23,6 +23,7 @@ class HotCams:
                 try:
                     cam = Camera(_cam_id, **kwargs)
                     await cam.update()
+                    print(f"Connection to camera {kwargs['name']} established.")
                     break
                 except (ONVIFError, httpx.ConnectTimeout, httpx.ConnectError) as e:
                     if 'name' in kwargs:
@@ -38,7 +39,7 @@ class HotCams:
         async def try_connect(self, cam_id: int):
             async with db.get_session() as session:
                 cam_repo = CamRepository(session)
-                cam_db_obj = await cam_repo.get(cam_id)
+                cam_db_obj = await cam_repo.get_or_none(cam_id)
                 if cam_db_obj is None:
                     raise HTTPException(
                         status_code=status.HTTP_404_NOT_FOUND
diff --git a/database/api/cam_groups.py b/database/api/cam_groups.py
index 2c3b1de..72b15cf 100644
--- a/database/api/cam_groups.py
+++ b/database/api/cam_groups.py
@@ -30,10 +30,6 @@ async def get_group(
         group_repo: GroupRepository = Depends(db.get_repository(GroupRepository))
 ):
     group = await group_repo.get(group_id)
-    if group is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return group
 
 
@@ -84,10 +80,6 @@ async def list_cams_in_group(
         group_repo: GroupRepository = Depends(db.get_repository(GroupRepository))
 ):
     group = await group_repo.get(group_id)
-    if group is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return await group_repo.list_cams_in_group(group)
 
 
diff --git a/database/api/cams.py b/database/api/cams.py
index d1e2c6b..6b5404e 100644
--- a/database/api/cams.py
+++ b/database/api/cams.py
@@ -30,10 +30,6 @@ async def get_cam(
         cam_repo: CamRepository = Depends(db.get_repository(CamRepository))
 ):
     cam = await cam_repo.get(cam_id)
-    if cam is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return cam
 
 
diff --git a/database/api/nodes.py b/database/api/nodes.py
index 3fc28c2..6a512ff 100644
--- a/database/api/nodes.py
+++ b/database/api/nodes.py
@@ -27,10 +27,6 @@ async def get_node(
         node_repo: NodeRepository = Depends(db.get_repository(NodeRepository))
 ):
     node = await node_repo.get(node_id)
-    if node is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return node
 
 
diff --git a/database/api/roles.py b/database/api/roles.py
index 0d7d175..6d89668 100644
--- a/database/api/roles.py
+++ b/database/api/roles.py
@@ -27,10 +27,6 @@ async def get_role(
         role_repo: RoleRepository = Depends(db.get_repository(RoleRepository))
 ):
     role = await role_repo.get(role_id)
-    if role is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return role
 
 
diff --git a/database/api/users.py b/database/api/users.py
index 4607d74..0d6954e 100644
--- a/database/api/users.py
+++ b/database/api/users.py
@@ -29,10 +29,6 @@ async def get_user(
         user_repo: UserRepository = Depends(db.get_repository(UserRepository))
 ):
     user = await user_repo.get(user_id)
-    if user is None:
-        raise HTTPException(
-            status_code=status.HTTP_404_NOT_FOUND
-        )
     return user
 
 
-- 
GitLab