Composer 中的 ThingWorx 模型定义 > 建模 > 事物模板 > 远程模板 > 事物存在 > 事物存在:在 Java 中使用 ThingWorx Extension SDK 创建的自定义报告策略
事物存在:在 Java 中使用 ThingWorx Extension SDK 创建的自定义报告策略
本教程不涉及有关创建 ThingWorx 扩展的基础知识。假定
您知道如何构建 ThingWorx 扩展。
您已安装包括报告对象的 Extension SDK 8.4.0 或更高版本。
您知道如何构建扩展并导入 ThingWorx Platform 中。
自定义策略物件可以在 Java 或 JavaScript 以及 XML 中创建。首先,您需要创建一个策略事物模板并实现和覆盖 ReportingAlgorithm 方法。然后,创建一个策略事物。本主题将介绍在 Java 中创建策略的步骤。要使用 JavaScript 创建策略事物,请参阅 事物存在:使用 ThingWorx Composer 创建自定义 ReportingStrategy 事物
创建策略事物模板
要在 Java 中创建自定义策略,首先要创建一个策略事物模板。事物模板可充当基于 Java 算法的代码库。事物模板的类定义应如下所示:
@ThingworxBaseTemplateDefinition(name = "ReportingStrategy")
public class MyJavaStrategy extends ReportingStrategy {
实现并覆盖 ReportingAlgorithm 方法
接下来,您需要实现并覆盖 ReportingAlgorithm 方法。此方法包含正常运行所需的许多注释。确保正确实现的最简单解决方案是复制以下代码:
@Override
@ThingworxServiceDefinition(name = ReportingConstants.REPORTING_ALGORITHM_SERVICE_NAME, isAllowOverride = true,
description = "Evaluates a Thing to determine if it is Present")
@ThingworxServiceResult(name = CommonPropertyNames.PROP_RESULT, baseType = "BOOLEAN",
description = "Whether the parameters describe a Device that is Reporting normally")
public Boolean ReportingAlgorithm(
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_NAME,
description = "Name of the event",
baseType = "STRING" ) String eventName,
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_TIME,
description = "Time event is triggered",
baseType = "DATETIME" ) DateTime eventTime,
@ThingworxServiceParameter(
name = ReportingConstants.SOURCE,
description = "Name of Thing that triggered event",
baseType = "STRING" ) String source,
@ThingworxServiceParameter(
name = ReportingConstants.SOURCE_PROPERTY,
description = "Soure property",
baseType = "STRING" ) String sourceProperty,
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_DATA,
description = "Parameters necessary for the algorithm to determine if a Thing is reporting",
baseType = "INFOTABLE") InfoTable eventData) {
// specifically boolean primitive; this method should never return null
boolean result = false;

// TODO: Custom Strategy implementation here

return result;
}
示例自定义策略事物模板
示例自定义策略事物模板应类似于以下示例。在本示例中,自定义策略在事物未连接时将其标记为存在,而在事物连接时将其标记为不存在。

import org.joda.time.DateTime;
import org.slf4j.Logger;

import com.thingworx.logging.LogUtilities;
import com.thingworx.metadata.annotations.ThingworxBaseTemplateDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceParameter;
import com.thingworx.metadata.annotations.ThingworxServiceResult;
import com.thingworx.things.connected.reportable.ReportingConstants;
import com.thingworx.things.connected.reportable.ReportingStrategy;
import com.thingworx.types.InfoTable;
import com.thingworx.types.constants.CommonPropertyNames;

@ThingworxBaseTemplateDefinition(name = "ReportingStrategy")
public class MyJavaStrategy extends ReportingStrategy {

private static final Logger logger =
LogUtilities.getInstance().getApplicationLogger(MyJavaStrategy.class);

@Override
@ThingworxServiceDefinition(name = ReportingConstants.REPORTING_ALGORITHM_SERVICE_NAME,
isAllowOverride = true,
description = "Evaluates a Thing to determine if it is Present")
@ThingworxServiceResult(name = CommonPropertyNames.PROP_RESULT, baseType = "BOOLEAN",
description = "Whether the parameters describe a Device that is Reporting normally")
public Boolean ReportingAlgorithm(
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_NAME,
description = "Name of the event",
baseType = "STRING" ) String eventName,
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_TIME,
description = "Time event is triggered",
baseType = "DATETIME" ) DateTime eventTime,
@ThingworxServiceParameter(
name = ReportingConstants.SOURCE,
description = "Name of Thing that triggered event",
baseType = "STRING" ) String source,
@ThingworxServiceParameter(
name = ReportingConstants.SOURCE_PROPERTY,
description = "Soure property",
baseType = "STRING" ) String sourceProperty,
@ThingworxServiceParameter(
name = ReportingConstants.EVENT_DATA,
description = "Parameters necessary for the algorithm to determine if a Thing is reporting",
baseType = "INFOTABLE") InfoTable eventData) {
// specifically boolean primitive; this method should never return null
boolean result;
try {
boolean isBound = (Boolean) eventData.getFirstRow().getValue(ReportingConstants.PROP_ISBOUND);
result = !isBound;
logger.info("Custom strategy has is bound [" + isBound + "].
Setting reporting to [" + result + "]");
} catch (Exception e) {
logger.error("Caught exception while evaluating reporting algorithm.
Marking as not reporting");
result = false;
}
return result;
}

}
创建策略事物
策略事物可扩展您的自定义策略模板,并允许您将其设置为远程实体的报告配置表中的报告算法。
要创建事物,您的项目必须具备如下所示的实体文件夹路径,即<项目根>/Entites/Things,同时,事物必须具备以下命名约定 Things_ThingName.xml,其中 ThingName 为自定义事物的名称。
在本示例中,事物名称为 "CustomStrategyJava",因此文件名为 Things_CustomStrategyJava.xml,内容如下所示。您需要根据 ThingPackageThingNameThingTemplate 名称分别对 14、19 和 23 行进行编辑。
* 
将以下行复制到 IDE 或 XML 编辑器后,您将看到正确的行号。
<?xml version="1.0" encoding="UTF-8"?>
<Entities
<Things>
<Thing
effectiveThingPackage="MyStrategyPackage"
enabled="true"
name="CustomStrategyJava"
published="false"
thingTemplate="MyJavaStrategy"
</Thing>
</Things>
</Entities>
执行策略
自 ThingWorx 8.4.0 版本起,只要事物与 ThingWorx Platform 绑定或解除绑定,就会重新评估远程事物的 isReporting 状态。也可通过执行远程事物上的 EvaluateReporting 服务,随时 (例如,通过计时器) 对此状态进行重新评估。