Skip to content

Commit f19ced3

Browse files
committed
Merge branch 'main' of github.com:NickM-27/swatch
2 parents 1560964 + 1caac31 commit f19ced3

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

docs/config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ cameras:
6767
# REQUIRED: Name of the zone.
6868
street:
6969
# REQUIRED: Coordinates to crop the zone by.
70-
# NOTE: The order of the coordinates are: y, y+h, x, x+w starting in the top left corner as 0, 0.
70+
# NOTE: The order of the coordinates are: x, y, x+w, y+h starting in the top left corner as 0, 0.
7171
coordinates: 225, 540, 350, 620
7272
# REQUIRED: List of objects that may be in this zone. These correspond to
7373
# the objects list defined previously and are matched by name.

swatch/app.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ def __init__(self) -> None:
2626
"""Init SwatchApp."""
2727
print("Starting SwatchApp")
2828
self.stop_event = multiprocessing.Event()
29+
# startup nginx
30+
os.system("/usr/local/nginx/sbin/nginx &")
31+
32+
# startup internal processes
33+
self.__init_config__()
34+
self.__init_db__()
35+
self.__init_processing__()
36+
self.__init_snapshot_cleanup__()
37+
self.__init_detection_cleanup__()
38+
self.__init_web_server__()
2939

3040
def __init_config__(self) -> None:
3141
"""Init SwatchApp with saved config file."""
@@ -90,17 +100,6 @@ def __init_web_server__(self) -> None:
90100
def start(self) -> None:
91101
"""Start SwatchApp."""
92102

93-
# startup nginx
94-
os.system("/usr/local/nginx/sbin/nginx &")
95-
96-
# startup internal processes
97-
self.__init_config__()
98-
self.__init_db__()
99-
self.__init_processing__()
100-
self.__init_snapshot_cleanup__()
101-
self.__init_detection_cleanup__()
102-
self.__init_web_server__()
103-
104103
def receiveSignal(signalNumber: int, frame: Optional[FrameType]) -> None:
105104
self.stop()
106105
sys.exit()

swatch/snapshot.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SnapshotProcessor:
3636

3737
def __init__(self, config: SwatchConfig) -> None:
3838
self.config = config
39+
self.media_dir = os.environ.get("MEDIA_DIR", CONST_MEDIA_DIR)
3940

4041
def save_snapshot(
4142
self,
@@ -47,7 +48,7 @@ def save_snapshot(
4748
"""Saves the file snapshot to the correct snapshot dir."""
4849
time = datetime.datetime.now()
4950

50-
file_dir = f"{CONST_MEDIA_DIR}/snapshots/{time.strftime('%m-%d')}/{camera_name}"
51+
file_dir = f"{self.media_dir}/snapshots/{time.strftime('%m-%d')}/{camera_name}"
5152

5253
if not os.path.exists(file_dir):
5354
print(f"{file_dir} doesn't exist, creating...")
@@ -87,10 +88,10 @@ def save_snapshot(
8788

8889
def get_detection_snapshot(self, detection: Detection) -> Any:
8990
"""Get file snapshot for a specific detection."""
90-
file_dir = f"{CONST_MEDIA_DIR}/snapshots/{datetime.datetime.fromtimestamp(detection.start_time).strftime('%m-%d')}/{detection.camera}"
91+
file_dir = f"{self.media_dir}/snapshots/{datetime.datetime.fromtimestamp(detection.start_time).strftime('%m-%d')}/{detection.camera}"
9192

9293
if not os.path.exists(file_dir):
93-
file_dir = f"{CONST_MEDIA_DIR}/snapshots/{datetime.datetime.fromtimestamp(detection.end_time).strftime('%m-%d')}/{detection.camera}"
94+
file_dir = f"{self.media_dir}/snapshots/{datetime.datetime.fromtimestamp(detection.end_time).strftime('%m-%d')}/{detection.camera}"
9495

9596
if not os.path.exists(file_dir):
9697
return None
@@ -142,7 +143,7 @@ def get_latest_zone_snapshot(
142143

143144
def get_latest_detection_snapshot(self, camera_name: str) -> Any:
144145
"""Get the latest file snapshot for a <camera_name> detection."""
145-
snaps_dir = f"{CONST_MEDIA_DIR}/snapshots"
146+
snaps_dir = f"{self.media_dir}/snapshots"
146147
recent_folder = max(
147148
[os.path.join(snaps_dir, basename) for basename in os.listdir(snaps_dir)]
148149
)
@@ -173,6 +174,7 @@ def __init__(
173174
threading.Thread.__init__(self)
174175
self.name = "snapshot_cleanup"
175176
self.config = config
177+
self.media_dir = os.environ.get("MEDIA_DIR", CONST_MEDIA_DIR)
176178
self.stop_event = stop_event
177179

178180
def cleanup_snapshots(self, camera_config: CameraConfig):
@@ -182,9 +184,9 @@ def cleanup_snapshots(self, camera_config: CameraConfig):
182184
)
183185
valid_month, _, valid_day = retain_days_ago.strftime("%m-%d").partition("-")
184186

185-
for snap_dir in os.listdir(f"{CONST_MEDIA_DIR}/snapshots/"):
187+
for snap_dir in os.listdir(f"{self.media_dir}/snapshots/"):
186188
if not os.path.isdir(
187-
os.path.join(f"{CONST_MEDIA_DIR}/snapshots/", snap_dir)
189+
os.path.join(f"{self.media_dir}/snapshots/", snap_dir)
188190
):
189191
continue
190192

@@ -193,11 +195,11 @@ def cleanup_snapshots(self, camera_config: CameraConfig):
193195

194196
if month == valid_month and valid_day >= day:
195197
delete_dir(
196-
f"{CONST_MEDIA_DIR}/snapshots/{month}-{day}", camera_config.name
198+
f"{self.media_dir}/snapshots/{month}-{day}", camera_config.name
197199
)
198200
elif valid_month > month and (int(day) - int(valid_day)) <= 24:
199201
delete_dir(
200-
f"{CONST_MEDIA_DIR}/snapshots/{month}-{day}", camera_config.name
202+
f"{self.media_dir}/snapshots/{month}-{day}", camera_config.name
201203
)
202204

203205
def run(self):

0 commit comments

Comments
 (0)