UNPKG

st-ethernet-ip

Version:
83 lines (66 loc) 2.56 kB
const eip = require('./dist/index.js') let cont = new eip.Controller() cont.connect('192.168.100.2') .then(async () => { let directory = {} // Seperate 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); } } } }