2626
2727from occameracontrol .agent import Agent
2828from occameracontrol .metrics import register_camera_move , \
29- register_camera_expectation
29+ register_camera_expectation , register_camera_status
3030
3131
3232logger = logging .getLogger (__name__ )
@@ -75,7 +75,7 @@ def __str__(self) -> str:
7575 '''
7676 return f"'{ self .agent .agent_id } ' @ '{ self .url } '"
7777
78- def is_on (self ):
78+ def is_on (self ) -> bool :
7979 """Retrieve whether or not the camera is in Standby.
8080 For Panasonic camera AW-UE70:
8181 0 if Standby
@@ -92,7 +92,7 @@ def is_on(self):
9292
9393 -1 if Something went wrong
9494 """
95-
95+ state = False
9696 if self .type == CameraType .panasonic :
9797 url = f'{ self .url } /cgi-bin/aw_ptz'
9898 command = "#O"
@@ -102,8 +102,8 @@ def is_on(self):
102102 logger .debug ('GET %s with params: %s' , url , params )
103103 response = requests .get (url , auth = auth , params = params , timeout = 5 )
104104 response .raise_for_status ()
105- state = int (response .content .decode ().removeprefix ('p' ))
106- while state == 3 :
105+ state_int = int (response .content .decode ().removeprefix ('p' ))
106+ while state_int == 3 :
107107 # Escape the transition from standby to on
108108 time .sleep (3 )
109109 response = requests .get (
@@ -112,8 +112,7 @@ def is_on(self):
112112 params = params ,
113113 timeout = 5 )
114114 response .raise_for_status ()
115- state = int (response .content .decode ().removeprefix ('p' ))
116- return bool (state )
115+ state = bool (state_int )
117116
118117 if self .type == CameraType .sony :
119118 url = f'{ self .url } /command/inquiry.cgi'
@@ -129,14 +128,14 @@ def is_on(self):
129128 timeout = 5 )
130129 response .raise_for_status ()
131130 values = response .content .decode ().split ("&" )
132- state = False
133131 for v in values :
134132 if "Power" in v :
135133 if v .removeprefix ("Power=" )[1 ] == 'on' :
136- return True
134+ state = True
137135 else :
138- return False
139- return False
136+ state = False
137+ register_camera_status (self .url , state )
138+ return state
140139
141140 def set_power (self , turn_on = True ):
142141 """Activate the camera or put it into standby mode.
@@ -170,6 +169,8 @@ def set_power(self, turn_on=True):
170169 timeout = 5 )
171170 response .raise_for_status ()
172171
172+ self .is_on ()
173+
173174 def move_to_preset (self , preset : int ):
174175 '''Move the PTZ camera to the specified preset position
175176 '''
@@ -236,6 +237,7 @@ def update_position(self):
236237 logger .info ('[%s] Event `%s` started' , agent_id , event .title )
237238 logger .info ('[%s] Moving to preset %i' , agent_id ,
238239 self .preset_active )
240+ logger .debug ('[%s] Retrieving the camera state' , agent_id )
239241 if not self .is_on ():
240242 self .set_power ()
241243 time .sleep (10 )
@@ -244,15 +246,19 @@ def update_position(self):
244246 if self .position != self .preset_inactive :
245247 logger .info ('[%s] Returning to preset %i' , agent_id ,
246248 self .preset_inactive )
249+ logger .debug ('[%s] Retrieving the camera state' , agent_id )
247250 if not self .is_on ():
248251 self .set_power ()
249252 time .sleep (10 )
250253 self .move_to_preset (self .preset_inactive )
251-
254+ # Regular update
252255 if time .time () - self .last_updated >= self .update_frequency :
253256 logger .info ('[%s] Re-sending preset %i to camera' , agent_id ,
254257 self .position )
258+
259+ logger .debug ('[%s] Retrieving the camera state' , agent_id )
255260 if not self .is_on ():
256261 self .set_power ()
257262 time .sleep (10 )
263+ register_camera_status (self .url , self .is_on ())
258264 self .move_to_preset (self .position )
0 commit comments