Long story short, this place has a popup with params. For transfer verification location. All the sudden it stopped working on one tank but works on all the others. I still have no idea why. But what I do know know is the code and process looked really messy. So i figured I’d just refactor it and hope it fixes the issue.
But I don’t program as often as i’d like. So I wanted others opinions or if they see I did something wrong, or have a better way to refactor something etc…
I’m gonna post the original code below: Then below that post my refactored code. I appreciate anyone who takes the time to look at it.
#orignial guys code
valve = event.source.parent.getComponent('Label').text
#Grabs valve table
system.nav.openWindow("Main Windows/TableImport")
window = system.gui.getWindow("Main Windows/TableImport")
data = window.rootContainer.getComponent("Valve Table").data
system.nav.closeWindow("Main Windows/TableImport")
valve_desc = event.source.parent.getComponent('Label 1').text
valve_dest = ""
for row in range(data.rowCount):
index_row = data.getValueAt(row, 0)
if index_row == valve:
valve_dest = data.getValueAt(row, 1)
break
#Goes through table to see if valve is a "valve"
for row in range(data.rowCount):
index_row = data.getValueAt(row, 0)
print valve
#Starts valve comparison if the valve exists
if index_row == valve:
#Checks if valve has multiple destinations
if valve == "AB_211220" or valve == "XV_211102" or valve == "AB_210720" or valve == "XV_210402" or valve == "XV_210502" or valve == "XV_210102" or valve == "XV_211002" or valve == "XV_210603" or valve == "XV_210803" or valve == "XV_210903":
#Opens tank detail's window to get destination
system.nav.openWindow("Tanks Details")
window = system.gui.getWindow("Tanks Details")
desti_cap = window.rootContainer.getComponent("Container")
desti_cap = desti_cap.getComponent("Tansfer_Container")
desti_cap = desti_cap.getComponent("Destination Indicator").text
desti = desti_cap.lower()
system.nav.closeWindow("Tanks Details")
message = valve_desc + " will open to " + '"' + desti + '"' + ". Enter Destination:"
system.gui.messageBox(message, "Destination Check")
input = system.gui.showTouchscreenKeyboard("")
#Compares input with chosen destination
if input == desti:
value = event.source.Setvalue
event.source.parent.Set_tag_state = value
else:
system.gui.messageBox("Input did not match destination")
else:
#Grabs valve table
# system.nav.openWindow("Main Windows/TableImport")
# window = system.gui.getWindow("Main Windows/TableImport")
# data = window.rootContainer.getComponent("Valve Table").data
# system.nav.closeWindow("Main Windows/TableImport")
message = valve_desc + " will open to " + '"' + valve_dest + '"' + ". Enter Destination:"
system.gui.messageBox(message, "Destination Check")
input = system.gui.showTouchscreenKeyboard("")
#message = valve_desc + " will open to " + '"' + valve_dest + '"' + "."
#input = system.gui.inputBox(message, "")
#Searches valve number to match destination with input
for row in range(data.rowCount):
index_row = data.getValueAt(row, 0)
if index_row == valve:
index_col = data.getValueAt(row, 1)
if index_col == input:
#system.gui.messageBox("Input matches destination")
value = event.source.Setvalue
event.source.parent.Set_tag_state = value
else:
system.gui.messageBox("Input did not match destination")
break
elif (row+1) == data.rowCount:
#system.gui.messageBox("This is not a valve")
value = event.source.Setvalue
event.source.parent.Set_tag_state = value
#value = event.source.Setvalue
#
#event.source.parent.Set_tag_state = value
#my code
# Gets Valve
valve = event.source.parent.getComponent('Label').text
# List of vales with multiple locations.
ValveMultLocs =["AB_211220","XV_211102","AB_210720","XV_210402","XV_210502","XV_210102","XV_211002","XV_210603","XV_210803","XV_210903"]
for loc in ValveMultLocs:
if loc == valve:
# Get Tank Number
GetTank = event.source.parent.parent.Page
# Read the selected Transfer To for apropriate tank
TransferTo = system.tag.read("System1/Project1/Tank Details/"+GetTank+"/TransferTo").value
# validate transfer
message = "Do you want to open" GetTank + "to" + TransferTo
system.gui.messageBox(message)
# ask user to verify transfer location
UserInputTransfer = system.gui.showTouchscreenKeyboard("Verify Transfer Location")
if UserInputTransfer = TransferTo:
message = GetTank + " will now open to " + TransferToHere
system.gui.box(message)
value = event.source.Setvalue
event.source.parent.Set_tag_state = value
else:
system.gui.messageBox("Transfer input did not match Tank Details selection.")
else:
#Dictionary to place valve location dataset in.
CRvalveTo = {}
#Dataset of valve locations
ValveDataset = system.tag.read("System1/Project1/HOA/Valves").value
#Puts dataset into a dict
for rowRead in ValveDataset:
CRvalveTo[rowRead['id']] = rowRead['description']
#Gets transfer location
valveCrossx = CRvalveTo[valve]
UserInputTransfere = system.gui.showTouchscreenKeyboard("Verify Transfer Location")
if UserInputTransfere = valveCrossx:
message = valve + " will now open to " + valveCrossx
system.gui.box(message)
value = event.source.Setvalue
event.source.parent.Set_tag_state = value
else:
system.gui.messageBox("Transfer input did not match Tank Details selection.")