Skip to main content
1-Visitor
May 17, 2018
Solved

Error when setting property value to a created thing

  • May 17, 2018
  • 6 replies
  • 3738 views

I am trying to create an extension, which provides a Template having a service for dynamic custom thing creation.

 

After installing the extension and creating a Thing from the extension template, i am executing the createThing service. I am successfully able to create the Things dynamically using the below code, however my property is not getting updated.

 

public static void createSomeThing(DeviceNode node) throws Exception {
		
	String thingName = "MT-" + node.getName();
	Thing deviceThing = ThingUtilities.findThing(thingName);
		
	if (deviceThing == null) {
		deviceThing = ThingTemplateUtilities.createThingFromTemplate("MyTemplate", thingName, node.getName(), true);
	}
	deviceThing.EnableThing();
	deviceThing.RestartThing();
		
	if (deviceThing.GetValueStream() == null) {
		String streamName = thingName+"Stream";
		Thing deviceVS = ThingTemplateUtilities.createThingFromTemplate(
				"ValueStream", streamName, "Value stream for " + node.getName(), true);
		deviceVS.EnableThing();
		deviceVS.RestartThing();
		deviceThing.SetValueStream(streamName);
	}
	deviceThing.setPropertyValue("DeviceName", 
			new StringPrimitive(node.getName() + "-" + node.getUuid()));
}

This is how the Thing is defined:

@ThingworxBaseTemplateDefinition(name = "GenericThing")
@ThingworxPropertyDefinitions(properties = {
		@ThingworxPropertyDefinition(name = "DeviceName", description = "", category = "", baseType = "STRING", isLocalOnly = false, aspects = {"dataChangeType:VALUE", "isReadOnly:true", "isLogged:true" }) })
public class MyTemplate extends Thing {

 // Some basic stuff
}

I get the below error in the logs once the service is executed.

2018-05-17 14:27:59.957+0530 [L: ERROR] [O: c.t.t.Thing] [Device03] [U: Administrator] [S: ] [T: http-nio-8080-exec-9] Attempt To Write Property [DeviceName] Failed - Thing Not Running
2018-05-17 14:27:59.957+0530 [L: WARN] [O: c.t.t.Thing] [I: ] [U: Administrator] [S: ] [T: http-nio-8080-exec-9] Thing [Device03] has logged properties but does not have a valid value stream assigned

Please help on how to resolve the above error.

 

Best answer by CarlesColl

At least with Server Side Javascript happens, I don't know if with Java it's the same.
When you Enable/Restart a Thing, you need to point to the new instance in memory of the thing, as you restarted it and the old one it's destroyed and it creates a new one.

Then your code after 

deviceThing.RestartThing();

should search again for the new instance of the thing:

deviceThing = ThingUtilities.findThing(thingName);

 otherwise you are working with the old In-Memory Instance of the thing...

6 replies

22-Sapphire I
May 17, 2018

Looks like for some reason your Enable/Restart is not working? 

Do the Things properly instantiate in composer?

What messages are in the Thingworx application log?

1-Visitor
May 17, 2018

The Things are instantiated properly and can be viewed from the Console. Its only that the properties are not set and value stream is not created.

 

Also i noticed that i receive a different error while running the service for 1st time

Attempt To Write Property [DeviceName] Failed - Thing Not Running

All subsequent executions return the below error:

Thing [~] has logged properties but does not have a valid value stream assigned
1-Visitor
May 17, 2018

At least with Server Side Javascript happens, I don't know if with Java it's the same.
When you Enable/Restart a Thing, you need to point to the new instance in memory of the thing, as you restarted it and the old one it's destroyed and it creates a new one.

Then your code after 

deviceThing.RestartThing();

should search again for the new instance of the thing:

deviceThing = ThingUtilities.findThing(thingName);

 otherwise you are working with the old In-Memory Instance of the thing...