问题
I am new to ThingWorx and I want to get some practical flavour of implementing services on this example. I have such data model:
Thing 'Car' has Thing 'Sensor'(Infotable)
I want to have service of CarTemplate that will return all implemented Cars and instead of Sensor's Object it will return Sensor's 'name' property.
What I have now:
"Car1Name" | SensorObject
What I want:
"Car1Name" | "Accelerator1Name"
Please, help me to make it happens.
回答1:
There's no kind of "Static" services on ThingTemplates, if you want to recover all Implementing things of a ThingTemplate with properties values you should build a Thing Helper.
What's a Thing Helper? It's another thing, call it whatever you want, let's say CarHelpers, which has a Service called GetCarsWithSensors, which does a ThingTemplates["ThingTemplateName"].GetImplementingThings(), or a GetImplementingThingsWithData and returns the desired Infotable.
回答2:
Carles answer is valid, but I would avoid using QueryImplementingThingsWithData. The problem with QueryImplementingThingsWithData is that Thingworx will check visibility, then security for every single property on every single implemented Thing. This is fine if you are running as a user in the Administrators group but once you have a lot of UserGroups and OrganizationalUnits this will slow down, A LOT.
Instead do something like this: (You'll need to create a DataShape and set that as your service return datashape)
var result; //result infotable, of your CarDataShape
var myThings = ThingTemplates["CarTemplate"].QueryImplementingThings();
for(var i=0; i< myThings.getRowCount(); i++) {
var myCar = Things[myThings.rows[i].name];
for(var j=0; j < myCar.sensorProperty.getRowCount(); j++) {
var newRow = {};
newRow.name = myCar.name;
newRow.sensor = myCar.sensorProperty.rows[j].sensorName;
result.AddRow(myCar);
}
}
来源:https://stackoverflow.com/questions/36721805/thingworx-customize-getimplementingthings-service