Skip to content

Commit 2835d7b

Browse files
author
Jan-Matthis
committed
Added a metric for the camera mode
1 parent 160464f commit 2835d7b

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

occameracontrol/camera.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from occameracontrol.agent import Agent
2828
from occameracontrol.metrics import register_camera_move, \
29-
register_camera_expectation
29+
register_camera_expectation, register_camera_status
3030

3131

3232
logger = 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)

occameracontrol/metrics.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
'camera_position_expected',
4646
'The position (preset number) a camera should be in',
4747
('camera',))
48+
camera_is_on = Gauge(
49+
'camera_status',
50+
'Whether the camera is on (True) or in standby (False)',
51+
('camera',))
4852

4953

5054
class RequestErrorHandler():
@@ -120,6 +124,16 @@ def register_camera_expectation(camera: str, position: int):
120124
camera_position_expected.labels(camera).set(position)
121125

122126

127+
def register_camera_status(camera: str, status: bool):
128+
'''Update metrics for the status of the camera. This ensures the (power)
129+
state of the camera is available as part of the metrics.
130+
131+
:param camera: Camera identified
132+
:param status: True if camera is 'on', False if 'standby'
133+
'''
134+
camera_is_on.labels(camera).set(status)
135+
136+
123137
def start_metrics_exporter():
124138
'''Start the web server for the metrics exporter endpoint if it is enabled
125139
in the configuration.

0 commit comments

Comments
 (0)