Find paths of all tags in default provider

ignition79
#1

I’m attempting to read all tags in the gateway from the default provider and copy those to a database. I’m able to do so if I hard-code each tagpath using TagPathParser.parse(“string path”) , but is what is the way to do so without hard-coding? If I do:

TagPath path = TagPathParser.parse("[default]");
List<Tag>tag=browse(path);

Is there a way to get the tagpaths of the list of tags?

Any help is appreciated.

0 Likes

#2
def getAllTags(tagPath):
	appRoot = tagPath
	browseTagArray = system.tag.browseTags(parentPath="[default]"+appRoot,recursive=True)
	tagDataset = []
	tagHeaders = ['name', 'fullPath', 'type', 'dataType']
	for browseTag in browseTagArray:
		if not browseTag.isFolder():
			tagDataset.append([browseTag.name, browseTag.fullPath, browseTag.type, browseTag.dataType]);
	return system.dataset.toDataSet(tagHeaders, tagDataset)

This function will take a passed root folder and parse ALL the tags below that and return it as a dataset.
Be careful if you have a very large tag database, this could take a long time.

0 Likes

#3

I assume I’ll need to download jython, then (sorry, I’m pretty new to java). Thank you, I’ll try this.

0 Likes

#4

I don’t think he realized this was a module SDK question (this is a module SDK question, right?)

0 Likes

#5

Yes, it is.

0 Likes

#6

You’ll want to use TagManager#browseAsync, starting with the TagPath you built to the provider, and then continuing to browse recursively for all the results you get at each level.

0 Likes

#7

So I’m using the 7.9 API and I still haven’t been able to access the tags. I assume my problem is that my nextNode string in getChildPath is empty? The project compiles but there’s no change to the database.

 public void findTags() throws IOException {
    	String nextNode="";
    	dpath = TagPathParser.parse("[default]");
    	tag = context.getTagManager().getTagProvider("[default]").browse(dpath);
    	temppath=dpath;
    	while (tag.isEmpty()==true) {
			temppath = temppath.getChildPath(nextNode);
			tag = context.getTagManager().getTagProvider("[default]").browse(temppath);
    	}
    	String tagname=tag.get(0).getName();
    	TagPath name = TagPathParser.parse(tagname);
    	TagPath parent = name.getParentPath();
    	TagPath reversepath[]=new TagPath[]{};
    	TagPath temparr[]=new TagPath[] {};
    	while(parent!=dpath) {
    		TagPath pathslash = BasicTagPath.append(parent, TagPathParser.parse("/"));
    		temparr[0]= pathslash;
    		parent=parent.getParentPath();
    		reversepath=ArrayUtils.addAll(reversepath, temparr);
    	}
    	for (int i=((reversepath.length)-1);i>=0;i--) {
    		wholepath= BasicTagPath.append(wholepath,reversepath[i]);
    	}
    	wholepath = BasicTagPath.append(dpath, wholepath);
    	wholepath=BasicTagPath.append(wholepath, name);
    	readingTag();
    }
    public void readingTag() throws IOException{
		listpaths.add(wholepath);
		
		TagListener listener = new TagListener(wholepath) {
		};
		
		context.getTagManager().subscribe(wholepath, listener);
		tags= context.getTagManager().read(listpaths);
		tagvalue = (int)tags.get(0).getValue();
		tagqual = tags.get(0).getQuality();
		Date df = tags.get(0).getTimestamp();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss", Locale.getDefault());
		tagdate = dateFormat.format(df);
		tagpath = listpaths.get(0);
		datacopy(tagpath,tagvalue,tagqual,tagdate);
	}
	public void datacopy(TagPath tp,int value, Quality qual, String date) {
		try {
			dbconnection = GatewayHook.context.getDatasourceManager().getConnection("ModTest");//connect to db
		} catch (SQLException e) {
			logger.fatal("Database connection failed.",e);
		}
		String query = String.format("INSERT INTO [ModTAGS].[dbo].[ModuleTags] (TagName, TagValue, TagQuality, TagTimeStamp) VALUES ('%s',%d,'%s','%s')",tp,value,qual,date);//insert tag list
		try {
			dbconnection.runPrepQuery(query);//insert tag list
		} catch (SQLException e) {
			logger.fatal("SQL copy fail",e);
			
		}
	}
0 Likes

#8

I’d start by adding a bunch of logging to let you know how far you’re getting and if you’re getting the data you expect at each step.

0 Likes

#9

Ahh man, sorry about that, I browse using latest posts, not by category. :blush:

1 Like

#10

Not a problem!

0 Likes

#11

It appears the problem is with my .getChildPath(nextNode) statement since no it returns the name of the tag provider ("[default]"):

TagPath dPath=TagPathParser.parse("[default]");
TagPath cPath=dPath.getChildPath("[default]");
nextNode = cPath.getItemName();
TagPath cPath2 = cPath.getChildPath(nextNode);
String sPath = cPath2.getItemName();

sPath (and cPath2) appear to be “[default]” and I can’t get the folder below it. What am I missing in the getChildPath parameters?

0 Likes

#12

In case anyone has time, I’m trying to find the the path to a tag that may be nested inside a folder:
[default]/FolderName/Tag. However, getChildPath only returns the nextNode string parameter as a tagpath, not the child of that string. Is getChildPath the right way to go about this?

TagPath dPath=TagPathParser.parse("[default]");
TagPath cPath=dPath.getChildPath("[default]");
nextNode = cPath.getItemName();
TagPath cPath2 = cPath.getChildPath(nextNode);
String sPath = cPath2.getItemName();

sPath (and cPath2) appear to be “[default]” and I can’t get the folder below it.

0 Likes

#13

Figured it out! I assumed using browse("[default]") returned an empty collection, but a folder is treated as a tag, so the name of the first folder is returned.

tag = context.getTagManager().getTagProvider("default").browse(dpath);
//tag.get(0) is TestPath (a folder in the tagprovider)
0 Likes