Datasource (the value
parameter in the transform functions below):
[
{
"label": "GPA",
"expanded": true,
"icon": {
"path": "material/business",
"color": "",
"style": {}
},
"data": {
"EqID": 1,
"EqType": "enterprise",
"EqEnabled": true,
"EqName": "GPA",
"EqPath": "GPA"
},
"items": [
{
"label": "Virginia",
"expanded": true,
"icon": {
"path": "material/location_city",
"color": "",
"style": {}
},
"data": {
"EqID": 1,
"EqType": "site",
"EqEnabled": true,
"EqName": "Virginia",
"EqPath": "GPA/Virginia"
},
"items": [
{
"label": "Mixing",
"expanded": true,
"icon": {
"path": "material/business_center",
"color": "",
"style": {}
},
"data": {
"EqID": 1,
"EqType": "area",
"EqEnabled": true,
"EqName": "Mixing",
"EqPath": "GPA/Virginia/Mixing"
},
"items": [
{
"label": "Mixer 1",
"expanded": false,
"icon": {
"path": "material/line_style",
"color": "",
"style": {}
},
"data": {
"EqID": 1,
"EqType": "line",
"EqEnabled": true,
"EqName": "Mixer 1",
"EqPath": "GPA/Virginia/Mixing/Mixer 1"
},
"items": [
{
"label": "Drum",
"expanded": false,
"icon": {
"path": "material/apartment",
"color": "",
"style": {}
},
"data": {
"EqID": 1,
"EqType": "cell",
"EqEnabled": true,
"EqName": "Drum",
"EqPath": "GPA/Virginia/Mixing/Mixer 1/Drum"
},
"items": []
},
{
"label": "Sweeper",
"expanded": false,
"icon": {
"path": "material/apartment",
"color": "",
"style": {}
},
"data": {
"EqID": 3,
"EqType": "cell",
"EqEnabled": true,
"EqName": "Sweeper",
"EqPath": "GPA/Virginia/Mixing/Mixer 1/Sweeper"
},
"items": []
}
]
},
{
"label": "Mixer 2",
"expanded": false,
"icon": {
"path": "material/line_style",
"color": "",
"style": {}
},
"data": {
"EqID": 5,
"EqType": "line",
"EqEnabled": true,
"EqName": "Mixer 2",
"EqPath": "GPA/Virginia/Mixing/Mixer 2"
},
"items": [
{
"label": "Drum",
"expanded": false,
"icon": {
"path": "material/apartment",
"color": "",
"style": {}
},
"data": {
"EqID": 4,
"EqType": "cell",
"EqEnabled": true,
"EqName": "Drum",
"EqPath": "GPA/Virginia/Mixing/Mixer 2/Drum"
},
"items": []
}
]
},
{
"label": "Mixer 3",
"expanded": false,
"icon": {
"path": "material/line_style",
"color": "",
"style": {}
},
"data": {
"EqID": 8,
"EqType": "line",
"EqEnabled": true,
"EqName": "Mixer 3",
"EqPath": "GPA/Virginia/Mixing/Mixer 3"
},
"items": []
},
{
"label": "Mixer 4",
"expanded": false,
"icon": {
"path": "material/line_style",
"color": "",
"style": {}
},
"data": {
"EqID": 9,
"EqType": "line",
"EqEnabled": true,
"EqName": "Mixer 4",
"EqPath": "GPA/Virginia/Mixing/Mixer 4"
},
"items": [
{
"label": "Cell 1",
"expanded": false,
"icon": {
"path": "material/apartment",
"color": "",
"style": {}
},
"data": {
"EqID": 10,
"EqType": "cell",
"EqEnabled": true,
"EqName": "Cell 1",
"EqPath": "GPA/Virginia/Mixing/Mixer 4/Cell 1"
},
"items": []
},
{
"label": "Cell 2",
"expanded": false,
"icon": {
"path": "material/apartment",
"color": "",
"style": {}
},
"data": {
"EqID": 11,
"EqType": "cell",
"EqEnabled": true,
"EqName": "Cell 2",
"EqPath": "GPA/Virginia/Mixing/Mixer 4/Cell 2"
},
"items": []
}
]
}
]
}
]
}
]
}
]
When I try to pass index
as a string:
def transform(self, value, quality, timestamp):
final_node = {
"EqEnabled": "",
"EqID": "",
"EqName": "",
"EqPath": "",
"EqType": ""
}
if self.props.selection[0] and len(self.props.selection) > 0:
path = self.props.selection[0].split("/") # 0/0/0/0
current_node = value
current_node_index = 0
path_array_final_index = len(path) - 1
for index in path:
index = index
current_node = current_node.get(index)
current_node_index += 1
if current_node_index != path_array_final_index:
current_node = current_node.get("items")
current_node_data = current_node["data"]
final_node["EqEnabled"] = current_node_data["EqEnabled"]
final_node["EqID"] = current_node_data["EqID"]
final_node["EqName"] = current_node_data["EqName"]
final_node["EqPath"] = current_node_data["EqPath"]
final_node["EqType"] = current_node_data["EqType"]
return final_node
Traceback (most recent call last): File "<transform>", line 16, in transform TypeError: com.inductiveautomation.perspective.gateway.script.JsonifiableArrayList indices must be integers
When I try to pass index
as an integer:
def transform(self, value, quality, timestamp):
final_node = {
"EqEnabled": "",
"EqID": "",
"EqName": "",
"EqPath": "",
"EqType": ""
}
if self.props.selection[0] and len(self.props.selection) > 0:
path = self.props.selection[0].split("/") # 0/0/0/0
current_node = value
current_node_index = 0
path_array_final_index = len(path) - 1
for index in path:
index = index
current_node = current_node.get(int(index))
current_node_index += 1
if current_node_index != path_array_final_index:
current_node = current_node.get("items")
current_node_data = current_node["data"]
final_node["EqEnabled"] = current_node_data["EqEnabled"]
final_node["EqID"] = current_node_data["EqID"]
final_node["EqName"] = current_node_data["EqName"]
final_node["EqPath"] = current_node_data["EqPath"]
final_node["EqType"] = current_node_data["EqType"]
return final_node
Traceback (most recent call last): File "<transform>", line 17, in transform TypeError: expected a str