For a recent project, I was needing to find all of the children in a Network Hierarchy of a particular template type... so I put together a little script that I thought I'd share. Maybe this will be useful to others as well.
In my situation, this script lived in the Location template. This was useful so that I could find all the Sensor Things under any particular node, no matter how deep they are.
For example, given a network like this:
- Location 1
- Sensor 1
- Location 1A
- Sensor 2
- Sensor 3
- Location 1AA
- Sensor 4
- Location 1B
- Sensor 5
If you run this service in Location 1, you'll get an InfoTable with these Things:
- Sensor 1
- Sensor 2
- Sensor 3
- Sensor 4
- Sensor 5
From Location 1A:
- Sensor 2
- Sensor 3
- Sensor 4
From Location 1AA:
- Sensor 4
From Location 1B:
- Sensor 5
For this service, these are the inputs/outputs:
- Inputs: none
- Output: InfoTable of type NetworkConnection
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(AlertSummary)
let result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "NetworkConnection"
});
// since the hierarchy could contain locations or sensors, need to recursively loop down to get all the sensors
function findChildrenSensors(thingName) {
let childrenThings = Networks["Hierarchy_NW"].GetChildConnections({
name: thingName /* STRING */
});
for each (var row in childrenThings.rows) {
// row.to has the name of the child Thing
if (Things[row.to].IsDerivedFromTemplate({thingTemplateName: "Location_TT"})) {
findChildrenSensors(row.to);
} else if (Things[row.to].IsDerivedFromTemplate({thingTemplateName: "Sensor_TT"})) {
result.AddRow(row);
}
}
}
findChildrenSensors(me.name);