Automatic Checks on a Timer
An Auto check sheet completes itself. Kanoa creates the check, reads every item's value from its attribute's data source, and submits it, with nobody involved. That only works if every required item has an attribute with an enabled data source for the check's asset, so the sheet must also require an asset.
-
In Configuration → Quality → Check Sheets, add
Coating SPC Checksin theCoreForge\Operationsfolder. -
On the Settings tab, select Auto and Asset Required, and choose
CoreForge\Philadelphia\Coating\Spray Line 1. -
On the Checks tab, add three items, each Type
Check, Data TypeFloat, Enabled and Required:Check item Attribute Units Coating ThicknessCoreForge\Coating\ThicknessµmFlow RateCoreForge\Coating\Flow RategpmLine SpeedCoreForge\Coating\Line SpeedfpmIf a unit isn't in the list, type it into the Units field to create it.
-
On the Triggers tab, add a
Timed Intervaltrigger with an interval of5minutes. Leave the other timers at0; an automatic check completes immediately, so it never becomes due or late. -
Save, release, and let it run for at least an hour so there's data to analyze.
Timed interval triggers are fired by the Quality Schedule gateway timer, which calls intervalTimerEvent every minute by default. Check that its status is Running on the Timers tab of the Kanoa gateway config page (Kanoa > Settings), then check that the sheet is released, the trigger is enabled, and the attribute data sources are enabled for Spray Line 1.
Copy this script into the Ignition Designer Script Console (Tools → Script Console) and run it. It checks that Coating SPC Checks is a released Auto sheet on Spray Line 1, that its items read the Coating attributes, and that its Timed Interval trigger is enabled at 5 minutes. It also checks that a check was taken in the last 10 minutes, that the Quality Schedule timer is running, that there's an hour of approved checks to analyze, and that the latest one has all three values. Each check prints PASS or FAIL with what to fix. It doesn't change anything.
Show the check script
# Kanoa Quality Training - check "Automatic Checks on a Timer" (Coating Line SPC, lesson 2)
#
# Run in the Ignition Designer Script Console (Tools > Script Console), in the
# project that inherits KANOA_SCRIPTS. It only reads; it changes nothing.
# Every check prints PASS or FAIL with what to fix, then a summary.
SHEET = 'Coating SPC Checks'
FOLDER = 'CoreForge\\Operations'
ASSET = 'CoreForge\\Philadelphia\\Coating\\Spray Line 1'
ITEMS = [
# name, attribute, units
# micro sign, Greek mu, or plain um; escaped so the script is plain ASCII and pastes cleanly anywhere
('Coating Thickness', 'CoreForge\\Coating\\Thickness', [u'\u00b5m', u'\u03bcm', 'um']),
('Flow Rate', 'CoreForge\\Coating\\Flow Rate', ['gpm']),
('Line Speed', 'CoreForge\\Coating\\Line Speed', ['fpm']),
]
INTERVAL = 5
TIMER = 'qualitySchedule' # the Quality Schedule gateway timer, which fires Timed Interval triggers
HOUR_OF_CHECKS = 60 / INTERVAL
results = []
def check(ok, what, fix=None):
results.append(bool(ok))
if ok:
print 'PASS %s' % what
else:
print 'FAIL %s' % what
if fix:
print ' -> %s' % fix
def note(what):
print 'NOTE %s' % what
def field(row, *names):
for n in names:
if n in row and row[n] is not None:
return row[n]
return None
def text(v):
return '' if v is None else unicode(v).strip()
def key(v):
return ' '.join(text(v).lower().split())
def truthy(v):
return str(v).lower() in ('true', '1', 'yes')
def num(v):
try:
return float(v)
except:
return None
def rows(data):
# Kanoa returns a dataset, a list of dictionaries, or None
if data is None:
return []
if hasattr(data, 'getRowCount'):
cols = list(data.getColumnNames())
return [dict((c, data.getValueAt(i, c)) for c in cols) for i in range(data.getRowCount())]
return [dict(r) for r in data]
def findSheet(name):
"""All versions of a check sheet, and the one to check: the Released version, or else the newest.
A revision renames the archived version '<name> - N' and the open one '<name> - In Revision',
so the other versions are found by their master sheet, not by name."""
sheets = rows(system.kanoa.quality.sheet.getSheets({}))
named = [s for s in sheets if text(field(s, 'chkShtName')) == name] or \
[s for s in sheets if key(field(s, 'chkShtName')) == key(name)]
if not named:
return None, []
masters = set(text(field(s, 'masterChkShtId', 'chkShtId')) for s in named)
versions = [s for s in sheets if text(field(s, 'masterChkShtId', 'chkShtId')) in masters]
versions.sort(key=lambda s: num(field(s, 'chkShtVersionNumber')) or 0)
released = [s for s in versions if text(field(s, 'chkShtVersionStateName')) == 'Released']
return (released or versions)[-1], versions
def findItem(items, name):
exact = [i for i in items if text(field(i, 'chkItemName')) == name]
close = [i for i in items if key(field(i, 'chkItemName')) == key(name)]
return (exact or close or [None])[0]
def millis(v):
return v.getTime() if hasattr(v, 'getTime') else 0
def sheetEvents(versions, start, end):
"""The sheet's check events between two dates, newest first."""
out = []
for s in versions:
out += rows(system.kanoa.quality.event.getChkShtEvents({'chkShtId': field(s, 'chkShtId'), 'startDate': start, 'endDate': end}))
out.sort(key=lambda e: millis(field(e, 'scheduledDate', 'takenDate')), reverse=True)
return out
def eventItems(event):
"""The recorded values of a check event, by check item name."""
byName = {}
for r in rows(system.kanoa.quality.event.getChkShtEventItems({'chkShtEventId': field(event, 'chkShtEventId')})):
byName.setdefault(text(field(r, 'chkItemName')), []).append(r)
return byName
print '=' * 70
print 'Kanoa Quality Training - Automatic Checks on a Timer check'
print '=' * 70
sheet, versions = findSheet(SHEET)
if sheet is None:
check(False, 'Check sheet %s exists' % SHEET,
'In Configuration > Quality > Check Sheets, add %s in the %s folder.' % (SHEET, FOLDER))
else:
# 1. The check sheet and its settings
print '\n-- Check sheet'
actual = text(field(sheet, 'chkShtName'))
check(actual == SHEET, 'Check sheet %s exists' % SHEET, "Found '%s'. Rename it to %s." % (actual, SHEET))
folder = text(field(sheet, 'chkShtTypePath'))
check(folder == FOLDER, 'It is in the %s folder' % FOLDER,
"It's in '%s'. On the Settings tab, set the folder to %s." % (folder or 'no folder', FOLDER))
check(truthy(field(sheet, 'auto')), 'Auto is selected', 'On the Settings tab, select Auto.')
check(truthy(field(sheet, 'assetReqd')), 'Asset Required is selected', 'On the Settings tab, select Asset Required.')
assets = [text(field(r, 'assetPath')) for r in rows(system.kanoa.quality.sheet.getAssets(field(sheet, 'chkShtId')))]
check(ASSET in assets, 'Its asset is Spray Line 1',
'Under Asset Required, choose %s.%s' % (ASSET, (' Now: %s.' % ', '.join(assets)) if assets else ''))
state = text(field(sheet, 'chkShtVersionStateName'))
check(state == 'Released', 'It is Released', "It's %s. Close the editor and change its status to Released." % (state or 'not set'))
# 2. Check items
print '\n-- Check items'
items = rows(system.kanoa.quality.config.getChecks({'chkShtId': field(sheet, 'chkShtId')}))
for name, attribute, units in ITEMS:
item = findItem(items, name)
if item is None:
check(False, '%s exists' % name, 'On the Checks tab, add a check item named %s.' % name)
continue
problems = []
actual = text(field(item, 'chkItemName'))
if actual != name:
problems.append("rename '%s' to %s" % (actual, name))
if text(field(item, 'chkItemTypeName')) != 'Check':
problems.append('set Type to Check')
if text(field(item, 'dataTypeName')) != 'Float':
problems.append("set Data Type to Float (it's %s)" % (text(field(item, 'dataTypeName')) or 'blank'))
if not truthy(field(item, 'enabled')):
problems.append('select Enabled')
if not truthy(field(item, 'required')):
problems.append('select Required')
if text(field(item, 'attributePath')) != attribute:
problems.append("assign the attribute %s (it's %s)" % (attribute, text(field(item, 'attributePath')) or 'none'))
if text(field(item, 'engUnitName')).lower() not in [u.lower() for u in units]:
problems.append("set Units to %s (it's %s)" % (units[0], text(field(item, 'engUnitName')) or 'blank'))
check(not problems, '%s reads %s' % (name, attribute), '; '.join(problems).capitalize() + '.')
# 3. The timed trigger
print '\n-- Timed Interval trigger'
triggers = [t for t in rows(system.kanoa.quality.config.getChkShtTriggers({'chkShtId': field(sheet, 'chkShtId')}))
if text(field(t, 'triggerTypeName')) == 'Timed Interval']
if not triggers:
check(False, 'It has a Timed Interval trigger', 'On the Triggers tab, add a Timed Interval trigger with an interval of %d minutes.' % INTERVAL)
else:
t = triggers[0]
check(num(field(t, 'intervalMins')) == INTERVAL, 'The interval is %d minutes' % INTERVAL,
"It's %s. Edit the trigger and set the interval to %d." % (text(field(t, 'intervalMins')) or 'blank', INTERVAL))
check(truthy(field(t, 'enabled')), 'The trigger is enabled', 'Edit the trigger and select Enabled.')
# 4. Checks taken automatically
print '\n-- Automatic checks'
now = system.date.now()
events = sheetEvents(versions, system.date.addHours(now, -24), now)
recent = [e for e in events if millis(field(e, 'scheduledDate', 'takenDate')) >= millis(system.date.addMinutes(now, -2 * INTERVAL))]
check(recent, 'A check was taken in the last %d minutes' % (2 * INTERVAL),
'No recent checks. Check the sheet is released, the trigger and the data sources are enabled, '
'and that the Quality Schedule timer is Running on the Timers tab of the Kanoa gateway config page (Kanoa > Settings).')
try:
jobs = [j for j in rows(system.kanoa.config.getJobs()) if text(field(j, 'jobName')) == TIMER]
except:
jobs = None
if jobs is None:
note("Couldn't read the gateway timers (system.kanoa.config.getJobs). Check the Timers tab of the Kanoa gateway config page.")
else:
status = text(field(jobs[0], 'status')) if jobs else 'not found'
check(jobs and truthy(field(jobs[0], 'enabled')) and not status, 'The Quality Schedule gateway timer is running',
"It's %s. On the Timers tab of the Kanoa gateway config page (Kanoa > Settings), switch Quality Schedule on; "
'Kanoa Quality must be licensed (or in trial) and turned on in the app settings.' % status)
done = [e for e in events if text(field(e, 'chkShtStateName')) == 'Approved']
check(len(done) >= HOUR_OF_CHECKS, 'At least an hour of checks to analyze (%d approved in the last 24 hours)' % len(done),
'Let it run for at least an hour (%d checks) before the next lesson.' % HOUR_OF_CHECKS)
if done:
byName = eventItems(done[0])
empty = [n for n, a, u in ITEMS if not [r for r in byName.get(n, []) if text(field(r, 'chkItemValue'))]]
check(not empty, 'The latest check has a value for all three items',
'No value for %s. Check that attribute has an enabled data source on Spray Line 1.' % ', '.join(empty))
stuck = [e for e in events if text(field(e, 'chkShtStateName')) not in ('Approved', 'Submitted')]
if stuck:
note('%d check(s) in the last 24 hours were not completed automatically (%s). An auto check needs a value for every required item.'
% (len(stuck), ', '.join(sorted(set(text(field(e, 'chkShtStateName')) for e in stuck)))))
passed = results.count(True)
print '\n' + '=' * 70
print 'Result: %d of %d checks passed' % (passed, len(results))
if passed == len(results):
print 'All good. Coating SPC Checks is collecting data.'
else:
print 'Fix the FAIL items above, then run this again.'
print '=' * 70
check-sheet-coating-spc-checks.json: the finished Coating SPC Checks, including its attributes and data sources, to compare with your build.