Create States with Scripting
We'll use scripts to create and link a large set of downtime states. You define the states as a list of dictionaries, create any missing state categories, create the states, and link them to an asset with a state code for each asset/state link.
The order matters: a state category must exist before you can create a state in it, and a state must exist before you can link it to an asset. The state code is not part of the state itself; it is assigned when the state is linked to an asset. Machine 1's states get fixed codes (61 to 114), because the CoreForge simulator sends those codes for Machine 1. Until now, Machine 1's simulator has only sent Faulted (code 4) as downtime, so it didn't log codes Machine 1 didn't have yet as Unknown State. The script switches it to the new codes once they're linked. Manual Pack 1 has no simulator, so its new links are numbered from the highest state code already linked to it, which avoids conflicts.
- Copy the following script to the Script Console in the Designer and run it. It creates the state categories and states for
Machine 1andManual Pack 1, links them to both assets, and switches Machine 1's simulator to send the new codes. It's safe to run again: anything that already exists is left alone.
View the generateStates script
"""
Create the downtime and running states for Machine 1 and Manual Pack 1, and link them.
Run in the Ignition Designer Script Console. States in Kanoa are built in three layers,
and each has to exist before the next:
1. State categories, e.g. Mechanical\\Spindle. A category path can be several
levels deep; any missing level is created, parent first.
2. States, e.g. 'Spindle Overload' in Mechanical\\Spindle, with a state type
(Running, Planned Downtime, Unplanned Downtime) and a colour. States belong to
the whole enterprise, not to one asset, so a state listed for both assets
(e.g. 'Unexpected Stop') is created once and linked to both.
3. Asset-state links. Linking a state to an asset is what makes it available on
that asset, and the link holds the state code: the number the PLC (here, the
CoreForge simulator) sends to say which state the asset is in.
State codes: Machine 1's states have fixed codes (61 to 114) because the CoreForge
simulator sends those codes for Machine 1. Manual Pack 1 has no simulator, so its
states are numbered from the highest code already linked to it, skipping any code
in use. Add 'stateCode': n to a definition to pin a code.
Last, it points Machine 1's simulator at the new codes. Until they're linked, the
simulator only sends Faulted (4), so Machine 1's downtime isn't Unknown State.
Safe to run again: existing categories and states are reused, states already
linked to an asset are left alone, and only what's missing is created or linked.
Nothing is deleted. The simulator table is rewritten each run with the same rows.
"""
import sys
userId = system.kanoa.security.getIDPUserId({'userName': 'SYSTEM'})
# Each asset and the states to link to it: state code (optional), name, category path, state type and colour
ASSETS = [
{
'assetPath': 'CoreForge\\Philadelphia\\Machining\\Machine 1',
'states': [
{'stateCode': 61, 'stateName': 'Part replacement', 'stateCategory': 'Mechanical', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 62, 'stateName': 'Minor Fault', 'stateCategory': 'Generic', 'stateType': 'Unplanned Downtime', 'stateColor': '#FF8A8A'},
{'stateCode': 63, 'stateName': 'Major Fault', 'stateCategory': 'Generic', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 64, 'stateName': 'Material Defect', 'stateCategory': 'Process Failure', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 65, 'stateName': 'Setup Failure', 'stateCategory': 'Process Failure', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 66, 'stateName': 'Utility Outage', 'stateCategory': 'Process Failure', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 67, 'stateName': 'Automatic Cycle Running', 'stateCategory': 'Production', 'stateType': 'Running', 'stateColor': '#1E88E5'},
{'stateCode': 68, 'stateName': 'Manual Operation', 'stateCategory': 'Production\\Manual', 'stateType': 'Running', 'stateColor': '#42A5F5'},
{'stateCode': 69, 'stateName': 'Probe Measurement Cycle', 'stateCategory': 'Inspection', 'stateType': 'Running', 'stateColor': '#1565C0'},
{'stateCode': 70, 'stateName': 'Tool Changeover', 'stateCategory': 'Tooling', 'stateType': 'Planned Downtime', 'stateColor': '#4DD0E1'},
{'stateCode': 71, 'stateName': 'Fixture Changeover', 'stateCategory': 'Setup', 'stateType': 'Planned Downtime', 'stateColor': '#26C6DA'},
{'stateCode': 72, 'stateName': 'Preventive Maintenance', 'stateCategory': 'Maintenance', 'stateType': 'Planned Downtime', 'stateColor': '#00ACC1'},
{'stateCode': 73, 'stateName': 'Emergency Stop Activated', 'stateCategory': 'Safety', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 74, 'stateName': 'Door Open Alarm', 'stateCategory': 'Safety', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateCode': 75, 'stateName': 'Light Curtain Fault', 'stateCategory': 'Safety', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateCode': 76, 'stateName': 'Spindle Overload', 'stateCategory': 'Mechanical\\Spindle', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateCode': 77, 'stateName': 'Spindle Bearing Failure', 'stateCategory': 'Mechanical\\Spindle', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateCode': 78, 'stateName': 'Spindle Orientation Fault', 'stateCategory': 'Mechanical\\Spindle', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateCode': 79, 'stateName': 'Axis Drive Fault', 'stateCategory': 'Mechanical\\Axis', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateCode': 80, 'stateName': 'Axis Overtravel', 'stateCategory': 'Mechanical\\Axis', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateCode': 81, 'stateName': 'Ballscrew Fault', 'stateCategory': 'Mechanical\\Axis', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateCode': 82, 'stateName': 'Linear Guide Fault', 'stateCategory': 'Mechanical\\Axis', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateCode': 83, 'stateName': 'Chuck Clamp Failure', 'stateCategory': 'Mechanical\\Workholding', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateCode': 84, 'stateName': 'Fixture Clamp Fault', 'stateCategory': 'Mechanical\\Workholding', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'},
{'stateCode': 85, 'stateName': 'Part Detection Failure', 'stateCategory': 'Mechanical\\Workholding', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 86, 'stateName': 'Tool Breakage Detected', 'stateCategory': 'Tooling', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateCode': 87, 'stateName': 'Tool Not Found', 'stateCategory': 'Tooling', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateCode': 88, 'stateName': 'Tool Changer Fault', 'stateCategory': 'Tooling', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateCode': 89, 'stateName': 'Tool Life Exceeded', 'stateCategory': 'Tooling', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateCode': 90, 'stateName': 'Incorrect Tool Loaded', 'stateCategory': 'Tooling', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateCode': 91, 'stateName': 'Servo Alarm', 'stateCategory': 'Controls\\Servo', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateCode': 92, 'stateName': 'Encoder Fault', 'stateCategory': 'Controls\\Feedback', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateCode': 93, 'stateName': 'PLC Communication Loss', 'stateCategory': 'Controls\\Communication', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateCode': 94, 'stateName': 'CNC Program Error', 'stateCategory': 'Controls\\Program', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'},
{'stateCode': 95, 'stateName': 'M-Code Timeout', 'stateCategory': 'Controls\\Program', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 96, 'stateName': 'Software Crash', 'stateCategory': 'Controls\\Software', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateCode': 97, 'stateName': 'Power Failure', 'stateCategory': 'Electrical', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateCode': 98, 'stateName': 'Control Cabinet Overtemperature', 'stateCategory': 'Electrical\\Cabinet', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateCode': 99, 'stateName': 'Drive Amplifier Fault', 'stateCategory': 'Electrical\\Drive', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateCode': 100, 'stateName': 'Breaker Trip', 'stateCategory': 'Electrical\\Power', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateCode': 101, 'stateName': 'Low Air Pressure', 'stateCategory': 'Utilities\\Air', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateCode': 102, 'stateName': 'Hydraulic Pressure Fault', 'stateCategory': 'Utilities\\Hydraulic', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateCode': 103, 'stateName': 'Vacuum Failure', 'stateCategory': 'Utilities\\Vacuum', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateCode': 104, 'stateName': 'Coolant Pump Failure', 'stateCategory': 'Utilities\\Coolant', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'},
{'stateCode': 105, 'stateName': 'Low Coolant Level', 'stateCategory': 'Utilities\\Coolant', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateCode': 106, 'stateName': 'Coolant Temperature High', 'stateCategory': 'Utilities\\Coolant', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateCode': 107, 'stateName': 'Chip Conveyor Jam', 'stateCategory': 'Material Handling\\Chips', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateCode': 108, 'stateName': 'Chip Bin Full', 'stateCategory': 'Material Handling\\Chips', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateCode': 109, 'stateName': 'Bar Feeder Fault', 'stateCategory': 'Material Handling\\Feed', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateCode': 110, 'stateName': 'Material Load Failure', 'stateCategory': 'Material Handling\\Feed', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateCode': 111, 'stateName': 'Network Disconnect', 'stateCategory': 'IT\\Network', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateCode': 112, 'stateName': 'MES Communication Loss', 'stateCategory': 'IT\\Integration', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateCode': 113, 'stateName': 'Unexpected Stop', 'stateCategory': 'Unknown', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateCode': 114, 'stateName': 'Alarm Requires Operator Reset', 'stateCategory': 'Unknown', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'}
]
},
{
'assetPath': 'CoreForge\\Philadelphia\\Finishing\\Manual Pack 1',
'states': [
{'stateName': 'Packaging Material Changeover', 'stateCategory': 'Changeover\\Packaging', 'stateType': 'Planned Downtime', 'stateColor': '#F9A825'},
{'stateName': 'Label Roll Changeover', 'stateCategory': 'Changeover\\Labeling', 'stateType': 'Planned Downtime', 'stateColor': '#FBC02D'},
{'stateName': 'Packing Station Cleaning', 'stateCategory': 'Sanitation', 'stateType': 'Planned Downtime', 'stateColor': '#FDD835'},
{'stateName': 'Missing Packaging Materials', 'stateCategory': 'Material\\Packaging', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateName': 'Wrong Box Size Available', 'stateCategory': 'Material\\Packaging', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateName': 'Damaged Boxes', 'stateCategory': 'Material\\Packaging', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateName': 'Missing Product Inserts', 'stateCategory': 'Material\\Documentation', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateName': 'Label Printer Offline', 'stateCategory': 'Equipment\\Labeling', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateName': 'Label Printer Jam', 'stateCategory': 'Equipment\\Labeling', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateName': 'Label Misprint', 'stateCategory': 'Equipment\\Labeling', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateName': 'Scanner Not Reading Barcode', 'stateCategory': 'Equipment\\Scanning', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateName': 'Scale Communication Loss', 'stateCategory': 'Equipment\\Scale', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateName': 'Scale Out of Tolerance', 'stateCategory': 'Equipment\\Scale', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'},
{'stateName': 'Box Taper Fault', 'stateCategory': 'Equipment\\Sealing', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateName': 'Tape Roll Empty', 'stateCategory': 'Equipment\\Sealing', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateName': 'Product Count Mismatch', 'stateCategory': 'Quality\\Count', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateName': 'Incorrect Item at Pack Station', 'stateCategory': 'Quality\\Identification', 'stateType': 'Unplanned Downtime', 'stateColor': '#B71C1C'},
{'stateName': 'Incorrect Coating Variant', 'stateCategory': 'Quality\\Identification', 'stateType': 'Unplanned Downtime', 'stateColor': '#EF5350'},
{'stateName': 'Damaged Parts Found', 'stateCategory': 'Quality\\Parts', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateName': 'Bad Coating Found', 'stateCategory': 'Quality\\Coating', 'stateType': 'Unplanned Downtime', 'stateColor': '#8E1B1B'},
{'stateName': 'Work Instruction Missing', 'stateCategory': 'Operations\\Documentation', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateName': 'Order Information Mismatch', 'stateCategory': 'Operations\\Order', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'},
{'stateName': 'Awaiting QA Release', 'stateCategory': 'Operations\\Hold', 'stateType': 'Unplanned Downtime', 'stateColor': '#F44336'},
{'stateName': 'Awaiting Material Movement', 'stateCategory': 'Operations\\Material Flow', 'stateType': 'Unplanned Downtime', 'stateColor': '#ba1a1a'},
{'stateName': 'MES Communication Loss', 'stateCategory': 'IT\\Integration', 'stateType': 'Unplanned Downtime', 'stateColor': '#E57373'},
{'stateName': 'Barcode Lookup Failed', 'stateCategory': 'IT\\Integration', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateName': 'Workstation Login Issue', 'stateCategory': 'IT\\Workstation', 'stateType': 'Unplanned Downtime', 'stateColor': '#E53935'},
{'stateName': 'Ergonomic Adjustment Required', 'stateCategory': 'Safety\\Ergonomics', 'stateType': 'Unplanned Downtime', 'stateColor': '#C62828'},
{'stateName': 'Packing Area Obstruction', 'stateCategory': 'Safety\\Area', 'stateType': 'Unplanned Downtime', 'stateColor': '#D32F2F'},
{'stateName': 'Unexpected Stop', 'stateCategory': 'Unknown', 'stateType': 'Unplanned Downtime', 'stateColor': '#A52A2A'}
]
},
]
allDefinitions = [d for a in ASSETS for d in a['states']]
print '=' * 70
print 'Creating states for %s' % ', '.join(a['assetPath'].split('\\')[-1] for a in ASSETS)
print '=' * 70
# =========================
# Step 0: Check everything we need before changing anything
# =========================
print '\nChecking assets, state types and definitions...'
for a in ASSETS:
a['assetId'] = system.kanoa.utilities.getFieldValue('assetId', system.kanoa.asset.getAssets({'assetPath': a['assetPath']}))
if a['assetId'] is None:
raise Exception('No asset at %s. Check the path.' % a['assetPath'])
names = [d['stateName'] for d in a['states']]
duplicates = sorted(set(n for n in names if names.count(n) > 1))
if duplicates:
raise Exception('States listed more than once for %s: %s' % (a['assetPath'], ', '.join(duplicates)))
print ' %s: assetId %s, %d states' % (a['assetPath'], a['assetId'], len(a['states']))
stateTypes = system.kanoa.utilities.convertDatasetToDict(system.kanoa.asset.getStateType({}), 'stateTypeName', 'stateTypeId')
badTypes = sorted(set(d['stateType'] for d in allDefinitions if d['stateType'] not in stateTypes))
if badTypes:
raise Exception('Unknown state type(s): %s. Valid types: %s' % (', '.join(badTypes), ', '.join(sorted(stateTypes))))
# A state listed for both assets must have the same type; a different colour just uses the first one
firstDefinition = {}
for d in allDefinitions:
key = (d['stateCategory'], d['stateName'])
first = firstDefinition.setdefault(key, d)
if first['stateType'] != d['stateType']:
raise Exception('%s / %s is listed as both %s and %s' % (key + (first['stateType'], d['stateType'])))
if first['stateColor'] != d['stateColor']:
print ' Note: %s / %s is listed with colours %s and %s; using %s' % (key + (first['stateColor'], d['stateColor'], first['stateColor']))
# =========================
# Step 1: Create missing state categories
# =========================
def getParentPath(path):
"""'Mechanical\\Spindle' -> 'Mechanical'; a top-level category has no parent."""
parts = path.split('\\')
return '\\'.join(parts[:-1]) if len(parts) > 1 else None
def ensureStateCategory(categoryPath, stateCategories):
"""Return the category's id, creating it (and any missing parents) if needed."""
if stateCategories.get(categoryPath):
return stateCategories[categoryPath]
parentPath = getParentPath(categoryPath)
parentId = ensureStateCategory(parentPath, stateCategories) if parentPath else None
stateCategoryInfo = {'parentId': parentId, 'stateCategoryName': categoryPath.split('\\')[-1], 'enabled': True}
stateCategoryId, msg = system.kanoa.asset.addStateCategory(stateCategoryInfo, userId)
if msg or stateCategoryId is None:
raise Exception('Could not create state category %s: %s' % (categoryPath, msg))
print ' Created category %s (stateCategoryId %s)' % (categoryPath, stateCategoryId)
stateCategories[categoryPath] = stateCategoryId
return stateCategoryId
print '\n-- Step 1: State categories'
# All categories, including disabled ones, so we never try to create one twice
stateCategories = system.kanoa.utilities.convertDatasetToDict(
system.kanoa.asset.getStateCategory({}), 'stateCategoryPath', 'stateCategoryId')
needed = sorted(set(d['stateCategory'] for d in allDefinitions))
before = len(stateCategories)
for categoryPath in needed:
ensureStateCategory(categoryPath, stateCategories)
print ' %d categories used by these states; %d created (including parent categories)' % (len(needed), len(stateCategories) - before)
# =========================
# Step 2: Create missing states (once, even if both assets use them)
# =========================
print '\n-- Step 2: States'
stateIds = {}
for s in system.kanoa.utilities.convertDatasetToJSON(system.kanoa.asset.getStates({})):
stateIds[(s['stateCategoryPath'], s['stateName'])] = s['stateId']
created, existed = 0, 0
for key, d in sorted(firstDefinition.items()):
if key in stateIds:
existed += 1
continue
stateInfo = {
'stateName': d['stateName'],
'stateColor': d['stateColor'],
'stateCategoryId': stateCategories[d['stateCategory']],
'stateTypeId': stateTypes[d['stateType']],
'assetGroupId': None,
'enabled': True
}
stateId, msg = system.kanoa.asset.addState(stateInfo, userId)
if msg or stateId is None:
print ' FAILED: %s / %s: %s' % (key + (msg,))
continue
print ' Created: %s / %s (stateId %s, %s)' % (key + (stateId, d['stateType']))
stateIds[key] = stateId
created += 1
print ' %d states created, %d already existed' % (created, existed)
# =========================
# Step 3: Link each asset's states with a state code
# =========================
def currentLinks(assetId):
"""{stateId: stateCode} for every state linked to the asset right now."""
links = system.kanoa.utilities.convertDatasetToJSON(system.kanoa.asset.getAssetStates({'assetId': assetId}))
return dict((s['stateId'], s['stateCode']) for s in links)
def linkStates(assetId, ids, codes):
"""Link the states, each with its state code. Returns the asset's links afterwards, so the caller can confirm they were saved."""
try:
system.kanoa.asset.linkAssetStates([assetId], ids, userId, codes)
except:
print ' linkAssetStates raised: %s' % str(sys.exc_info()[1]).split('\n')[0]
return currentLinks(assetId)
for a in ASSETS:
assetPath, assetId = a['assetPath'], a['assetId']
print '\n-- Step 3: Links to %s' % assetPath
# All links, including disabled ones, so a new code never reuses one already taken
links = currentLinks(assetId)
usedCodes = set(c for c in links.values() if c is not None)
nextStateCode = max(usedCodes) + 1 if usedCodes else 1
print ' %d states already linked; highest code %s' % (len(links), max(usedCodes) if usedCodes else 'none')
linkIds, linkCodes, linkNames = [], [], []
for d in a['states']:
stateId = stateIds.get((d['stateCategory'], d['stateName']))
if stateId is None:
print ' SKIPPED: %s was not created' % d['stateName']
continue
if stateId in links:
continue
code = d.get('stateCode')
if code is None:
while nextStateCode in usedCodes:
nextStateCode += 1
code = nextStateCode
elif code in usedCodes:
print ' SKIPPED: %s wants code %s, which is already used on this asset' % (d['stateName'], code)
continue
usedCodes.add(code)
linkIds.append(stateId)
linkCodes.append(code)
linkNames.append(d['stateName'])
print ' Will link %-34s code %s' % (d['stateName'], code)
if not linkIds:
print ' Nothing new to link'
continue
after = linkStates(assetId, linkIds, linkCodes)
missing = [n for n, i in zip(linkNames, linkIds) if i not in after]
wrongCode = [(n, c, after[i]) for n, i, c in zip(linkNames, linkIds, linkCodes) if i in after and after[i] != c]
print ' Linked %d of %d state(s)' % (len(linkIds) - len(missing), len(linkIds))
if missing:
print ' NOT LINKED: %s' % ', '.join(missing)
for name, wanted, got in wrongCode:
print ' WRONG CODE: %s has code %s, expected %s' % (name, got, wanted)
# =========================
# Step 4: Point Machine 1's simulator at its new states
# =========================
# The CoreForge simulator picks Machine 1's downtime codes from this table. Until now
# it only sent Faulted (4), because the other codes weren't linked to Machine 1. Each
# code is sent only if it's now linked, so none of them arrive as Unknown State.
SIM_TABLE = '[Kanoa]CoreForge Sim/Site/Machining/Machine 1/sim/state/simParams/downtimeTable'
SIM_DOWNTIME = [
# state code, weight (how often it's picked), shortest and longest duration in minutes
[107, 9.79, 20.78, 27.21],
[65, 2.72, 10.26, 18.47],
[61, 4.89, 20.99, 25.39],
[77, 9.87, 12.3, 16.2],
[72, 7.16, 25.71, 28.58],
[102, 2.78, 17.1, 25.44],
[105, 3.73, 25.16, 29.11],
[70, 7.85, 11.93, 28.14],
[110, 8.48, 27.4, 29.53],
[91, 7.37, 6.58, 26.09],
[75, 2.71, 8.53, 25.85],
[88, 3.84, 7.95, 21.61],
[113, 7.89, 25.22, 27.43],
[85, 4.44, 7.34, 21.7],
[99, 9.77, 20.96, 23.59],
[73, 6.44, 23.06, 24.07],
[95, 6.22, 14.88, 15.08],
[97, 7.04, 18.89, 26.85],
[64, 2.44, 11.28, 22.27],
[79, 5.22, 23.69, 26.2],
[104, 2.45, 6.18, 24.42],
[4, 9.36, 8.14, 19.94],
[92, 8.84, 13.24, 14.42],
[83, 3.9, 22.9, 27.66],
[82, 2.79, 8.94, 23.56],
[76, 2.59, 25.79, 27.41],
[100, 4.51, 15.2, 17.74],
[112, 5.34, 10.77, 20.75],
[84, 2.18, 22.78, 25.69],
[1, 5.12, 16.08, 29.68],
[89, 1.99, 9.78, 16.6],
[98, 2.16, 14.27, 15.71],
[109, 8.54, 29.63, 29.92],
[74, 2.13, 25.54, 27.17],
[78, 8.5, 21.15, 23.64],
[87, 2.21, 24.18, 24.43],
[90, 5.67, 10.09, 17.1],
[111, 9.48, 12.54, 18.95],
[21, 8.39, 6.25, 12.09],
[69, 1.05, 15.64, 20.96],
]
print '\n-- Step 4: Machine 1 simulator'
machine1 = [a for a in ASSETS if a['assetPath'].endswith('\\Machine 1')][0]
linkedCodes = set(c for c in currentLinks(machine1['assetId']).values() if c is not None)
simRows = [r for r in SIM_DOWNTIME if r[0] in linkedCodes]
notLinked = sorted(r[0] for r in SIM_DOWNTIME if r[0] not in linkedCodes)
quality = system.tag.writeBlocking([SIM_TABLE], [system.dataset.toDataSet(
['stateCode', 'weight', 'minDurationMinutes', 'maxDurationMinutes'], simRows)])[0]
if quality.isGood():
print ' The simulator now sends %d downtime codes for Machine 1' % len(simRows)
else:
print ' Could not write %s: %s' % (SIM_TABLE, quality)
if notLinked:
print ' Not sent, because they are not linked to Machine 1: %s' % ', '.join(str(c) for c in notLinked)
print '\n' + '=' * 70
print 'Done. Check the states on each asset in Configuration > Ops > Assets, on the States tab.'
print '=' * 70
After running the script, verify in the application that:
- New state categories were created.
- New states were created.
- The states have the correct category, state type, and color, and no asset group (the script sets
assetGroupIdtoNone). Machine 1andManual Pack 1each have their new states linked.- State codes are present and do not conflict with existing state codes.
- Over the next few minutes,
Machine 1's downtime shows the new states, not justFaulted.
Copy this script into the Ignition Designer Script Console (Tools → Script Console) and run it after the script. It counts the states linked to Machine 1 and Manual Pack 1: the 4 built-in states plus the 54 and 30 the script links. It doesn't change anything.
Show the check script
# Kanoa Ops Training - check "Create States with Scripting" (Scaling across the enterprise, lesson 1)
#
# Run in the Ignition Designer Script Console. It counts the states linked to each
# asset: the 4 built-in states plus the ones the lesson's scripts link.
ASSETS = [
# asset path, states the lesson's script links
('CoreForge\\Philadelphia\\Machining\\Machine 1', 54),
('CoreForge\\Philadelphia\\Finishing\\Manual Pack 1', 30),
]
BUILT_IN = 4
results = []
print '=' * 70
print 'Kanoa Ops Training - Create States with Scripting check'
print '=' * 70
for assetPath, scripted in ASSETS:
name = assetPath.split('\\')[-1]
assetId = system.kanoa.utilities.getFieldValue('assetId', system.kanoa.asset.getAssets({'assetPath': assetPath}))
if assetId is None:
results.append(False)
print 'FAIL %s not found at %s' % (name, assetPath)
continue
count = len(system.kanoa.utilities.convertDatasetToJSON(system.kanoa.asset.getAssetStates({'assetId': assetId})))
expected = BUILT_IN + scripted
ok = count >= expected
results.append(ok)
if ok:
print 'PASS %s has %d state links (expected at least %d)' % (name, count, expected)
else:
print 'FAIL %s has %d state links, expected at least %d' % (name, count, expected)
print ' -> Run the %s script again. It only creates and links what is missing.' % name
print '=' * 70
print 'Result: %d of %d checks passed' % (results.count(True), len(results))
print '=' * 70