Adds ability to add channels via CSV

This commit is contained in:
Patrick McDonagh
2017-07-18 10:13:55 -05:00
parent f75a051faf
commit fd2507f5e4
2 changed files with 47 additions and 0 deletions

View File

@@ -27,3 +27,12 @@ def query_meshify_api(endpoint):
q_url = MESHIFY_BASE_URL + endpoint
q_req = requests.get(q_url, auth=MESHIFY_AUTH)
return json.loads(q_req.text) if q_req.status_code == 200 else []
def post_meshify_api(endpoint, data):
"""Post data to the meshify API."""
q_url = MESHIFY_BASE_URL + endpoint
q_req = requests.post(q_url, data=json.dumps(data), auth=MESHIFY_AUTH)
if q_req.status_code != 200:
print(q_req.status_code)
return json.loads(q_req.text) if q_req.status_code == 200 else []

38
postChannels.py Normal file
View File

@@ -0,0 +1,38 @@
"""Read a CSV file of channels and post them to Meshify via the API."""
import csv
import meshify
import sys
def main(csv_file, devicetype):
"""Main function."""
csvfile = open(csv_file, 'rU')
reader = csv.DictReader(csvfile, dialect=csv.excel)
channels = []
idx = 0
for x in reader:
channels.append(x)
channels[idx]["fromMe"] = False
channels[idx]["regex"] = ""
channels[idx]["regexErrMsg"] = ""
channels[idx]["dataType"] = int(channels[idx]["dataType"])
channels[idx]["deviceTypeId"] = int(channels[idx]["deviceTypeId"])
channels[idx]["channelType"] = int(channels[idx]["channelType"])
channels[idx]["io"] = bool(channels[idx]["io"])
idx += 1
try:
this_devicetype = meshify.find_by_name(devicetype, meshify.query_meshify_api("devicetypes"))
for c in channels:
print(meshify.post_meshify_api("devicetypes/{}/channels".format(this_devicetype['id']), c))
except KeyError:
print("Could not find key {}".format(devicetype))
if __name__ == '__main__':
if len(sys.argv) == 3:
main(sys.argv[1], sys.argv[2])
else:
print("Syntax is python postChannels.py <filepath.csv> <devicetype name>")