class ServiceApi(AbstractServiceApi): def __init__(self): global object_list object_list.append(self) self.cn = None self.service = None self.service_connection_string = None self.amqp_service_client = None self.pool = ThreadPool() def __del__(self): self.pool.close() def headers(self): return { "Authorization": self.cn["sas"], "Request-Id": str(uuid.uuid4()), "User-Agent": "azure-edge-e2e", } def connect(self, service_connection_string): self.service_connection_string = service_connection_string self.cn = connection_string.connection_string_to_sas_token( service_connection_string) self.service = IotHubGatewayServiceAPIs("https://" + self.cn["host"]).service def disconnect(self): if self.amqp_service_client: self.amqp_service_client.disconnect() self.amqp_serice_client = None self.cn = None self.service = None def call_module_method_async(self, device_id, module_id, method_invoke_parameters): def thread_proc(): return self.service.invoke_device_method1( device_id, module_id, method_invoke_parameters, custom_headers=self.headers(), ).as_dict() return self.pool.apply_async(thread_proc) def call_device_method_async(self, device_id, method_invoke_parameters): def thread_proc(): return self.service.invoke_device_method( device_id, method_invoke_parameters, custom_headers=self.headers()).as_dict() return self.pool.apply_async(thread_proc) def send_c2d(self, device_id, message): if not self.amqp_service_client: self.amqp_service_client = AmqpServiceClient() self.amqp_service_client.connect(self.service_connection_string) self.amqp_service_client.send_to_device(device_id, message)
class RegistryApi: def __init__(self): global object_list object_list.append(self) self.cn = None self.service = None def headers(self): return { "Authorization": self.cn["sas"], "Request-Id": str(uuid.uuid4()), "User-Agent": "azure-edge-e2e", } def connect(self, service_connection_string): self.cn = connection_string.connection_string_to_sas_token( service_connection_string ) self.service = IotHubGatewayServiceAPIs("https://" + self.cn["host"]).service def disconnect(self): pass def get_module_twin(self, device_id, module_id): return self.service.get_module_twin( device_id, module_id, custom_headers=self.headers() ).as_dict() def patch_module_twin(self, device_id, module_id, patch): twin = models.Twin.from_dict(patch) self.service.update_module_twin( device_id, module_id, twin, custom_headers=self.headers() )
class ServiceApi(AbstractServiceApi): def __init__(self): self.service = None self.service_connection_string = None self.amqp_service_client = None def headers(self): cn = connection_string.connection_string_to_sas_token( self.service_connection_string) return { "Authorization": cn["sas"], "Request-Id": str(uuid.uuid4()), "User-Agent": "azure-edge-e2e", } async def connect(self, service_connection_string): self.service_connection_string = service_connection_string host = connection_string.connection_string_to_dictionary( service_connection_string)["HostName"] self.service = IotHubGatewayServiceAPIs("https://" + host).service async def disconnect(self): if self.amqp_service_client: await self.amqp_service_client.disconnect() self.amqp_serice_client = None self.service = None @emulate_async def call_module_method(self, device_id, module_id, method_invoke_parameters): return self.service.invoke_device_method1( device_id, module_id, method_invoke_parameters, custom_headers=self.headers(), ).as_dict() @emulate_async def call_device_method(self, device_id, method_invoke_parameters): return self.service.invoke_device_method( device_id, method_invoke_parameters, custom_headers=self.headers()).as_dict() async def send_c2d(self, device_id, message): if not self.amqp_service_client: self.amqp_service_client = AmqpServiceClient() await self.amqp_service_client.connect( self.service_connection_string) await self.amqp_service_client.send_to_device(device_id, message) async def get_blob_upload_status(self): if not self.amqp_service_client: self.amqp_service_client = AmqpServiceClient() await self.amqp_service_client.connect( self.service_connection_string) return await self.amqp_service_client.get_next_blob_status()
class RegistryApi(AbstractRegistryApi): def __init__(self): global object_list object_list.append(self) self.service = None self.service_connection_string = None def headers(self): cn = connection_string.connection_string_to_sas_token( self.service_connection_string) return { "Authorization": cn["sas"], "Request-Id": str(uuid.uuid4()), "User-Agent": "azure-edge-e2e", } def connect_sync(self, service_connection_string): self.service_connection_string = service_connection_string host = connection_string.connection_string_to_dictionary( service_connection_string)["HostName"] self.service = IotHubGatewayServiceAPIs("https://" + host).service def disconnect_sync(self): pass @emulate_async def get_module_twin(self, device_id, module_id): return self.service.get_module_twin( device_id, module_id, custom_headers=self.headers()).as_dict()["properties"] @emulate_async def patch_module_twin(self, device_id, module_id, patch): twin = models.Twin.from_dict({"properties": patch}) self.service.update_module_twin(device_id, module_id, twin, custom_headers=self.headers()) @emulate_async def get_device_twin(self, device_id): return self.service.get_twin( device_id, custom_headers=self.headers()).as_dict()["properties"] @emulate_async def patch_device_twin(self, device_id, patch): twin = models.Twin.from_dict({"properties": patch}) self.service.update_twin(device_id, twin, custom_headers=self.headers())
def connect_sync(self, service_connection_string): self.service_connection_string = service_connection_string host = connection_string.connection_string_to_dictionary( service_connection_string)["HostName"] self.service = IotHubGatewayServiceAPIs("https://" + host).service
def connect_sync(self, service_connection_string): self.cn = connection_string.connection_string_to_sas_token( service_connection_string) self.service = IotHubGatewayServiceAPIs("https://" + self.cn["host"]).service