I am using a gateway event script to evaluate if data was created in a SQL database. At the end of this script, an email is sent if any data was missed. The script is evaluating everything correctly, but it is sending me 2 emails each time instead of one.
currentDateTime = system.tag.read('[System]Gateway/CurrentDateTime').value
currentDateString = system.date.format(currentDateTime, 'MM/dd/yyyy')
activeUICs = system.db.runQuery("SELECT site_name, uic, site_active FROM ignition.expected_sites")
activeUICsCSV = system.dataset.toCSV(activeUICs, showHeaders = 0)
storageQuery = 'SELECT uic FROM ignition.datatable WHERE date = "' + currentDateString + '"'
dataStored = system.db.runQuery(storageQuery)
dataStoredCSV = system.dataset.toCSV(dataStored, showHeaders = 0)
counter = 0
sitesNotCaptured = "The Following Sites Were Not Able Capture Data Today:\n"
missingUICs = 0
for i in activeUICsCSV.split('\n'):
UICCaptured = 0
counter = counter + 1
iSplit = i.split(',')
dataStoredCounter = 0
for i in dataStoredCSV.split('\n'):
if "0" in iSplit[2]:
UICCaptured = 1
break
dataStoredCounter = dataStoredCounter + 1
if iSplit[1] == i:
UICCaptured = 1
break
if dataStoredCounter == len(dataStoredCSV.split('\n')) - 1:
break
if counter == len(activeUICsCSV.split('\n')) - 1:
break
if UICCaptured == 0:
sitesNotCaptured = sitesNotCaptured + iSplit[0].strip('"') + '\n'
missingUICs = missingUICs + 1
recipients = ["myEmail@myCompany.com"]
emailBody = sitesNotCaptured
gateway = "smtp.gateway.example:25:tls"
source = "automated@email.com"
pw = "myPass"
system.net.sendEmail(smtp = gateway, fromAddr = source, subject="Data - Missing Sites", body=emailBody, html=0, to=recipients, username=source, password=pw)