mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Improve UVC performance by not logging in on each image fetch
This makes the UVC camera_image() method not log into the camera on every single image fetch, which reduces load on hass and the camera, and lowers the latency to get an actual image fetched.
This commit is contained in:
parent
263839a336
commit
0ba7fb40a4
@ -58,6 +58,8 @@ class UnifiVideoCamera(Camera):
|
||||
self._uuid = uuid
|
||||
self._name = name
|
||||
self.is_streaming = False
|
||||
self._connect_addr = None
|
||||
self._camera = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@ -68,31 +70,55 @@ class UnifiVideoCamera(Camera):
|
||||
caminfo = self._nvr.get_camera(self._uuid)
|
||||
return caminfo['recordingSettings']['fullTimeRecordEnabled']
|
||||
|
||||
def camera_image(self):
|
||||
def _login(self):
|
||||
from uvcclient import camera as uvc_camera
|
||||
|
||||
caminfo = self._nvr.get_camera(self._uuid)
|
||||
if self._connect_addr:
|
||||
addrs = [self._connect_addr]
|
||||
else:
|
||||
addrs = [caminfo['host'], caminfo['internalHost']]
|
||||
|
||||
camera = None
|
||||
for addr in [caminfo['host'], caminfo['internalHost']]:
|
||||
for addr in addrs:
|
||||
try:
|
||||
camera = uvc_camera.UVCCameraClient(addr,
|
||||
caminfo['username'],
|
||||
'ubnt')
|
||||
camera.login()
|
||||
_LOGGER.debug('Logged into UVC camera %(name)s via %(addr)s',
|
||||
dict(name=self._name, addr=addr))
|
||||
self._connect_addr = addr
|
||||
except socket.error:
|
||||
pass
|
||||
except uvc_camera.CameraConnectError:
|
||||
pass
|
||||
except uvc_camera.CameraAuthError:
|
||||
pass
|
||||
|
||||
if not camera:
|
||||
_LOGGER.error('Unable to login to camera')
|
||||
return None
|
||||
|
||||
try:
|
||||
camera.login()
|
||||
return camera.get_snapshot()
|
||||
except uvc_camera.CameraConnectError:
|
||||
_LOGGER.error('Failed to connect to camera %s', self._name)
|
||||
self._camera = camera
|
||||
return True
|
||||
|
||||
def camera_image(self):
|
||||
from uvcclient import camera as uvc_camera
|
||||
if not self._camera:
|
||||
if not self._login():
|
||||
return
|
||||
|
||||
def _get_image(retry=True):
|
||||
try:
|
||||
return self._camera.get_snapshot()
|
||||
except uvc_camera.CameraConnectError:
|
||||
_LOGGER.error('Unable to contact camera')
|
||||
except uvc_camera.CameraAuthError:
|
||||
if retry:
|
||||
self._login()
|
||||
return _get_image(retry=False)
|
||||
else:
|
||||
_LOGGER.error('Unable to log into camera, unable '
|
||||
'to get snapshot')
|
||||
raise
|
||||
|
||||
return _get_image()
|
||||
|
Loading…
x
Reference in New Issue
Block a user