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
I am trying to create a service that pulls a list of properties from a webservice to create multiple Things of the same type. I am able to create multiple things in the webservice but cannot find a way to refer to the newly created things in the service so that I can enable and restart.
Here is the code I am trying to run:
/// Loops through a JSON object to extract properties for each thing
while (i < json_obj.devices.length)
{
var params = {
name: json_obj.devices[i].deviceId/* STRING */,
description: json_obj.devices[i].name /* STRING */,
thingTemplateName: 'XXXXXX' /* THINGTEMPLATENAME */
};
// no return
Resources["EntityServices"].CreateThing(params);
i++;
//
//Need to enable and restart newly created thing. Not sure how to reference it
}
Solved! Go to Solution.
The handle is the name you assigned the thing when you created it:
var params = {
name: "NewThingName",
thingTemplateName: "GenericThing"
};
Resources["EntityServices"].CreateThing(params);
Things["NewThingName"].Enable();
Things["NewThingName"].Restart();
Here is some helpful info:
I can think of a couple of different ways:
1. Enable and restart the Things in the same loop where you created them.
2. Create another loop with the same loop parameters you used to create them and enable/restart in that loop.
3. Save the name of the newly created things in an array and pass that array to a function that enables/restarts them
I would like to do it in the loop. Once I create them how do I call the service to enable and restart. I'm not sure how to get the handle to the newly created thing.
The handle is the name you assigned the thing when you created it:
var params = {
name: "NewThingName",
thingTemplateName: "GenericThing"
};
Resources["EntityServices"].CreateThing(params);
Things["NewThingName"].Enable();
Things["NewThingName"].Restart();
Here is some helpful info: