Composer 中的 ThingWorx 模型定義 > 建模 > 物範本 > 遠端範本 > 物件存在 > 物件存在:使用 ThingWorx Extension SDK 在 Java 中建立的自訂報告策略
物件存在:使用 ThingWorx Extension SDK 在 Java 中建立的自訂報告策略
本教學專區不包含建立 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 服務,隨時重新評估此狀態 (例如,透過計時器)。