Embedded Service in Entity Template
Entity services can only apply to entity instances. Entity template, corresponding properties and instance used in following examples are listed below:
- Template: device. Namespace is factory.
- Property:
- code: string. Represents device code.
- name: string. Represents device name.
- workshop: string. Represents workshop of the device.
- first_use_time: Date. Represents the time when the device is first used.
- temperature: Float. Represents device temperature.
- pressure: Float. Represents device pressure.
- Instance: Alias is boiler1.info
Namespaces in examples can be skipped when the alias is unique.
getPropertyValue
{
"propName": "namespace.propertyName"
}
var inputs = {
"propName": "factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyValue.
{
//post body
"propName": "factory.temperature"
}
getPropertyValues
{
"propNames": "namespace.propertyName1,namespace.propertyName2"
}
{
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName1": 50,
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName2": 150
}
{
"propNames": "factory.pressure,factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyValues.
{
//post body
"propNames": "factory.pressure,factory.temperature"
}
getPropertyVQTValue
{
"propName": "namespace.propertyName"
}
{
"value": 18,
"tags": {
"name": "/templateNamespace/templateName/instanceName/propertyNamespace/propertyName",
"value": 50,
"status": "0",
"timeStamp": 1612355987068
}
}
var inputs = {
"propName": "factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyVQTValue.
{
//post body
"propNames": "namespace.propertyName1,namespace.propertyName2"
}
getPropertyVQTValues
{
"propNames": "namespace.propertyName"
}
{
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName1": {
"name": "/templateNamespace/templateName/instanceName/propertyNamespace/propertyName1",
"value": 50,
"status": "0",
"timeStamp": 1612356497037
},
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName2": {
"name": "/templateNamespace/templateName/instanceName/propertyNamespace/propertyName2",
"value": 150,
"status": "0",
"timeStamp": 1612356497037
}
}
var inputs = {
"propNames": "factory.pressure,factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyVQTValues.
{
//post body
"propNames": "factory.pressure,factory.temperature"
}
getPropertyLastValue
{
"propName": "namespace.propertyName"
}
var inputs = {
"propName": "factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyLastValue.
{
//post body
"propName": "factory.temperature"
}
getPropertyLastVQTValue
{
"propName": "namespace.propertyName"
}
{
"time": "2021-02-03T13:16:17Z",
"value": 50,
"status": "0"
}
var inputs = {
"propName": "factory.temperature"
}
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertyLastVQTValue.
{
//post body
"propName": "factory.temperature"
}
getPropertiesHistory
- fill: strategy of filling values.
- groupBy: Group query in time sequence.
- limit: Return data volume.
- offset: Offset value.
- select: Required. Search for full names of tags. You can define query strategy before tags such as first, max, sum and more.
- where: Required. Search time period. The minimum time is required.
{
"fill": {
"strategy": "previous"
},
"groupBy": {
"time": "300s"
},
"limit": 5,
"offset": 0,
"select": [
"first(\"templateNamespace.templateName.instanceName.propertyNamespace.propertyName\")"
],
"where": {
"and": {
"timestamp": {
"ge": "",
"gt": "2020-05-26T05:30:00Z",
"le": "",
"lt": "2020-05-26T06:15:01Z"
}
}
}
}
var inputs = {
"fill": {
"strategy": "previous"
},
"groupBy": {
"time": "300s"
},
"limit": 5,
"offset": 0,
"select": [
"first(\"factory.device.boiler1.factory.temperature\")"
],
"where": {
"and": {
"timestamp": {
"ge": "",
"gt": "2020-05-26T05:30:00Z",
"le": "",
"lt": "2020-05-26T06:15:01Z"
}
}
}
}
var param = {
"inputs": JSON.stringify(inputs)
}
var propValue = objects.boiler1.getPropertiesHistory(param)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getPropertiesHistory.
{
//post body
"fill": {
"strategy": "previous"
},
"groupBy": {
"time": "300s"
},
"limit": 5,
"offset": 0,
"select": [
"first(\"factory.device.boiler1.factory.temperature\")"
],
"where": {
"and": {
"timestamp": {
"ge": "",
"gt": "2020-05-26T05:30:00Z",
"le": "",
"lt": "2020-05-26T06:15:01Z"
}
}
}
}
getCertainHistory
- propNames: Namespace and alias of the property to be searched for.
- time: Query time.
- strategy: Query strategy. pre is the former value of the set query time and next is the next one.
{
"propNames": [
"namespace.propertyName1",
"namespace.propertyName2"
],
"time": "2021-01-01T12:00:00Z",
"strategy": "pre/next"
}
{
"map": {
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName1": 50,
"/templateNamespace/templateName/instanceName/propertyNamespace/propertyName2": 150
}
}
var inputs = {
"propNames": [
"factory.temperature",
"factory.pressure"
],
"time": "2021-01-01T12:00:00Z",
"strategy": "pre"
}
var param = {
"inputs": JSON.stringify(inputs)
}
var propValue = objects.boiler1.getCertainHistory(param)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/getCertainHistory.
{
//post body
"propNames": [
"factory.temperature",
"factory.pressure"
],
"time": "2021-01-01T12:00:00Z",
"strategy": "next"
}
setPropertyValue
- propName: Namespace and alias of the property to be searched for.
- propValue: Property value to be set.
{
"propName": "namespace.propertyName",
"propValue": 123
}
{
"success":true
}
var inputs = {
"propName": "factory.temperature",
"propValue": 123
}
var result = objects.boiler1.setPropertyValue(inputs)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/setPropertyValue.
{
//post body
"propName": "factory.temperature",
"propValue": 123
}
setPropertyValues
- key: Namespace and alias of the property to be searched for.
- value: Property value to be set.
{
"namsapace.propertyName1": "11",// key1: value1
"namsapace.propertyName2": "zhangsan"// key2: value2
}
{
"success":true
}
var inputs = {
"factory.temperature": 50,
"factory.pressure": 150
}
objects.boiler1.setPropertyValues(inputs)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/setPropertyValues.
{
//post body
"factory.temperature": 50,
"factory.pressure": 150
}
setPropertyVQTValues
- key: Namespace and alias of the property to be searched for.
- value: Property value set to be configured.
{
"namespace.propertyName": [//key
{ // value
"value": 234,
"timestamp": 1612408661716
}
]
}
{
"success":true
}
var param = {
"factory.temperature": [
{
"value": 50,
"timestamp": 1612408661716
}
],
"factory.pressure": [
{
"value": 150,
"timestamp": 1612408661716
}
]
}
var inputs = {
inputs: JSON.stringify(param)
}
objects.boiler1.setPropertyVQTValues(inputs)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/setPropertyVQTValues.
{
//post body
"factory.temperature": [
{
"value": 50,
"timestamp": 1612408661716
}
],
"factory.pressure": [
{
"value": 150,
"timestamp": 1612408661716
}
]
}
setPropertyDefaultValue
- propName: Namespace and alias of the property to be searched for.
- propValue: Property value to be set.
{
"propName": "namespace.propertyName",
"propValue": 123
}
{
"success":true
}
var inputs = {
"propName": "factory.temperature",
"propValue": 123
}
var result = objects.boiler1.setPropertyDefaultValue(inputs)
{domain}/runtime/factory/template/device/instance/boiler1/service/system/setPropertyVQTValues.
{
//post body
"propName": "factory.temperature",
"propValue": 123
}
Embedded Service in Form Template
Form template services can only apply to form templates. Template and corresponding properties used in following examples are listed below:
- Template: user. Namespace is work.
- Property:
- uid: string. Represents staff code.
- name: string. Represents staff name.
- dep: string. Represents the department to which the staff belongs.
- age: Integer. Represents satff age.
- employ_time: Date. Represents the start date of the staff.
AddDataTableEntry
- key: Property alias.
- value: Property value to be set.
{ //key: value
"uid": "01",
"name": "Emma",
"age": 30
}
// input parameter is an object
var inputs = {
uid: "01",
name: "Emma",
age: 30
};
templates["work.user"].AddDataTableEntry(inputs);
{domain}/runtime/work/template/user/service/system/AddDataTableEntry.
{
//post body
"uid": "01",
"name": "Emma",
"age": 30
}
AddDataTableEntry
- key: Property alias.
- value: Property value to be set.
{ //key: value
"uid": "01",
"name": "Emma",
"age": 30
}
// input parameter is an object
var inputs = {
uid: "01",
name: "Emma",
age: 30
};
templates["work.user"].AddDataTableEntry(inputs);
{domain}/runtime/work/template/user/service/system/AddDataTableEntry.
{
//post body
"uid": "01",
"name": "Emma",
"age": 30
}
AddDataTableEntries
{
"list":[
{
"name": "Emma",
"age": 30
},
{
"system.name": "Jason",
"system.age": 40
}
]
}
var param = {
list: [
{
name: "Emma",
age: 30
},
{
name: "Jason",
age: 40
}
]
};
var params = {
params: JSON.stringify(param)
}
// leave out the namespace when accessing template
templates["work.user"].AddDataTableEntries(params);
{domain}/runtime/work/template/user/service/system/AddDataTableEntry.
{
//post body
"list":[
{
"name": "Emma",
"age": 30
},
{
"system.name": "Jason",
"system.age": 40
}
]
}
UpdateDataTableEntry
- update: Alias and value of the property to be updated.
- where: Conditions of the property. Multiple conditions are separated by commas.
Only properties that conform to the defined conditions will be updated with set values and conditions cannot be empty.
{
"update": {
"age": 40,
"grade": 3
},
"where": {
"name": "Emma",
"dep": "Dev"
}
}
var inputs = {
update: {
age: 40,
grade: 3
},
where: {
name: "Emma",
dep: "Dev"
}
}
templates["work.user"].UpdateDataTableEntry(inputs);
{domain}/runtime/work/template/user/service/system/UpdateDataTableEntry.
{
//post body
"update": {
"age": 40,
"grade": 3
},
"where": {
"name": "Emma",
"dep": "Dev"
}
}
DeleteDataTableEntries
{
"name": "Emma",
"dep": "Dev"
}
var inputs = {
name: "Emma",
dep: "Dev"
};
templates.user.DeleteDataTableEntries(inputs);
{domain}/runtime/work/template/user/service/system/DeleteDataTableEntries.
{
//post body
"name": "Emma",
"dep": "Dev"
}
GetDataTableEntries
- fields: Return fields.
- distinct: Whether to deduplicate.
- pageIndex: Start page of the query. It is 1 by default.
- pageSize: Page size of the query. It is 50 by default.
- order: Sort the data. order means sorting field, sort is sorting method, desc/asc is descending or ascending order.
- condition: Search conditions.
When using fuzzy match, you can only use and and or as operator.
{
"fields": [],
"distinct":false,
"pageIndex":1,
"pageSize":50,
"order": [
{
"order": "name",
"sort": "desc"
}
],
"condition": { //both "and" and "or" conditions need to be met.
"and": { // conditions are: name is Emma and age is 20
"name": "Emma",
"age": 20
},
"or": { // conditions are: name is Jason or age is 30
"name": "Jason",
"age": 30
}
}
}
{
"pagination": {
"current": 1,
"total": 1,
"pageSize": 10
},
"list": [
{
"work.uid": "user_fdbaa34tbvs",
"work.name": "zhangsan",
"work.age": 32,
"work.dep": "销售部",
"work.app_name": "system",
"work.creator_id": "1025876580291488",
"work.last_modifier_id": "1025876580291488",
"work.created_time": "2021-02-03 12:32:12",
"work.last_modified_time": "2021-02-03 12:32:12"
}
]
}
var param = {
"fields": ["name", "age"],
"pageIndex":1,
"pageSize":10,
"order": [
{
"order": "name",
"sort": "desc"
}
],
"condition": {
"and": {
"name": "Emma",
"age": 20
}
}
}
var params = {
params: JSON.stringify(param)
}
var data = templates["work.user"].GetDataTableEntries(params);
{domain}/runtime/work/template/user/service/system/GetDataTableEntries.
{
//post body
"fields": ["name", "age"],
"pageIndex":1,
"pageSize":10,
"order": [
{
"order": "name",
"sort": "desc"
}
],
"condition": {
"and": {
"name": "Emma",
"age": 20
}
}
}
getDatatableDetails
{
"data": [
{
"columns": [
{
"name": "name",
"type": "varchar"
},
{
"name": "age",
"type": "int"
},
{
"name": "code",
"type": "varchar"
},
{
"name": "dataCategory",
"type": "varchar"
}
],
"tableName": "tbl_weiqyTableObj_weiqyTableDataStruct"
}
],
"error": false
}
querySQLTest
{
"data": [
"system_id",
"system_created_time",
"system_last_modified_time",
"system_creator_id",
"system_last_modifier_id",
"system_app_name",
"system_app_access_mode",
"zhuzh_za_name",
"zhuzh_za_age"
]
}
querySQLExec
{
"sql": "select * from table1 where name=${name} and age=${age}",
"params": [{
"name": "name",
"value": "zhangsan"
}, {
"name": "age",
"value": 10
}
],
"pageSize": 200
}
{
"total": 2,
"data": {
"columnNames": ["system_id", "system_created_time", "system_last_modified_time", "system_creator_id", "system_last_modifier_id", "system_app_name", "system_app_access_mode", "scp_scp_names", "scp_scp_ages", "scp_scp_xx"],
"dataSource": [
{
"scp_scp_xx": "123",
"system_creator_id": "1376878722077440",
"system_app_name": "App_2467085462006d991209bd7597024b28",
"system_id": 10773,
"system_last_modifier_id": "1376878722077440",
"system_app_access_mode": "PUBLIC",
"scp_scp_names": "Jay",
"system_created_time": "2021-06-11 01:17:32",
"system_last_modified_time": "2021-06-11 01:17:32",
"scp_scp_ages": "123"
},
{
"scp_scp_xx": "33",
"system_creator_id": "1376878722077440",
"system_app_name": "App_2467085462006d991209bd7597024b28",
"system_id": 10776,
"system_last_modifier_id": "1376878722077440",
"system_app_access_mode": "PUBLIC",
"scp_scp_names": "Jason2",
"system_created_time": "2021-06-11 01:19:41",
"system_last_modified_time": "2021-06-11 01:19:41",
"scp_scp_ages": "33"
}
]
}
}
Common Services
This chapter introduces some common services you can use by writing script.
Get Auto-increment Value
var result = services['SerialNumberService'].acquire();
result;
Get Aggregated Result in InfluxDB
function influxDbDataFun(expressStr, objName, propName, startTimeUtcStr, endTimeUtcStr) {
var objProp = objName + '.' + propName;
log.debug("objProp=" + objProp);
var query = {
"select": [expressStr + "(\"" + objProp + "\")"],
"where": {
"and": {
"timestamp": {
"ge": startTimeUtcStr,
"lt": endTimeUtcStr
}
}
}
}
var resultHistoryJsonStr = ObjectPool.get('testInstance').executeService('getPropertiesHistory', {inputs: JSON.stringify(query)});
log.debug('resultHistoryJsonStr = ' + resultHistoryJsonStr);
return resultHistoryJsonStr.results[0].datas[0];
}
var result = influxDbDataFun('sum', 'syetem.testTemplate', 'system.p1', '2021-04-10T00:00:00Z', '2021-04-15T00:00:00Z');
result;
Parameter | Description |
---|---|
expressStr | Expressions such as sum/mean/first/last/max/min |
objName | Template namespace.template alias.instance alias |
propName | Property namespace.property alias |
startTimeUtcStr | Start time such as '2019-07-10T17:08:47Z' |
endTimeUtcStr | End time such as '2019-07-12T17:11:47Z' |
History Value Query
var query = {
select: [
"first(\"system.testTemplate.testInstance.system.p4\")"
],
where: {
and: {
timestamp: {
"ge": "2021-04-10T00:00:00Z",
"le": "2021-04-15T00:00:00Z"
}
}
},
groupBy: {
time: "1s,1s"
},
fill: {
"strategy": "previous"
},
offset: 0,
limit: 5
}
var result = ObjectPool.get('testInstance').executeService('getPropertiesHistory',{inputs: JSON.stringify(query)});
result;
> and >> are marks for nesting.
Key | Value | Type | Description |
---|---|---|---|
select | ["function("template namespace.template alias.instance alias.property namespace.property alias")"] or [""template namespace.template alias.instance alias.property namespace.property alias""] | String array | Required. The selected object property and aggregation methods (mean/sum/max/min) only support numberic parameters, and first/last/count supports all types of data. |
where | ("and":()) | Object | Required. Filter conditions. Presently only and is available. |
>and | ("timestamp":()) | Object | Filter conditions. Presently only timestamp is available. |
>>timestamp | ("ge":"2020-04-01T00:00:00Z,"le":"2020-04-01T00:00:00Z"") | Object | Time period. ge, gt, lt and le are available. |
groupBy | ("time":"") | Object | Grouping conditions. Only time is available. |
>time | "1s,1s" | String | Data granularity. Supports s, m, h, d, w and m. The first parameter is data granularity and the last is grouping offset. |
fill | ("strategy":"") | Object | Fill strategy. Only previous is available. |
>strategy | "previous" | Get the data from previous group when current group is empty | - |
offset | Positive integer | Integer | Value offset. |
limit | Positive integer | Integer | Pieces of data. |
{
"unauthorized": [],
"results": [
{
"datas": [
[
"2021-04-10T00:00:00Z",
null,
null
],
[
"2021-04-10T00:00:01Z",
null,
null
],
[
"2021-04-10T00:00:02Z",
null,
null
],
[
"2021-04-10T00:00:03Z",
null,
null
],
[
"2021-04-10T00:00:04Z",
null,
null
]
],
"authorized": true,
"function": "first",
"name": "/system/testTemplate/testInstance/system/p4",
"fields": [
"time",
"value",
"status"
]
}
]
}
> and >> are marks for nesting.
Key | Value | Type | Description |
---|---|---|---|
name | /template namespace/template alias/instance alias/property namespace/property alias | String | Full path of the selected object property. |
function | Aggregation function | String | Aggregation methods such as mean/sum/max/min only support numberic parameters, and first/last/count supports all types of data. |
>fields | ["time","value","status"] | String array | Return fields such as time/value/status. |
>datas | ["2021-04-10T00:00:04Z",null,null] | 2D array | Value after grouping (null means empty). |
Service Calls Service
var inputs = {
propValue:'test111', //String property value
propName:'p1' //Stringproperty name
};
ObjectPool.get('testInstance').executeService('setPropertyValue',inputs);
var httservice = services["HttpClientService"];
var result = httpservice.getString(baseUrl,3000);