Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete list of all tag names that are valid on the controller #46

Open
SerafinTech opened this issue Aug 23, 2023 · 0 comments
Open
Assignees

Comments

@SerafinTech
Copy link
Owner

A user contacted me by email and wanted to see a way to get a complete list of all tag names that would valid on the controller including drilling down through arrays and structures. I came up with this solution. Would this be useful as a utility function?

const eip = require('st-ethernet-ip')

let cont = new eip.Controller()

cont.connect('192.168.121.10')
    .then(async () => {

        let directory = {}

        // Separate tags by program / controller scope
        cont.tagList.forEach( t => {
            directory[t.program] = [];
        })

        // Iterate through each scope
        for (program in directory) {
            let nameList = []
            let progValue = (program === 'null') ? null : program;
            let taglist = cont.tagList.filter(t2 => t2.program === progValue);

            // Iterate through each tag of each scope
            for (let i = 0; i < taglist.length; i++) {
                await drillDown(cont, nameList, taglist[i])
            }
            
            directory[program] = nameList
        }

        // Change 'null' name to 'Controller'
        directory['Controller'] = directory['null']
        delete directory.null

        console.log(directory)

    })

// Recursive tag name listing function
async function drillDown(cont, nameList, tagInfo, previousName) {
    let tagLength = 1;
    if (tagInfo.type.arrayDims > 0) {
        if (tagInfo.info === undefined) {
            let contTag = cont.newTag(tagInfo.name, tagInfo.program, false, 1);
            tagLength = await cont.getTagArraySize(contTag); 
        } else {
            tagLength = tagInfo.info;
        }
        for (let i = 0; i < tagLength; i++) {
            let newName
            if (previousName) {
                newName = previousName + '.' + tagInfo.name + '[' + i + ']';
            } else {
                newName = tagInfo.name + '[' + i + ']';
            }

            nameList.push(newName);

            if (tagInfo.type.structure) {
                let members = cont.templateList[tagInfo.type.code]._members;
                for (let a = 0; a < members.length; a++) {
                    await drillDown(cont, nameList, members[a], newName);
                }
            }

        }
    } else {
        let newName
        if (previousName) {
            newName = previousName + '.' + tagInfo.name 
        } else {
            newName = tagInfo.name
        }

        nameList.push(newName);

        if (tagInfo.type.structure) {
            let members = cont.templateList[tagInfo.type.code]._members;
            for (let a = 0; a < members.length; a++) {
                await drillDown(cont, nameList, members[a], newName);
            }
        }
    }
    
}
@SerafinTech SerafinTech self-assigned this Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant