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:
Dan Smith 2016-02-14 17:06:46 +00:00
parent 263839a336
commit 0ba7fb40a4

View File

@ -58,6 +58,8 @@ class UnifiVideoCamera(Camera):
self._uuid = uuid self._uuid = uuid
self._name = name self._name = name
self.is_streaming = False self.is_streaming = False
self._connect_addr = None
self._camera = None
@property @property
def name(self): def name(self):
@ -68,31 +70,55 @@ class UnifiVideoCamera(Camera):
caminfo = self._nvr.get_camera(self._uuid) caminfo = self._nvr.get_camera(self._uuid)
return caminfo['recordingSettings']['fullTimeRecordEnabled'] return caminfo['recordingSettings']['fullTimeRecordEnabled']
def camera_image(self): def _login(self):
from uvcclient import camera as uvc_camera from uvcclient import camera as uvc_camera
caminfo = self._nvr.get_camera(self._uuid) caminfo = self._nvr.get_camera(self._uuid)
if self._connect_addr:
addrs = [self._connect_addr]
else:
addrs = [caminfo['host'], caminfo['internalHost']]
camera = None camera = None
for addr in [caminfo['host'], caminfo['internalHost']]: for addr in addrs:
try: try:
camera = uvc_camera.UVCCameraClient(addr, camera = uvc_camera.UVCCameraClient(addr,
caminfo['username'], caminfo['username'],
'ubnt') 'ubnt')
camera.login()
_LOGGER.debug('Logged into UVC camera %(name)s via %(addr)s', _LOGGER.debug('Logged into UVC camera %(name)s via %(addr)s',
dict(name=self._name, addr=addr)) dict(name=self._name, addr=addr))
self._connect_addr = addr
except socket.error: except socket.error:
pass pass
except uvc_camera.CameraConnectError: except uvc_camera.CameraConnectError:
pass pass
except uvc_camera.CameraAuthError: except uvc_camera.CameraAuthError:
pass pass
if not camera: if not camera:
_LOGGER.error('Unable to login to camera') _LOGGER.error('Unable to login to camera')
return None return None
try: self._camera = camera
camera.login() return True
return camera.get_snapshot()
except uvc_camera.CameraConnectError: def camera_image(self):
_LOGGER.error('Failed to connect to camera %s', self._name) 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()