At our site we interface with most information that doesn't come from a plc tag or opc device connection via getApi
requests to a custom api for datalogging / product info storing. The way we do this returns our needed info in the form of large dataset value tags, but I have recently learned this is a less than ideal acquisition method for what the company is most interested in, reports. What would be a better way or where should I start looking to learn that? This is an example of what our current process looks like in Ignition's scripting project library.
def PackingLineB24():
# API request to get information
rawdata = CAP.apiGet(13)
# Find the maximum number of analysis results in any data entry
max_analysis_results = [0]
# Function to sort packing and determine max analysis results length
def sortPacking(data):
lot = data['LotNumber']
prod = data['Product']['ProductKey']
prodid = data['Product']['ID']
owner = data['Product']['Owner']['Value']
date = data['Date']
cust = data['Customer']['Name']
analysisResults = data['AnalysisResults']
if analysisResults and len(analysisResults) > 0:
max_analysis_results[0] = max(max_analysis_results[0], len(analysisResults))
# Prepare the basic data
base_data = [lot, prod, prodid, owner, date, cust]
# Extract `hlvs` values
hlvs_data = [result['Value'] for result in analysisResults]
return base_data, hlvs_data
return None, None
rawPacking = [sortPacking(data) for data in rawdata]
# Remove None values and prepare data with variable headers
rawPacking = [item for item in rawPacking if item[0] is not None]
# Finalize headers
headers = ["Lot Number", "Product Type", "Product ID", "Owner", "Date", "Customer"]
headers.append("Halves")
for i in range(1, max_analysis_results[0]):
headers.append("Halves {}".format({i + 1}))
# Prepare rows of data for the dataset
packing = []
for base_data, hlvs_data in rawPacking:
# Backfill `hlvs` values with `None` for missing analysis keys
row = base_data + hlvs_data + [None] * (max_analysis_results[0] - len(hlvs_data))
packing.append(row)
for pack in packing:
print(pack)
# Create a new Dataset of updated information to save to the tag
currentSet = system.dataset.toDataSet(headers, packing)
#Write to the tag
system.tag.writeBlocking(['[default]CWS Perspective/PackingLineB24'], [currentSet])