Skip to main content
12-Amethyst
August 12, 2024
Question

Creating properties inside Thing by service

  • August 12, 2024
  • 1 reply
  • 1142 views

Needed to copy some properties from one Thing to other. To avoid manual work, created Service with AddPropertyDefinition (later wanted to add service to copy values). It creates no error when running first time, but properties are not generated. So, started script - and it answer "Property [name] already exists".

Manual refreshing of Thing in web service does not help. Restart Thing by code also makes no difference.

 

Here is sample code:

 

myList = me.GetPropertyDefinitions({
category: "MyCategory" /* STRING */,
type: undefined /* BASETYPENAME */,
dataShape: undefined /* DATASHAPENAME */
});
 
for each (var x in myList.rows) {
Things["MyThing"].AddPropertyDefinition({
indexed: undefined /* BOOLEAN {"defaultValue":false} */,
defaultValue: undefined /* STRING */,
remoteBindingAspects: undefined /* JSON */,
description: "my desc"/* STRING {"defaultValue":""} */,
readOnly: undefined /* BOOLEAN {"defaultValue":false} */,
type: 'INFOTABLE' /* BASETYPENAME */,
remote: undefined /* BOOLEAN {"defaultValue":false} */,
remotePropertyName: undefined /* STRING */,
timeout: undefined /* INTEGER {"defaultValue":0} */,
pushType: undefined /* STRING */,
accessModifier: undefined /* JSON */,
dataChangeThreshold: undefined /* NUMBER */,
logged: undefined /* BOOLEAN {"defaultValue":false} */,
name: x.name /* STRING */,
pushThreshold: undefined /* NUMBER */,
dataChangeType: undefined /* STRING */,
category: "MyCategory" /* STRING */,
persistent: true /* BOOLEAN {"defaultValue":false} */,
dataShape: 'myDataShape' /* DATASHAPENAME */
});
Things["MyThing"].RestartThing();
Things["MyThing"].EnableThing();
 

1 reply

Rocko
19-Tanzanite
August 12, 2024

The line

category: MyCategory" /* STRING */,

misses the opening quote?

Also the line

description: ''my desc"/* STRING {"defaultValue":""} */,

uses two different types of quote, single and double.

ZbigniewK12-AmethystAuthor
12-Amethyst
August 12, 2024

That happens during anonymising code. In original code, it's done correctly, if not - it would generate error. 

19-Tanzanite
August 13, 2024

Hi @ZbigniewK 

 

If you want to copy all the property, you can use below code

// result: INFOTABLE dataShape: "PropertyDefinition"
let thing1PropertyInfoTable = Things["T"].GetPropertyDefinitions({
	category: undefined /* STRING */,
	type: undefined /* BASETYPENAME */,
	dataShape: undefined /* DATASHAPENAME */
});

// status: INFOTABLE dataShape: "BulkProcessingReport"
let result = me.AddPropertyDefinitions({
	ignoreInvalidDefinitions: true /* BOOLEAN */,
	values: thing1PropertyInfoTable /* INFOTABLE */
});

 

To add specific properties

// result: INFOTABLE dataShape: "PropertyDefinition"
let result = Things["T"].GetPropertyDefinitions({
	category: undefined /* STRING */ ,
	type: undefined /* BASETYPENAME */ ,
	dataShape: undefined /* DATASHAPENAME */
});

let propertyList = ["prop1", "prop2", "G"];

result.rows.toArray().forEach(jsonValue => {
	if (propertyList.indexOf(jsonValue.name) !== -1) {
		me.AddPropertyDefinition({
			defaultValue: undefined /* STRING */ ,
			remoteBindingAspects: undefined /* JSON */ ,
			description: jsonValue.description /* STRING */ ,
			readOnly: jsonValue.isReadOnly /* BOOLEAN */ ,
			remote: undefined /* BOOLEAN */ ,
			type: jsonValue.baseType /* BASETYPENAME */ ,
			remotePropertyName: undefined /* STRING */ ,
			timeout: undefined /* INTEGER */ ,
			pushType: undefined /* STRING */ ,
			dataChangeThreshold: undefined /* NUMBER */ ,
			logged: jsonValue.isLogged /* BOOLEAN */ ,
			name: jsonValue.name /* STRING */ ,
			pushThreshold: undefined /* NUMBER */ ,
			dataChangeType: undefined /* STRING */ ,
			category: undefined /* STRING */ ,
			persistent: jsonValue.isPersistent /* BOOLEAN */ ,
			dataShape: undefined /* DATASHAPENAME */
		});
	}
});

 

Note : Run above script in View Only mode

 

/VR