Composer での ThingWorx モデルの定義 > モデル化 > Thing Template > リモートテンプレート > Thing の存在 > Thing の存在: ThingWorx Extension SDK を使用して Java で作成されたカスタムレポートストラテジー
Thing の存在: ThingWorx Extension SDK を使用して Java で作成されたカスタムレポートストラテジー
このチュートリアルでは、ThingWorx 拡張機能の作成の基本については説明していません。ここでは以下を前提としています。
ThingWorx 拡張機能の構築方法について理解している。
レポートオブジェクトが含まれている、Extension SDK v.8.4.0 以降がインストールされている。
拡張機能を構築して ThingWorx Platform にインポートする方法について理解している。
Java、または JavaScript と XML で、カスタムストラテジー Thing を作成できます。最初に、ストラテジー Thing Template を作成し、ReportingAlgorithm メソッドを実装してオーバーライドする必要があります。その後で、ストラテジー Thing を作成します。このトピックでは、Java でストラテジーを作成する手順について説明します。JavaScript を使用してストラテジー Thing を作成するには、 Thing の存在: ThingWorx Composer を使用したカスタム ReportingStrategy Thing の作成を参照してください。
ストラテジー Thing Template の作成
Java でカスタムストラテジーを作成するには、最初にストラテジー Thing Template を作成します。Thing Template は Java ベースのアルゴリズムのコードベースとして機能します。Thing Template のクラス定義は以下のようになります。
@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;
}
サンプルカスタムストラテジー Thing Template
サンプルカスタムストラテジー Thing Template は以下の例のようになります。この例では、カスタムストラテジーは Thing が接続されていない場合は Thing が存在するものとしてマークし、接続されている場合には存在しないものとしてマークします。

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;
}

}
ストラテジー Thing の作成
ストラテジー Thing はカスタムストラテジーテンプレートを拡張し、ユーザーがリモートエンティティ上のレポートコンフィギュレーションテーブルでレポートアルゴリズムとしてカスタムストラテジーテンプレートを設定できるようにします。
Thing を作成するには、プロジェクトのエンティティフォルダパスが <プロジェクトルート>/Entites/Things であり、Thing の命名規則が Things_ThingName.xml (ここで、ThingName はカスタム Thing の名前) である必要があります。
この例では、Thing 名は "CustomStrategyJava" なので、ファイル名は Things_CustomStrategyJava.xml となり、コンテンツは以下に示すようになります。14、19、23 行をそれぞれ ThingPackageThingName、および ThingTemplate の名前に基づいて適宜編集する必要があります。
* 
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 から Thing がバインドまたはバインド解除されると必ず、Remote Thing の isReporting 状態が再評価されます。この状態は、Remote Thing で使用可能な EvaluateReporting サービスを実行することによって、(タイマーなどによって) いつでも再評価できます。