Composer의 ThingWorx 모델 정의 > 모델링 > 사물 템플릿 > 원격 템플릿 > 사물 존재 > 사물 존재: ThingWorx Extension SDK를 사용하여 Java로 작성된 사용자 정의 보고 전략
사물 존재: ThingWorx Extension SDK를 사용하여 Java로 작성된 사용자 정의 보고 전략
이 자습서에서는 ThingWorx Extension을 만들기 위한 관련된 기본 사항에 대해서는 다루지 않습니다. 이 자습서에서는 다음과 같다고 가정합니다.
사용자가 ThingWorx Extension을 빌드하는 방법을 알고 있습니다.
보고 객체가 포함된 Extension SDK 8.4.0 이상이 있습니다.
사용자가 ThingWorx Extension을 빌드하고 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;
}

}
전략 사물 만들기
전략 사물은 사용자 정의 전략 템플릿을 확장하며 이를 통해 원격 엔티티에 대한 보고 구성 테이블에서 전략 사물을 보고 알고리즘으로 설정할 수 있습니다.
사물을 만들려면 프로젝트에 엔티티 폴더 경로(<Project Root>/Entites/Things)가 있어야 하고 사물에 이름 규약(Things_ThingName.xml, 여기서 ThingName은 사용자 정의 사물의 이름임)이 있어야 합니다.
이 예에서는 사물 이름이 "CustomStrategyJava"이므로 파일 이름은 Things_CustomStrategyJava.xml이고 콘텐츠는 아래와 같습니다. 줄 14, 19 및 23을 각각 ThingPackage, ThingNameThingTemplate 이름에 따라 적절히 편집해야 합니다.
* 
아래 줄을 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 서비스를 실행하여 언제든지 재평가될 수 있습니다(예: 타이머 사용).