Go to the documentation of this file.00001
00002
00003 import sys, getopt
00004 import csv
00005
00006 def main(argv):
00007 csvFile = 'BMS_MLVDS_register_map_oct28.csv'
00008 xmlFile = 'BmsSchedule.xml'
00009 try:
00010 opts, args = getopt.getopt(argv, "hi:o:",["input=", "output="])
00011 except getopt.GetoptError:
00012 print("BMS_MLVDS_register_map_to_schedule.py -i <inputfile> -o <outputfile>")
00013 sys.exit(2)
00014 for opt, arg in opts:
00015 if opt == '-h':
00016 print 'BMS_MLVDS_register_map_to_schedule.py -i <inputfile> -o <outputfile>'
00017 sys.exit()
00018 elif opt in ("-i", "--input"):
00019 csvFile = arg
00020 elif opt in ("-o", "--output"):
00021 xmlFile = arg
00022
00023 print("Opening " + csvFile)
00024 data = csv.reader(open(csvFile))
00025
00026 print("Writing to " + xmlFile)
00027 xmlData = open(xmlFile, 'w')
00028
00029 xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00030 xmlData.write('<ScheduleData>' + "\n")
00031 xmlData.write(' <Properties>' + "\n")
00032 xmlData.write(' The schedule for the Battery Management System' + "\n")
00033 xmlData.write(' </Properties>' + "\n")
00034 xmlData.write(' <Elements>' + "\n")
00035 rowNum = 0
00036 for row in data:
00037 if rowNum == 0:
00038 tags = row
00039
00040 for i in range(len(tags)):
00041 tags[i] = tags[i].strip().replace(' ', '_')
00042 if("name" in tags):
00043 iName = tags.index("name")
00044 print("found name tag at index: " + str(iName))
00045 else:
00046 print("can not find name tag")
00047 break
00048 if("data_type" in tags):
00049 iDataType = tags.index("data_type")
00050 print("found datatype tag at index: " + str(iDataType))
00051 else:
00052 print("can not find datatype tag")
00053 break
00054 if("ROBONET_type" in tags):
00055 iDirection = tags.index("ROBONET_type")
00056 print("found ROBONET_type tag at index: " + str(iDirection))
00057 else:
00058 print("can not find ROBONET_type tag")
00059 break
00060 if("style" in tags):
00061 iStyle = tags.index("style")
00062 print("found style tag at index: " + str(iStyle))
00063 else:
00064 print("can not find style tag")
00065 break
00066 else:
00067 if(row[iName].strip().replace(' ', '_')):
00068 if("status" in row[iDirection].lower()):
00069 dirRW = "Read"
00070 elif("control" in row[iDirection].lower()):
00071 dirRW = "Write"
00072 else:
00073 rowNum +=1
00074 continue
00075 xmlData.write(' <Element {0:30} {1:15} {2:30} {3:20} {4:13} {5:40} /> \n'.format('id="' + str(row[iName].strip().replace(' ', '_')) + '"',
00076 'type="' + str(row[iDataType]) + '"',
00077 'offset1="' + str(row[iName]) + '"',
00078 'direction="' + dirRW + '"',
00079 'group="Power"',
00080 'style="' + str(row[iStyle]) + '"'))
00081 rowNum +=1
00082 xmlData.write(' </Elements>' + "\n")
00083 xmlData.write('</ScheduleData>')
00084
00085 xmlData.close()
00086
00087 if __name__ == "__main__":
00088 main(sys.argv[1:])