Thingworx平台configuration接口应用方法
程序员文章站
2022-03-22 14:33:40
...
在JavaSDK二次开发中经常会用到一些固定的配置参数,如数据库的操作调用。本文主要介绍如何简单的调用configuration中的参数,先创建一个ThingTemplate,然后创建两个configuration,创建一个servic,在创建configuration时配置的参数可有初始值,而如果希望输入值为**(即不可见),则可将dataShape中baseType改为“PASSWORD”,在实际提取配置值时则强制转换为所需的数据类型。
configuration具体API的方法应用见代码及注释(附带执行结果和API示例):
package demo_test;
import com.thingworx.logging.LogUtilities;
import com.thingworx.metadata.annotations.ThingworxBaseTemplateDefinition;
import com.thingworx.metadata.annotations.ThingworxConfigurationTableDefinition;
import com.thingworx.metadata.annotations.ThingworxConfigurationTableDefinitions;
import com.thingworx.metadata.annotations.ThingworxDataShapeDefinition;
import com.thingworx.metadata.annotations.ThingworxFieldDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceResult;
import com.thingworx.things.Thing;
import org.slf4j.Logger;
//**示例:实现Configuration中参数的调用
@ThingworxBaseTemplateDefinition(name = "GenericThing")
//创建配置表
@ThingworxConfigurationTableDefinitions(
tables = {
//配置1:配置名:configuration_a,两个值:inputa不可见,input_b可见
@ThingworxConfigurationTableDefinition(name = "configuration_a", description = "", isMultiRow = false, ordinal = 0,
dataShape = @ThingworxDataShapeDefinition(
fields = {
//配置1值:其中input_b配置为有默认值
@ThingworxFieldDefinition(name = "inputa", description = "", baseType = "PASSWORD", ordinal = 0),
@ThingworxFieldDefinition(name = "input_b", description = "", baseType = "STRING", ordinal = 1,aspects = {
"defaultValue:jdbc:sqlserver://hostIP:1433;database=DatabaseName" }) })),
//配置2:配置名:configuration_b,两个值:input_c不可见,input_d可见
@ThingworxConfigurationTableDefinition(name = "configuration_b", description = "", isMultiRow = false, ordinal = 0,
dataShape = @ThingworxDataShapeDefinition(
fields = {
//配置2值:
@ThingworxFieldDefinition(name = "input_c", description = "", baseType = "PASSWORD", ordinal = 0),
@ThingworxFieldDefinition(name = "input_d", description = "", baseType = "STRING", ordinal = 1) }))})
//创建ThingTemplate类
public class demoThingTemplate extends Thing {
private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(demoThingTemplate.class);
public demoThingTemplate() {
// TODO Auto-generated constructor stub
}
//创建Service1:
@ThingworxServiceDefinition(name = "thingtemplateTest", description = "", category = "", isAllowOverride = false, aspects = {
"isAsync:false" })
@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})
//service程序入口
public String thingtemplateTest() throws Exception {
_logger.trace("Entering Service: thingtemplateTest");
//获取配置1的值:
String input_a=(String)getConfigurationSetting("configuration_a","inputa");
String input_b=(String)getConfigurationSetting("configuration_a","input_b");
//配置input_b的初始值:
setConfigurationSetting("configuration_a","input_b",input_b);
SaveConfigurationTables();
//获取配置2的值:
String input_c=(String)getConfigurationSetting("configuration_b","input_c");
String input_d=(String)getConfigurationSetting("configuration_b","input_d");
//测试输出:
String output=input_a+input_b+input_c+input_d;
_logger.trace("Exiting Service: thingtemplateTest");
return output;
}
代码extension后导入平台中的执行结果:
API官方文档的的示例:
上一篇: C#类型转换
下一篇: 自动化机器上的物联网网关的目的是什么?