-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_message_csv.py
54 lines (41 loc) · 1.92 KB
/
send_message_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -------------------------------------------------------------------------
# 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 asyncio
import uuid
import datetime
from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device import Message
messages_to_send = 10
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 = "IOTHUB_DEVICE_CONNECTION_STRING"
# The client object is used to interact with your Azure IoT hub.
device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
# Connect the client.
await device_client.connect()
async def send_test_message(i):
print("sending message #" + str(i))
message_id=uuid.uuid4()
csv_text=str(i)+"," + str(message_id) +",test wind speed " + str(i) + "," +datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
msg = Message(csv_text)
msg.message_id = message_id
msg.correlation_id = "correlation-1234"
msg.custom_properties["tornado-warning"] = "yes"
msg.content_encoding = "utf-8"
msg.content_type = "application/csv"
await device_client.send_message(msg)
print("done sending message #" + str(i))
# send `messages_to_send` messages in parallel
await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)])
# Finally, shut down the client
await device_client.shutdown()
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()