-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python3 script that uploads data to IoTHub
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import argparse | ||
from azure.iot.device import IoTHubDeviceClient | ||
import time | ||
import os, fnmatch | ||
from datetime import datetime | ||
|
||
# specify/parse input parameters | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('dataFolder', help='folder containg CSV files to upload to Azure IoT hub') | ||
res = parser.parse_args() | ||
|
||
# prepare for upload | ||
print("Starting iothub client") | ||
iothub_connstring = "HostName=..." | ||
client = IoTHubDeviceClient.create_from_connection_string(iothub_connstring) | ||
client.connect() | ||
|
||
print("Reading files from " + os.path.abspath(res.dataFolder) + ":") | ||
|
||
#get a list of files from the folder, sorted alfabetically | ||
listOfFiles = sorted(os.listdir(res.dataFolder)) | ||
pattern = "*.csv" | ||
count = 0 | ||
|
||
for entry in listOfFiles: | ||
if fnmatch.fnmatch(entry, pattern): | ||
|
||
# read the file's contents | ||
filename = os.path.join( os.path.abspath(res.dataFolder), entry) | ||
file = open(filename, 'r') | ||
contents = file.read() | ||
|
||
# upload to azure IoT Hub | ||
client.send_message(contents) | ||
|
||
# close and delete file | ||
file.close() | ||
|
||
os.rename(filename, os.path.join(os.path.abspath(res.dataFolder), "uploaded_" + entry)) | ||
count = count + 1 | ||
|
||
print (str(count) + " - " + filename) | ||
|
||
|
||
print("Files uploaded: " + str(count)) | ||
|
||
client.disconnect() | ||
# meter um try-catch! |