-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityCamera.py
85 lines (74 loc) · 2.98 KB
/
SecurityCamera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from SmartLight import SmartLight
from datetime import datetime
import random
class SecurityCameraError(Exception):
""" Custom exception for security camera errors. """
pass
class SecurityCamera:
def __init__(self, device_id, automation_system):
self.device_id = device_id
self.status = "off"
self.recording = False
self.infrared = False
self.motion_detected = False
self.automation_system = automation_system # Store the automation system instance
# Initialize status_var
self.status_var = None
def turn_on(self):
self.status = "on"
self.motion_detected = True
self._log(f"SecurityCamera {self.device_id} is turned on.")
self._update_status_var()
def turn_off(self):
self.status = "off"
self.recording = False
self.infrared = False
self._log(f"SecurityCamera {self.device_id} is turned off.")
self._update_status_var()
def start_recording(self):
if self.status == "on":
self.recording = True
self._log(f"SecurityCamera {self.device_id} is recording.")
self._update_status_var()
else:
self._log(f"SecurityCamera {self.device_id} is off. Cannot start recording.")
def stop_recording(self):
if self.recording:
self.recording = False
self._log(f"SecurityCamera {self.device_id} stopped recording.")
self._update_status_var()
else:
self._log(f"SecurityCamera {self.device_id} is not recording. Cannot stop.")
def enable_infrared(self):
if self.status == "on":
self.infrared = True
self._log(f"SecurityCamera {self.device_id} infrared enabled.")
self._update_status_var()
else:
self._log(f"SecurityCamera {self.device_id} is off. Cannot enable infrared.")
def disable_infrared(self):
if self.infrared:
self.infrared = False
self._log(f"SecurityCamera {self.device_id} infrared disabled.")
self._update_status_var()
else:
self._log(f"SecurityCamera {self.device_id} infrared is not enabled. Cannot disable.")
def detect_motion(self):
self._log(f"SecurityCamera {self.device_id} detected motion.")
if self.status == "off":
self.turn_on()
if not self.recording:
try:
self.start_recording()
except SecurityCameraError as e:
self._log(f"Error: {e}")
return True
def _update_status_var(self):
""" Update the status variable if it's defined. """
if self.status_var:
self.status_var.set(f"Status: {self.status}, Recording: {self.recording}, Infrared: {self.infrared}")
def _log(self, message):
""" Log message with timestamp. """
timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
full_message = f"{timestamp} {message}"
print(full_message)