Files
cookie-cutter-driver/POCloud-CookieCutter-master/hooks/post_gen_project.py
Nico Melone 6a7cfe2f1b updated path
2021-08-12 11:05:44 -05:00

108 lines
3.4 KiB
Python

import csv
import os
pwd = os.getcwd()
#Generate a tag/channel list to be used in the template
csv_file = bool(input("Do you have a CSV file yes(1)/no(0): "))
if not csv_file:
num_channels = int(input("Number of Channels in the Driver: "))
mesh_name = []
tags = []
data_type = []
change_thres = []
guaruntee_sec = []
register_num = []
unit_number = []
channel_size = []
if csv_file:
csv_path = str(input("Where is your CSV file: "))
plc_or_mbs = int(input("Is this for PLC(1) or Modbus(2) channels: "))
if csv_file:
with open(csv_path, 'r') as tagsFile:
reader = csv.reader(tagsFile)
row_count = sum(1 for row in reader)
with open(csv_path, 'r') as tagsFile:
reader = csv.reader(tagsFile)
try:
f = open("Tags.py", "a")
print("writing tags")
f.write("tags = [ \n")
counter = 1
for row in reader:
print("{}".format(row[0]))
if plc_or_mbs == 1:
f.write("\tPLCChannel(PLC_IP_ADDRESS, \"{}\",\"{}\",\"{}\", {}, {}, plc_type=\"{}\")".format(row[0],row[1],row[2],row[3],row[4],"{{cookiecutter.device_type}}"))
else:
f.write("\tModbusChannel(\"{}\",{},\"{}\", {}, {},channel_size={}, unit_number={})".format(row[0],row[1],row[2],row[3],row[4], row[5], row[6]))
if counter < row_count:
f.write(",\n")
else:
f.write("\n]")
counter = counter + 1
f.close
print("Wrote {} Channels".format(row_count))
except:
print("Couldn't open file with tags")
else:
for x in range(num_channels):
mesh_name.append(input("Channel {} meshify name: ".format(x+1)))
if plc_or_mbs == 1:
tags.append(input("Channel {} tag name: ".format(x+1)))
data_type.append(input("Channel {} data type: ".format(x+1)))
change_thres.append(input("Channel {} change threshold: ".format(x+1)))
guaruntee_sec.append(input("Channel {} guarunteed seconds: ".format(x+1)))
if plc_or_mbs == 2:
register_num.append(input("Channel {} register number: ".format(x+1)))
unit_number.append(input("Channel {} unit number: ".format(x+1)))
channel_size.append(input("Channel {} channel size: ".format(x+1)))
#Generate a Tags.py file to hold a list of PLCChannels or ModbusChannels to be used in the driver to communicate
if(num_channels == 0):
try:
file = open("Tags.py", "a")
file.write("tags = []")
file.close
print("Wrote no Channels")
except:
print("Couldn't open file without tags")
elif plc_or_mbs == 1:
try:
file = open("Tags.py", "a")
file.write("tags = [ \n")
for x in range(num_channels):
file.write("\tPLCChannel(PLC_IP_ADDRESS, \"{}\",\"{}\",\"{}\", {}, {}, plc_type=\"{}\")".format(mesh_name[x],tags[x],data_type[x],change_thres[x],guaruntee_sec[x],"{{cookiecutter.device_type}}"))
if(x < num_channels-1):
file.write(",\n")
else:
file.write("\n]")
file.close
print("Wrote {} Channels".format(num_channels))
except:
print("Couldn't open file with tags")
else:
try:
print("Opening 3 {{cookiecutter.driver_name}}/Tags.py")
file = open("Tags.py", "a")
file.write("tags = [ \n")
for x in range(num_channels):
file.write("\tModbusChannel(\"{}\",{},\"{}\", {}, {},channel_size={}, unit_number={})".format(mesh_name[x],register_num[x],data_type[x],change_thres[x],guaruntee_sec[x],channel_size[x],unit_number[x]))
if(x < num_channels-1):
file.write(",\n")
else:
file.write("\n]")
file.close
print("Wrote {} Channels".format(num_channels))
except:
print("Couldn't open file with tags")