def send_request(dc, device_id, target, data=None): """ Sends a Device Request to the device with the given device ID using the given target and data. Args: dc (:class:`.DeviceCloud`): the Device Cloud instance. device_id (String): the device ID of the DRM device. target (String): the target of the Device Request. data (String, optional): the data of the Device Request. Returns: The Device Request response (if any). Raises: DeviceCloudHttpException: if there is any error sending the Device Request. """ # Generate the 'device_request' request. request = REQ_DEVICE_REQUEST.format( target, data if data is not None else "").strip() # Send the request and get the answer. resp = dc.sci.send_sci("data_service", DeviceTarget(device_id), request) # If the status is not 200, throw an exception. if resp.status_code != 200: raise DeviceCloudHttpException(resp) # Find and return the response (if any). re_search = re.search(REGEX_DEV_REQUEST_RESPONSE, resp.text, re.IGNORECASE) if re_search: return re_search.group(1) return None
def delete(self): """Delete this file from the device .. note:: After deleting the file, this object will no longer contain valid information and further calls to delete or get_data will return :class:`~.ErrorInfo` objects """ target = DeviceTarget(self.device_id) return self._fssapi.delete_file(target, self.path)[self.device_id]
def get_data(self): """Get the contents of this file :return: The contents of this file :rtype: six.binary_type """ target = DeviceTarget(self.device_id) return self._fssapi.get_file(target, self.path)[self.device_id]
def test_resp_parse(self, fake_send_sci): fake_resp = mock.MagicMock() fake_resp.status_code = 200 fake_resp.reason = "OK" fake_resp.content = EXAMPLE_ASYNC_SCI_RESPONSE fake_send_sci.return_value = fake_resp resp = self.dc.get_sci_api().send_sci_async( "send_message", DeviceTarget('00000000-00000000-00409dff-ffaabbcc'), EXAMPLE_SCI_REQUEST_PAYLOAD) self.assertEqual(resp.job_id, 133225503)
def test_bad_resp(self, fake_send_sci): fake_resp = mock.MagicMock() fake_resp.status_code = 400 fake_resp.reason = "OK" fake_resp.content = EXAMPLE_SCI_BAD_DEVICE fake_send_sci.return_value = fake_resp resp = self.dc.get_sci_api().send_sci_async( "send_message", DeviceTarget('00000000-00000000-00409dff-ffaabbcc'), EXAMPLE_SCI_REQUEST_PAYLOAD) self.assertIs(resp, None)
def send_set_drm_settings(dc, device_id, mo_value, df_value): """ Sends a 'set_setting' request to the device with the given device ID to set the remote manager's MO and DF settings. Args: dc (:class:`.DeviceCloud`): the Device Cloud instance. device_id (String): the device ID of the DRM device. mo_value (String): the value of the MO setting. df_value (String): the value of the DF setting. Returns: String: The error message if there was an error setting the DRM settings. `None` otherwise. Raises: DeviceCloudHttpException: if there is any error sending the request. """ # Generate the settings to set. settings = "" if mo_value != VALUE_UNDEFINED: settings += REQ_SETTING_MO.format(mo_value) if df_value != VALUE_UNDEFINED: settings += REQ_SETTING_DF.format(df_value) # Generate the 'set_setting' request with the DRM settings. request = REQ_SET_DRM_SETTINGS.format(settings) # Send the request and get the answer. resp = dc.sci.send_sci(SCI_SEND_MESSAGE, DeviceTarget(device_id), request, cache=False) # If the status is not 200, throw an exception. if resp.status_code != 200: raise DeviceCloudHttpException(resp) # Find and return the error (if any). re_search = re.search(REGEX_ERROR, resp.text, re.IGNORECASE) if re_search: error_msg = "" re_search = re.search(REGEX_ERROR_TITLE, resp.text, re.IGNORECASE) if re_search: error_msg = re_search.group(1) re_search = re.search(REGEX_ERROR_HINT, resp.text, re.IGNORECASE) if re_search: error_msg += ": %s" % re_search.group(1) return error_msg return None
def send_query_setting(dc, device_id, settings_group): """ Sends a 'query_setting' request to the device with the given device ID to get all the settings from the provided settings group. Args: dc (:class:`.DeviceCloud`): the Device Cloud instance. device_id (String): the device ID of the DRM device. settings_group (String): the name of the settings group to get. Returns: String: The 'query_setting' XML response (if any). Raises: DeviceCloudHttpException: if there is any error sending the request. """ # Generate the 'query_setting' request. request = REQ_QUERY_SETTING.format(settings_group) # Send the request and get the answer. resp = dc.sci.send_sci(SCI_SEND_MESSAGE, DeviceTarget(device_id), request, cache=True) # If the status is not 200, throw an exception. if resp.status_code != 200: raise DeviceCloudHttpException(resp) # Find and return the error (if any). re_search = re.search(REGEX_ERROR, resp.text, re.IGNORECASE) if re_search: error_msg = "" re_search = re.search(REGEX_ERROR_TITLE, resp.text, re.IGNORECASE) if re_search: error_msg = re_search.group(1) re_search = re.search(REGEX_ERROR_HINT, resp.text, re.IGNORECASE) if re_search: error_msg += ": %s" % re_search.group(1) return error_msg # Find and return the response (if any). re_search = re.search(REGEX_QUERY_SETTING_RESPONSE, resp.text, re.IGNORECASE) if re_search: return re_search.group(1) return None
def test_sci_successful_error(self): self._prepare_sci_response(EXAMPLE_SCI_DEVICE_NOT_CONNECTED) self.dc.get_sci_api().send_sci( operation="send_message", target=DeviceTarget("00000000-00000000-00409DFF-FF58175B"), payload="<reset/>") self.assertEqual( httpretty.last_request().body, six.b('<sci_request version="1.0">' '<send_message>' '<targets>' '<device id="00000000-00000000-00409DFF-FF58175B"/>' '</targets>' '<reset/>' '</send_message>' '</sci_request>'))
def test_sci_with_parameters(self): self._prepare_sci_response(EXAMPLE_SCI_REQUEST_RESPONSE) self.dc.get_sci_api().send_sci( operation="send_message", target=DeviceTarget('00000000-00000000-00409dff-ffaabbcc'), payload=EXAMPLE_SCI_REQUEST_PAYLOAD, reply="all", synchronous=True, sync_timeout=42, cache=False, allow_offline=True, wait_for_reconnect=True, ) request = httpretty.last_request().body.decode('utf8') # Verify attributes exist in <send_message> expected_attrib = { "reply": "all", "synchronous": "true", "syncTimeout": "42", "cache": "false", "allowOffline": "true", "waitForReconnect": "true", } request_e = ET.fromstring(request) send_message_e = request_e.find('./send_message') self.assertEqual(expected_attrib, send_message_e.attrib) # Strip white space from lines and concatenate request request = ''.join([line.strip() for line in request.splitlines()]) # Replace <send_message> from request with one without parameters so the final check can be done match = re.search('<send_message.*?>', request) request = request[:match.start()] + '<send_message>' + request[match. end():] self.assertEqual( request, six.u('<sci_request version="1.0">' '<send_message>' '<targets>' '<device id="00000000-00000000-00409dff-ffaabbcc"/>' '</targets>' '<rci_request version="1.1">' '<query_state>' '<device_stats/>' '</query_state>' '</rci_request>' '</send_message>' '</sci_request>'))
def test_sci_update_firmware_attribute(self): self._prepare_sci_response( EXAMPLE_UPDATE_FIRMWARE_INVALID_ATTRIBUTE_RESPONSE) self.dc.get_sci_api().send_sci( operation="update_firmware", attribute="filename=\"abcd.bin\"", target=DeviceTarget('00000000-00000000-00409dff-ffaabbcc'), payload=EXAMPLE_UPDATE_FIRMWARE_INVALID_ATTRIBUTE_REQUEST_PAYLOAD) request = httpretty.last_request().body.decode('utf8') request = ''.join([line.strip() for line in request.splitlines()]) self.assertEqual( request, six.u( '<sci_request version="1.0">' '<update_firmware filename="abcd.bin">' '<targets>' '<device id="00000000-00000000-00409dff-ffaabbcc"/>' '</targets>' '<data>aHNxcAbAADUct1cAAACAHEBAAAEABEAwAIBAAQAAACOFFzU</data>' '</update_firmware>' '</sci_request>'))
def test_sci_no_parameters(self): self._prepare_sci_response(EXAMPLE_SCI_REQUEST_RESPONSE) self.dc.get_sci_api().send_sci( operation="send_message", target=DeviceTarget('00000000-00000000-00409dff-ffaabbcc'), payload=EXAMPLE_SCI_REQUEST_PAYLOAD) request = httpretty.last_request().body.decode('utf8') # Strip white space from lines and concatenate request request = ''.join([line.strip() for line in request.splitlines()]) self.assertEqual( request, six.u('<sci_request version="1.0">' '<send_message>' '<targets>' '<device id="00000000-00000000-00409dff-ffaabbcc"/>' '</targets>' '<rci_request version="1.1">' '<query_state>' '<device_stats/>' '</query_state>' '</rci_request>' '</send_message>' '</sci_request>'))
def send_do_command(dc, device_id, target, data=None): """ Sends a 'do_command' request to the device with the given device ID using the given data. Args: dc (:class:`.DeviceCloud`): the Device Cloud instance. device_id (String): the device ID of the DRM device. target (String): the 'do_command' target. data (String, optional): the data of the 'do_command' request. Returns: String: The 'do_command' XML response (if any). Raises: DeviceCloudHttpException: if there is any error sending the request. """ # Generate the 'do_command' request. request = REQ_DO_COMMAND.format(target, data if data is not None else "") # Send the request and get the answer. Set cache to False in order to get # the answer from the device and not from DRM. resp = dc.sci.send_sci(SCI_SEND_MESSAGE, DeviceTarget(device_id), request, cache=False) # If the status is not 200, throw an exception. if resp.status_code != 200: raise DeviceCloudHttpException(resp) # Find and return the response (if any). re_search = re.search(REGEX_DO_CMD_RESPONSE, resp.text, re.IGNORECASE) if re_search: return re_search.group(1) return None
def list_contents(self): """List the contents of this directory :return: A LsInfo object that contains directories and files :rtype: :class:`~.LsInfo` or :class:`~.ErrorInfo` Here is an example usage:: # let dirinfo be a DirectoryInfo object ldata = dirinfo.list_contents() if isinstance(ldata, ErrorInfo): # Do some error handling logger.warn("Error listing file info: (%s) %s", ldata.errno, ldata.message) # It's of type LsInfo else: # Look at all the files for finfo in ldata.files: logger.info("Found file %s of size %s", finfo.path, finfo.size) # Look at all the directories for dinfo in ldata.directories: logger.info("Found directory %s of last modified %s", dinfo.path, dinfo.last_modified) """ target = DeviceTarget(self.device_id) return self._fssapi.list_files(target, self.path)[self.device_id]
offset=4, truncate=True) print(fssapi.get_file(target, tmp_file)) delete_test_file(fssapi, target, tmp_file) delete_test_file(fssapi, target, tmp_server_device_file) print("\nList of files in {}".format(base_dir)) out_dict = fssapi.list_files(target, base_dir) print(out_dict) if __name__ == "__main__": dc = get_authenticated_dc() device_id = "your-device-id-here" target = DeviceTarget(device_id) base_dir = '/a/directory/on/your/device' use_filesystem(dc, target, base_dir) fssapi = dc.get_fss_api() tmp_file_path = "{}/{}".format(base_dir, 'test_file.txt') put_test_file(fssapi, target, tmp_file_path) cutoff_time = time.time() get_modified_files(fssapi, target, base_dir, cutoff_time) print("\nModifying file {}".format(tmp_file_path)) fssapi.put_file(target, tmp_file_path, file_data=six.b("data"), offset=4) time.sleep(5) get_modified_files(fssapi, target, base_dir, cutoff_time) delete_test_file(fssapi, target, tmp_file_path) command_block = FileSystemServiceCommandBlock()