hello, I am looking to select a reason from a dropdown, then after pressing a button, the dropdown will send to mysql. I current am using a event handler script on the button. It is not functional. What would y'all advise?
The main goal is to have a machine go down for 15 minutes, then trigger a popup where the user has to select a reason the machine went down. The data will then be stored to a database.
def runAction(self, event):
# Get the selected reason from the dropdown
selected_reason = event.source.parent.getComponent('Dropdown').selectedValue
# Check if a reason is selected
if selected_reason:
try:
# Attempt to insert the selected reason into the database
system.db.runPrepUpdate("INSERT INTO ignition.senntech_201_downtime_reasons (reason) VALUES (?)", [selected_reason])
# Provide feedback to the user that the data was successfully stored
system.gui.messageBox("Data successfully stored in the database.", "Success")
except Exception as e:
# Handle any errors that occur during the database write process
system.gui.messageBox("An error occurred: " + str(e), "Error")
else:
# If no reason is selected, display a message to the user
system.gui.messageBox("Please select a reason before confirming.", "Warning")
the actual databse name is MySQL_Production, but the schema is ignition
add the database name before address and got this error:Error running action 'component.onActionPerformed' on Machine Downtime windows/Senntech 201 Popup@D/root/Button: Traceback (most recent call last): File "function:runAction", line 3, in runAction AttributeError: 'com.inductiveautomation.ignition.common.script.ada' object has no attribute 'source'
I am guessing you are trying to use Vision scoped things in Perspective. That won't work.
Use the property button in the script dialog to browse to the property you want.
system.gui.* functions are also Vision scoped. use system.perspective.openPopup() instead.
Is this closer?
# Retrieve the selected reason from the Dropdown component within the same parent container
selected_reason = self.parent.getComponent('Dropdown').props.value
# Specify the database schema
database_schema = "ignition"
# Specify the table name
table_name = "senntech_201_downtime_reasons"
# Specify the column name for the reason
column_name = "reason"
# Check if a reason is selected
if selected_reason:
try:
# Insert the selected reason into the database
system.db.runPrepUpdate("INSERT INTO {}.{} ({}) VALUES (?)".format(database_schema, table_name, column_name), [selected_reason])
# Provide feedback to the user that the data was successfully stored
system.perspective.print("Data successfully stored in the database.")
except Exception as e:
# Handle any errors that occur during the database write process
system.perspective.print("An error occurred: " + str(e))
else:
# If no reason is selected, display a warning message to the user
system.perspective.print("Please select a reason before confirming.")