0

我正在尝试基于 OPC Server 的浏览构建文件夹路径,我可以部分成功,因为我可以使用递归获取根文件夹节点,然后是其下的子节点等等,除非我无法构建我想要的格式,例如 [root/children1/children1,root/children1/children2,......] 这是这些字符串类型路径的列表?看看下面的代码:

您可以从图像中看到,我可以根据树视图中的结构提取文件夹,但我无法正确格式化?我还故意注释掉了变量 pathList 因为我无法正确解析并添加到列表中,所以我只是做了一个打印测试来确定我是否到达了子节点!

    #This code should browse the nodes in the server that are folders and start creating the directory path from the root.
#The code will then construct a dataset to be used in the tree browse component.
#format should be [root/child/child,root/child/child,........,Npath] in List


OPCServerName = "LINX-7923 Server"

#start from the root
NodeId = ""

def browseServer(nodeId):
    pathList = []
    children = system.opc.browseServer(OPCServerName, nodeId)
    for child in children:
        elementType = str(child.getElementType())#SEES FOLDERS AS TYPE OBJECTS INSTEAD OF TYPE "FOLDER"
        childNodeId = str(child.getServerNodeId().getNodeId()) #Get the Node ID ex: n=1;i=x
        folderName = str(child.getDisplayName())#get the string name of the node in the server
        if  elementType == 'OBJECT':#all folders are treated as objects thats why im doing this comparing
            print folderName
            #pathList.append(folderName)
            browseServer(childNodeId)
            
    return pathList

paths = browseServer(NodeId)
for path in paths:
    print path

点火输出

在此处输入图像描述

4

0 回答 0