44import json
55import sys
66from concurrent .futures import wait
7+ from time import sleep
78from typing import Any
89
910from core .observers .observer .hss_observer import HomeSecuritySystemObserver
1011from core .observers .subject .eye_subject import EyeSubject
1112from core .observers .subject .wifi_subject import WiFiSubject
1213from core .strategies .detectors .efficientdet_strategy import EfficientdetStrategy
1314from core .strategies .eye .picamera_strategy import PiCameraStrategy
15+ from core .strategies .notifier .telegram_strategy import TelegramStrategy
1416from core .strategies .notifier .whatsapp_strategy import WhatsappStrategy
1517from core .strategies .wifi .admin_panel_strategy import AdminPanelStrategy
16- from core .utils .datatypes import Protector , WhatsappReciever
18+ from core .utils .datatypes import Protector , TelegramReciever
19+ from core .utils .fileio_adaptor import upload_to_fileio
1720
1821
1922def read_configurations () -> dict [str , Any ]:
@@ -35,21 +38,20 @@ def main():
3538 # Read configurations.
3639 config , strategy_config = read_configurations ()
3740 # Create a WhatsApp notifier.
38- whatsapp_notifier = WhatsappStrategy ( )
41+ notifier = TelegramStrategy ( strategy_config [ 'telegram_strategy' ][ 'bot_key' ] )
3942 for reciever in config ['recievers' ]:
40- whatsapp_notifier .add_reciever (WhatsappReciever (reciever ['name' ],
41- reciever ['tel_no' ],
42- reciever ['callmebot_key' ]))
43+ notifier .add_reciever (TelegramReciever (reciever ['name' ],
44+ reciever ['chat_id' ]))
4345
4446 # Create a Protector within IpAddressStrategy.
45- ip_address_strategy = AdminPanelStrategy (strategy_config )
47+ network_strategy = AdminPanelStrategy (strategy_config [ 'admin_panel_strategy' ] )
4648 for protector in config ['protectors' ]:
47- ip_address_strategy .add_protector (Protector (protector ['name' ],
48- protector ['address' ]))
49+ network_strategy .add_protector (Protector (protector ['name' ],
50+ protector ['address' ]))
4951
5052 # Create observer.
5153 hss_observer = HomeSecuritySystemObserver ()
52- hss_observer .set_notifier (whatsapp_notifier )
54+ hss_observer .set_notifier (notifier )
5355
5456 # Create subjects to observe.
5557 wifi_subject = WiFiSubject ()
@@ -58,23 +60,31 @@ def main():
5860 eye_subject .attach (hss_observer )
5961
6062 # Run subjects.
61- wifi_subject .run (ip_address_strategy )
63+ wifi_subject .run (network_strategy )
6264
6365 # Set-up the camera to detect humans.
6466 camera = PiCameraStrategy ()
6567 camera .set_detector (EfficientdetStrategy ())
6668 eye_subject .run (camera , wifi_subject .get_protector_lock ())
6769
6870 # Notify that the system is running.
69- whatsapp_notifier .notify_all ("Home Security System is started." )
71+ notifier .notify_all ("Home Security System is started." )
72+ sleep (5 )
73+
74+ if isinstance (notifier , TelegramStrategy ):
75+ with open (f"{ eye_subject ._image_path } /initial_frame.jpg" , "rb" ) as initial_frame :
76+ notifier .send_image_all (initial_frame )
77+ elif isinstance (notifier , WhatsappStrategy ):
78+ fileio_link = upload_to_fileio (initial_frame )
79+ notifier .notify_all (f"Here is the initial frame: { fileio_link } ." )
7080
7181 # Wait for the futures.
72- _ , failures = wait ([wifi_subject .thread , eye_subject .thread ])
82+ _ , failures = wait ([wifi_subject .thread , eye_subject .thread ], return_when = "FIRST_COMPLETED" )
7383 for failure in failures :
74- whatsapp_notifier .notify_all ("Home Security System has failed to run. "
75- "Please check the logs." )
76- whatsapp_notifier .notify_all ("Error: " + str (failure .exception ()))
77- whatsapp_notifier .notify_all ("Result: " + str (failure .result ()))
84+ notifier .notify_all ("Home Security System has failed to run. Please check the logs." )
85+ notifier . notify_all ( "Failure: " + str ( failure ) )
86+ notifier .notify_all ("Error: " + str (failure .exception ()))
87+ notifier .notify_all ("Result: " + str (failure .result ()))
7888 # Close the application to let systemd re-start it.
7989 sys .exit (1 )
8090
0 commit comments