cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Error when setting property value to a created thing

unnikrisvp
10-Marble

Error when setting property value to a created thing

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions

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...

View solution in original post

6 REPLIES 6
PaiChung
22-Sapphire I
(To:unnikrisvp)

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?

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

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...

Thanks Carles. However that didnt work. I modified the code to find the Thing after restart, however I am getting the same error.

 

Thing deviceThing = ThingUtilities.findThing(thingName);
		
if (deviceThing == null) {
	deviceThing = 
		ThingTemplateUtilities.createThingFromTemplate(
			"MyThing", thingName, node.getName(), true);
	deviceThing.EnableThing();
	deviceThing.RestartThing();
	deviceThing = ThingUtilities.findThing(thingName);
}
deviceThing.setPropertyValue("DeviceName", 
	new StringPrimitive(node.getName()));

Thanks Carles, it wasn't working as the extension code changes were not reflected after import. I had to remove the earlier version, restart the server and again import the latest extension. It worked after that.

Yes with Java extensions you need to restart each time you update it, it's a pain, that's why we have 99% of our code on Javascript Services.

Top Tags