def test_login_v310(self, mock_h): c = camera.UVCCameraClient('foo', 'ubnt', 'ubnt') first = mock.MagicMock() second = mock.MagicMock() counter = [0] def fake_conn(*a, **k): if counter[0] == 0: counter[0] += 1 return first elif counter[0] == 1: counter[0] += 1 return second mock_h.side_effect = fake_conn cookie = 'thecookie AIROS_SESSIONID=foo; bar' first.getresponse.return_value.getheaders.return_value = [ ('Set-Cookie', cookie) ] second.getresponse.return_value.status = 200 c.login() first.request.assert_called_once_with('GET', '/') self.assertEqual('POST', second.request.call_args_list[0][0][0]) self.assertEqual('/login.cgi', second.request.call_args_list[0][0][1]) formdata = 'AIROS_SESSIONID=foo&password=ubnt&username=ubnt' self.assertEqual( sorted(formdata.split('&')), sorted(second.request.call_args_list[0][0][2].split('&'))) self.assertEqual(cookie, second.request.call_args_list[0][0][3]['Cookie'])
def test_get_snapshot(self): c = camera.UVCCameraClient('foo', 'ubnt', 'ubnt') with mock.patch.object(httplib, 'HTTPConnection') as mock_conn: conn = mock_conn.return_value conn.getresponse.return_value.status = 200 r = c.get_snapshot() self.assertEquals(conn.getresponse.return_value.read.return_value, r)
def test_cfgwrite(self): c = camera.UVCCameraClient('foo', 'ubnt', 'ubnt') c._cookie = 'foo-cookie' with mock.patch.object(httplib, 'HTTPConnection') as mock_h: conn = mock_h.return_value conn.getresponse.return_value.status = 200 self.assertTrue(c._cfgwrite('foo', 'bar')) headers = {'Cookie': 'foo-cookie'} conn.request.assert_called_once_with('GET', '/cfgwrite.cgi?foo=bar', headers=headers)
def do_snapshot(client, camera_info): password = INFO_STORE.get_camera_password(camera_info['uuid']) or 'ubnt' if client.server_version >= (3, 2, 0): cam_client = camera.UVCCameraClientV320(camera_info['host'], camera_info['username'], password) else: cam_client = camera.UVCCameraClient(camera_info['host'], camera_info['username'], password) try: cam_client.login() return cam_client.get_snapshot() except (camera.CameraAuthError, camera.CameraConnectError): # Fall back to proxy through the NVR return client.get_snapshot(camera_info['uuid'])
def do_reboot(client, camera_info): password = INFO_STORE.get_camera_password(camera_info['uuid']) or 'ubnt' if client.server_version >= (3, 2, 0): cam_client = camera.UVCCameraClientV320(camera_info['host'], camera_info['username'], password) else: cam_client = camera.UVCCameraClient(camera_info['host'], camera_info['username'], password) try: cam_client.login() return cam_client.reboot() except camera.CameraAuthError: print('Failed to login to camera') except camera.CameraConnectError: print('Failed to connect to camera') except Exception as e: print('Failed to reboot: %s' % e)
def camera_image(self): from uvcclient import camera as uvc_camera caminfo = self._nvr.get_camera(self._uuid) camera = None for addr in [caminfo['host'], caminfo['internalHost']]: try: camera = uvc_camera.UVCCameraClient(addr, caminfo['username'], 'ubnt') _LOGGER.debug('Logged into UVC camera %(name)s via %(addr)s', dict(name=self._name, addr=addr)) except socket.error: pass if not camera: _LOGGER.error('Unable to login to camera') return None camera.login() return camera.get_snapshot()
def _login(self): """Login to the camera.""" from uvcclient import camera as uvc_camera from uvcclient import store as uvc_store caminfo = self._nvr.get_camera(self._uuid) if self._connect_addr: addrs = [self._connect_addr] else: addrs = [caminfo['host'], caminfo['internalHost']] store = uvc_store.get_info_store() password = store.get_camera_password(self._uuid) if password is None: _LOGGER.debug('Logging into camera %(name)s with default password', dict(name=self._name)) password = '******' camera = None for addr in addrs: try: camera = uvc_camera.UVCCameraClient(addr, caminfo['username'], password) camera.login() _LOGGER.debug('Logged into UVC camera %(name)s via %(addr)s', dict(name=self._name, addr=addr)) self._connect_addr = addr break except socket.error: pass except uvc_camera.CameraConnectError: pass except uvc_camera.CameraAuthError: pass if not self._connect_addr: _LOGGER.error('Unable to login to camera') return None self._camera = camera return True
def do_led(camera_info, enabled): password = INFO_STORE.get_camera_password(camera_info['uuid']) or 'ubnt' cam_client = camera.UVCCameraClient(camera_info['host'], camera_info['username'], password) cam_client.login() cam_client.set_led(enabled)
def test_set_led_off(self): c = camera.UVCCameraClient('foo', 'ubnt', 'ubnt') with mock.patch.object(c, '_cfgwrite') as mock_write: c.set_led(True) mock_write.assert_called_once_with('led.front.status', 1)