Reading tag values in Gateway (Subscribing to Tags)

I figured out how to read tag values so I am posting the solution here in case someone will be struggling with this like I was.

  1. First of all you need to make new class implementing TagChangeListener
public class TagListener implements TagChangeListener {
	TagPath path;
	public TagListener(TagPath path) {
		this.path= path;
		// TODO Auto-generated constructor stub
	}

	private final Logger log = Logger.getLogger(getClass());

	@Override
	public TagProp getTagProperty() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void tagChanged(TagChangeEvent arg0) {
		List <TagPath> list= new ArrayList<TagPath>();
		list.add(path);
		List<QualifiedValue> tags= GatewayHook.context.getTagManager().read(list);
		log.error("Value : "+ tags.get(0).getValue());
	}
}

With overriding the “tagChanged” function you define what should happen when the tag changes.

  1. Than you use the “TagManager” provided by the context you are in to subscribe to the wanted tag.
public void readingTest() throws IOException{
		TagPath myPath= TagPathParser.parse("[default]underTag/TestniTag");
		TagListener listener=new TagListener(myPath) {

		};
		List <TagPath> lista= new ArrayList<TagPath>();
		lista.add(myPath);
		GatewayHook.context.getTagManager().subscribe(myPath, listener);
		
	}
4 Likes