コード例 #1
0
def test_instantiation_creates_proper_transport():
    connection_string = connection_string_format.format(
        fake_hostname, fake_device_id, fake_shared_access_key)
    authentication_provider = from_connection_string(connection_string)
    trans = MQTTTransport(authentication_provider)
    assert trans._auth_provider == authentication_provider
    assert trans._mqtt_provider is None
def test_sk_auth_provider_created_correctly_from_device_connection_string():
    connection_string = connection_string_device_sk_format.format(
        hostname, device_id, shared_access_key)
    auth_provider = from_connection_string(connection_string)
    assert isinstance(auth_provider, SymmetricKeyAuthenticationProvider)
    assert auth_provider.device_id == device_id
    assert auth_provider.hostname == hostname
コード例 #3
0
 def connect(self, transport, connection_string, ca_certificate):
     print("connecting using " + transport)
     self.auth_provider = from_connection_string(connection_string)
     if ca_certificate and "cert" in ca_certificate:
         self.auth_provider.ca_cert = ca_certificate["cert"]
     self.client = ModuleClient.from_authentication_provider(
         self.auth_provider, transport)
     object_list.append(self)
     self.client.connect()
コード例 #4
0
 def connect(self, transport_type, connection_string, ca_certificate):
     print("connecting using " + transport_type)
     auth_provider = from_connection_string(connection_string)
     if "GatewayHostName" in connection_string:
         auth_provider.ca_cert = ca_certificate.cert
     client = ModuleClient.from_authentication_provider(
         auth_provider, transport_type
     )
     return self._finish_connection(client)
コード例 #5
0
def module_transport():
    connection_string_mod = connection_string_format_mod.format(
        fake_hostname, fake_device_id, fake_module_id, fake_shared_access_key)
    authentication_provider = from_connection_string(connection_string_mod)

    with patch(
            "azure.iot.hub.devicesdk.transport.mqtt.mqtt_transport.MQTTProvider"
    ):
        transport = MQTTTransport(authentication_provider)
    transport.on_transport_connected = MagicMock()
    transport.on_transport_disconnected = MagicMock()
    yield transport
    transport.disconnect()
コード例 #6
0
def auth_provider(request):
    from azure.iot.hub.devicesdk.auth.authentication_provider_factory import (
        from_connection_string,
        from_shared_access_signature,
    )

    auth_type = request.param
    if auth_type == "SymmetricKey":
        return from_connection_string(
            connection_string_format.format(hostname, device_id,
                                            shared_access_key))
    elif auth_type == "SharedAccessSignature":
        uri = hostname + "/devices/" + device_id
        return from_shared_access_signature(
            sastoken_format.format(uri, signature, expiry))
コード例 #7
0
# --------------------------------------------------------------------------

import os
import json
import logging
import threading
from six.moves import input
from azure.iot.hub.devicesdk import DeviceClient
from azure.iot.hub.devicesdk.auth.authentication_provider_factory import from_connection_string

logging.basicConfig(level=logging.ERROR)

# The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
# The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client.
auth_provider = from_connection_string(conn_str)
# For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub.
# It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates
device_client = DeviceClient.from_authentication_provider(
    auth_provider, "mqtt")

# connect the client.
device_client.connect()


# define behavior for handling methods
def method1_listener(device_client):
    while True:
        method_request = device_client.receive_method(
            "method1")  # Wait for method1 calls
        payload = json.dumps({
コード例 #8
0
def authentication_provider(connection_string):
    auth_provider = from_connection_string(connection_string)
    return auth_provider
コード例 #9
0
def authentication_provider():
    connection_string = connection_string_format.format(
        fake_hostname, fake_device_id, fake_shared_access_key)
    auth_provider = from_connection_string(connection_string)
    return auth_provider
コード例 #10
0
async def main():
    # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
    conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
    # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client.
    auth_provider = from_connection_string(conn_str)
    # For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub.
    # It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates
    device_client = DeviceClient.from_authentication_provider(
        auth_provider, "mqtt")

    # connect the client.
    await device_client.connect()

    # define behavior for handling methods
    async def method1_listener(device_client):
        while True:
            method_request = await device_client.receive_method(
                "method1")  # Wait for method1 calls
            payload = json.dumps({
                "result": True,
                "data": "some data"
            })  # set response payload
            status = 200  # set return status code
            print("executed method1")
            await device_client.send_method_response(method_request, payload,
                                                     status)  # send response

    async def method2_listener(device_client):
        while True:
            method_request = await device_client.receive_method(
                "method2")  # Wait for method2 calls
            payload = json.dumps({
                "result": True,
                "data": 1234
            })  # set response payload
            status = 200  # set return status code
            print("executed method2")
            await device_client.send_method_response(method_request, payload,
                                                     status)  # send response

    async def generic_method_listener(device_client):
        while True:
            method_request = await device_client.receive_method(
            )  # Wait for unknown method calls
            payload = json.dumps({
                "result": False,
                "data": "unknown method"
            }  # set response payload
                                 )
            status = 400  # set return status code
            print("executed unknown method: " + method_request.name)
            await device_client.send_method_response(method_request, payload,
                                                     status)  # send response

    # define behavior for halting the application
    def stdin_listener():
        while True:
            selection = input("Press Q to quit\n")
            if selection == "Q" or selection == "q":
                print("Quitting...")
                break

    # Schedule tasks for Method Listener
    listeners = asyncio.gather(
        method1_listener(device_client),
        method2_listener(device_client),
        generic_method_listener(device_client),
    )

    # Run the stdin listener in the event loop
    loop = asyncio.get_running_loop()
    user_finished = loop.run_in_executor(None, stdin_listener)

    # Wait for user to indicate they are done listening for method calls
    await user_finished

    # Cancel listening
    listeners.cancel()

    # Finally, disconnect
    await device_client.disconnect()