示例:返工率 KPI
此示例中会创建一个名为“返工率”的新 KPI (ISO_22400)。返工率为工作单元、产品、生产订单或瑕疵类型的返工数量 (RQ) 与生产数量 (PQ) 之间的关系:
RQ/PQ
在本例中,返工数量 (RQ) KPI 元素一同创建。
1. 使用以下属性创建名为 ACME_CORP.KPI.ReworkRatioThingShape 的新事物形态:
ReworkRatio_currentValue“基本类型”=Number
ReworkRatio_lastCalculatedTime“基本类型”=DateTime
ReworkRatio_unitOfMeasure“基本类型”=String
确保各属性均选中“持续”复选框。
2. ACME_CORP.KPI.ReworkRatioThingShape 事物形态上实施以下服务:
Get_ReworkRatio_CurrentValue
Get_ReworkRatio_ThresholdValues
Get_ReworkRatio_Trend
ReworkRatio_Calculate
Set_ReworkRatio_CurrentValue
3. 在要计算此 KPI 的每种设备类型的事物模板上,覆盖 GetKPINames 服务。添加 ACME_CORP.KPI.ReworkRatioThingShape 事物形态,如以下示例代码所示:
var kpiInfoJSON = new Object();
// JSON of KPI information we want to test if they are implemented on "me"
// For custom KPI, please add into this JSON and following given format
// Warning the order is important
kpiInfoJSON.kpiInfoArray = [
{kpiThingShapeName: 'PTC.SCA.SCO.AvailabilityThingShape', kpiName: 'Availability'},
{kpiThingShapeName: 'PTC.SCA.SCO.QualityRatioThingShape', kpiName: 'QualityRatio'},
{kpiThingShapeName: 'PTC.SCA.SCO.EffectivenessThingShape', kpiName: 'Effectiveness'},
{kpiThingShapeName: 'PTC.SCA.SCO.OEEThingShape', kpiName: 'OEE'},
{kpiThingShapeName: 'ACME_CORP.KPI.ReworkRatioThingShape', kpiName: 'ReworkRatio'}
];
var result = Things["PTC.SCA.SCO.DefaultKPIManager"].GetImplementedKPIsOnThing({
thingName: me.name,
kpiInfo: kpiInfoJSON
});
4. 规定“返工率 KPI”的阈值。在 PTC.SCA.SCO.DefaultKPIManager 事物的“配置”页面,添加新的配置表。
a. PTC.SCA.SCO.DefaultKPIManager 事物上,添加具有以下值的配置表:
“表名称” - ReworkRatioThresholdValues
“数据形状” - PTC.SCA.SCO.KPIThresholdValues
“允许多行” - 确保选中此复选框。
b. 添加行至 ReworkRatioThresholdValues 配置表以获取所需的值范围,类似于以下图像:
5. 定义返工数量 (RQ) KPI 元素。
a. 在名为 reworkQuantity_RQACME_CORP.KPI.ReworkRatioThingShape 事物形态上创建属性,“基本类型”Number,并选中“持续”复选框。
b. 使用以下代码在名为 reworkQuantity_RQ_CalculateACME_CORP.KPI.ReworkRatioThingShape 事物形态上创建新的服务:
function isUndefinedOrNull(value){
return (value === null || typeof(value) === "undefined" );
}
result = me.reworkQuantity_RQ;
//Register first value to cache
me.RegisterValueToKPICache({
key: "reworkQuantity_RQ_firstValue" /* STRING */,
value: result, /* NUMBER */
TimeInfo : TimeInfo
});
c. 在要计算此 KPI 的每种设备类型的事物模板上,覆盖 GetKPIElementNames 服务。添加新属性至列表。例如:
result = "actualProductionTime_APT,goodQuantity_GQ,plannedBusyTime_PBT,plannedRunTimePerItem_PRI,producedQuantity_PQ,reworkQuantity_RQ";
d. 更新在步骤 2 中创建的 ReworkRatio_Calculate 服务以包含新的返工数量 (RQ) KPI 元素。例如:
var customLogger = logger.getLoggerContext().getLogger(logger.getName()+".com.ptc.sca.sco.KPICalculation.ReworkRatio");
var reworkRatio = me.reworkQuantity_RQ;
var producedQuantity = me.producedQuantity_PQ;
if (TimeInfo.getRowCount() > 0){
var row = TimeInfo.getRow(0);
var startTime = row.StartTimeDate;
var generatedID= row.GeneratedID;
// result from service GetKPIElementStartValue: INFOTABLE dataShape: "PTC.SCA.SCO.KPIElementValue"
var reworkQuantity_RQ_firstValue = me.GetKPICacheValue({
generatedID: generatedID/* STRING */,
name: "reworkQuantity_RQ_firstValue" /* STRING */
});
var producedQuantity_PQ_firstValue = me.GetKPICacheValue({
generatedID: generatedID/* STRING */,
name: "producedQuantity_PQ_firstValue" /* STRING */
});
var producedTotalQuantity = 0;
var producedReworkQuantity = 0;
if ( (typeof producedQuantity_PQ_firstValue !== 'undefined') && (typeof reworkQuantity_RQ_firstValue !== 'undefined') ){
producedTotalQuantity = (producedQuantity - producedQuantity_PQ_firstValue);
producedReworkQuantity = (reworkRatio - reworkQuantity_RQ_firstValue);
if ( producedTotalQuantity === 0 ){
// Until items are produced, rework ratio cannot be measured and will default to 1
// with producedReworkQuantity greater than 0, then the service will return 0.
reworkRatio = producedReworkQuantity > 0 ? 0: 1;
}
else{
reworkRatio = producedReworkQuantity / producedTotalQuantity;
}

customLogger.debug("ReworkRatio_Calculate: produced quantity value when time info started - last produced quantity :"+ producedQuantity_PQ_firstValue+"-"+producedQuantity);
customLogger.debug("ReworkRatio_Calculate: rework quantity value when time info started - last good quantity: "+ reworkQuantity_RQ_firstValue+"-"+reworkRatio);
customLogger.debug("ReworkRatio_Calculate: produced Rework Quantity / produced total quantity = quality ratio: " +producedReworkQuantity+ " / " + producedTotalQuantity + " = " + reworkRatio);
}
else{
customLogger.warn("ReworkRatio_Calculate for "+me.name+" could not retrieve the good quantity or produced quantity at the start of the time info.");
}
}
else{
customLogger.warn("ReworkRatio_Calculate for "+me.name+" could not retrieve the current time info. ReworkRatio = 0.");
}
var result = reworkRatio;
me.Set_ReworkRatio_CurrentValue({
value: result
});
var now = new Date();
me.ReworkRatio_lastCalculatedTime = now;