示例#1
0
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)
示例#2
0
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()