-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module id for x509
- Loading branch information
Showing
9 changed files
with
309 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
azure-iot-device/samples/advanced-hub-scenarios/send_d2c_via_module_x509.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# ------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
|
||
import os | ||
import uuid | ||
from azure.iot.device.aio import IoTHubModuleClient | ||
from azure.iot.device.iothub import Message | ||
from azure.iot.device.common import X509 | ||
import logging | ||
import asyncio | ||
|
||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
messages_to_send = 10 | ||
|
||
|
||
async def main(): | ||
hostname = os.getenv("HOSTNAME") | ||
|
||
# The device having a certain module that has been created on the portal | ||
# using X509 CA signing or Self signing capabilities | ||
|
||
device_id = os.getenv("DEVICE_ID") | ||
module_id = os.getenv("MODULE_ID") | ||
|
||
x509 = X509( | ||
cert_file=os.getenv("X509_CERT_FILE"), | ||
key_file=os.getenv("X509_KEY_FILE"), | ||
pass_phrase=os.getenv("PASS_PHRASE"), | ||
) | ||
|
||
module_client = IoTHubModuleClient.create_from_x509_certificate( | ||
hostname=hostname, x509=x509, device_id=device_id, module_id=module_id | ||
) | ||
|
||
# Connect the client. | ||
await module_client.connect() | ||
|
||
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_d2c_message(msg) | ||
print("done sending message #" + str(i)) | ||
|
||
await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)]) | ||
|
||
# finally, disconnect | ||
await module_client.disconnect() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
|
||
# If using Python 3.6 or below, use the following code instead of asyncio.run(main()): | ||
# loop = asyncio.get_event_loop() | ||
# loop.run_until_complete(main()) | ||
# loop.close() |
57 changes: 57 additions & 0 deletions
57
azure-iot-device/samples/legacy-samples/send_d2c_via_module_x509.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
|
||
import os | ||
import time | ||
import uuid | ||
from azure.iot.device import IoTHubModuleClient, Message | ||
from azure.iot.device.common import X509 | ||
import logging | ||
|
||
|
||
logging.basicConfig(level=logging.ERROR) | ||
|
||
hostname = os.getenv("HOSTNAME") | ||
|
||
# The device having a certain module that has been created on the portal | ||
# using X509 CA signing or Self signing capabilities | ||
# The <device_id>\<module_id> should be the common name of the certificate | ||
|
||
device_id = os.getenv("DEVICE_ID") | ||
module_id = os.getenv("MODULE_ID") | ||
|
||
x509 = X509( | ||
cert_file=os.getenv("X509_CERT_FILE"), | ||
key_file=os.getenv("X509_KEY_FILE"), | ||
pass_phrase=os.getenv("PASS_PHRASE"), | ||
) | ||
|
||
module_client = IoTHubModuleClient.create_from_x509_certificate( | ||
hostname=hostname, x509=x509, device_id=device_id, module_id=module_id | ||
) | ||
|
||
module_client.connect() | ||
|
||
|
||
# send 5 messages with a 1 second pause between each message | ||
for i in range(1, 6): | ||
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" | ||
module_client.send_d2c_message(msg) | ||
time.sleep(1) | ||
|
||
# send only string messages | ||
for i in range(6, 11): | ||
print("sending message #" + str(i)) | ||
module_client.send_d2c_message("test payload message " + str(i)) | ||
time.sleep(1) | ||
|
||
|
||
# finally, disconnect | ||
module_client.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.