Exemple #1
0
async def main():
    # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the module client.
    auth_provider = auth.from_environment()
    # For now, the SDK only supports MQTT as a protocol.
    # Inputs/Ouputs are only supported in the context of Azure IoT Edge and module client
    # The module client object acts as an Azure IoT Edge module and interacts with an Azure IoT Edge hub
    # It needs an Authentication Provider to secure the communication with the Edge hub.
    # This authentication provider is created from environment & delegates token generation to iotedged.
    module_client = ModuleClient.from_authentication_provider(
        auth_provider, "mqtt")

    # Connect the client.
    await module_client.connect()

    # Send a filled out Message object
    async def send_test_message(i):
        print("sending message #" + str(i))
        msg = Message("test wind speed " + str(i))
        msg.message_id = uuid.uuid4()
        msg.correlation_id = "correlation-1234"
        msg.custom_properties["tornado-warning"] = "yes"
        await module_client.send_to_output(msg, "twister")
        print("done sending message #" + str(i))

    await asyncio.gather(
        *[send_test_message(i) for i in range(1, messages_to_send)])

    # finally, disconnect
    module_client.disconnect()
async def create_environ_auth_provider():
    """
    This creates an authentication provider from the system's environment variables.
    """
    env_auth_provider = auth.from_environment()
    device_client_env = DeviceClient.from_authentication_provider(
        env_auth_provider, "mqtt")
    print("Authenticating from system environment...")
    await device_client_env.connect()
    print("Successfully authenticated!")
async def main():
    # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client.
    auth_provider = auth.from_environment()
    # 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
    module_client = ModuleClient.from_authentication_provider(
        auth_provider, "mqtt")

    # connect the client.
    await module_client.connect()

    # define behavior for receiving an input message on input1
    async def input1_listener(module_client):
        while True:
            input_message = await module_client.receive_input_message(
                "input1")  # blocking call
            print("the data in the message received on input1 was ")
            print(input_message.data)
            print("custom properties are")
            print(input_message.custom_properties)

    # define behavior for receiving an input message on input2
    async def input2_listener(module_client):
        while True:
            input_message = await module_client.receive_input_message(
                "input2")  # blocking call
            print("the data in the message received on input2 was ")
            print(input_message.data)
            print("custom properties are")
            print(input_message.custom_properties)

    # 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 task for C2D Listener
    listeners = asyncio.gather(input1_listener(module_client),
                               input2_listener(module_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 messages
    await user_finished

    # Cancel listening
    listeners.cancel()

    # Finally, disconnect
    await module_client.disconnect()
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import time
import uuid
from azure.iot.hub.devicesdk import ModuleClient, Message
from azure.iot.hub.devicesdk import auth

# The "Authentication Provider" is the object in charge of creating authentication "tokens" for the module client.
auth_provider = auth.from_environment()
# For now, the SDK only supports MQTT as a protocol.
# Inputs/Ouputs are only supported in the context of Azure IoT Edge and module client
# The module client object acts as an Azure IoT Edge module and interacts with an Azure IoT Edge hub
# It needs an Authentication Provider to secure the communication with the Edge hub.
# This authentication provider is created from environment & delegates token generation to iotedged.
module_client = ModuleClient.from_authentication_provider(
    auth_provider, "mqtt")

# Connect the client.
module_client.connect()

# send 5 messages with a 1 second pause between each message
for i in range(0, 5):
    print("sending message #" + str(i))
    msg = Message("test wind speed " + str(i))
    msg.message_id = uuid.uuid4()
    msg.correlation_id = "correlation-1234"
    msg.custom_properties["tornado-warning"] = "yes"