All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.unterrainer.commons.opcuabrowser.parts.Browser Maven / Gradle / Ivy

The newest version!
package info.unterrainer.commons.opcuabrowser.parts;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription;

import info.unterrainer.commons.opcuabrowser.OpcUaClient;
import info.unterrainer.commons.opcuabrowser.dtos.BrowseNode;
import info.unterrainer.commons.opcuabrowser.dtos.BrowseResultJson;
import info.unterrainer.commons.opcuabrowser.dtos.BrowseSiblingsJson;
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Browser {

	private final OpcUaClient client;
	@Getter
	private final NodeId browseRoot;
	@Getter
	private BrowseNode rootNode;
	@Getter
	private final MultiValuedMap siblings = new ArrayListValuedHashMap<>();

	public Browser(final OpcUaClient client, final NodeId browseRoot) {
		super();
		this.client = client;
		this.browseRoot = browseRoot;
		try {
			rootNode = BrowseNode.getFrom(client.getAddressSpace().getNode(browseRoot));
		} catch (InterruptedException | ExecutionException | UaException e) {
			log.warn("Browsing nodeId=[{}] failed: [{}] cause: [{}]", browseRoot, e.getMessage(),
					e.getCause().getMessage());
		}
	}

	public BrowseNode getNode(final NodeId nodeId) {
		return getNode(client, nodeId);
	}

	public static BrowseNode getNode(final OpcUaClient client, final NodeId nodeId) {
		BrowseNode n = null;
		try {
			n = BrowseNode.getFrom(client.getAddressSpace().getNode(nodeId));
		} catch (InterruptedException | ExecutionException | UaException e) {
			log.warn("Browsing nodeId=[{}] failed: [{}] cause: [{}]", nodeId, e.getMessage(),
					e.getCause().getMessage());
		}
		return n;
	}

	public void browse(final boolean browseChildren) {
		browse(browseRoot, browseChildren);
	}

	private void browse(final NodeId browseRoot, final boolean browseChildren) {
		try {
			List refs = client.getNodesUnder(browseRoot);
			String id = browseRoot.toParseableString();

			for (ReferenceDescription ref : refs) {
				BrowseNode n = BrowseNode.getFrom(ref, client);
				siblings.put(id, n);
				if (browseChildren)
					browse(n.getNodeId(), true);
			}

		} catch (Exception e) {
			log.warn("Browsing nodeId=[{}] failed: [{}] cause: [{}]", browseRoot, e.getMessage(),
					e.getCause().getMessage());
		}
	}

	public void log() {
		log("", browseRoot);
	}

	private void log(final String indent, final NodeId rootNodeId) {
		Collection nodes = siblings.get(rootNodeId.toParseableString());
		rootNode.log(indent + ".");
		nodes.forEach(e -> e.log(indent));
		nodes.forEach(e -> log(indent + ".", e.getNodeId()));
	}

	public BrowseResultJson toJson() throws IOException {
		Map nodeMap = new HashMap<>();
		List siblingsJsons = new ArrayList<>();

		jsonCollector(browseRoot, nodeMap, siblingsJsons);
		BrowseResultJson nodesJson = new BrowseResultJson();
		nodesJson.setRootNode(rootNode);
		nodesJson.setNodes(nodeMap);
		nodesJson.setSiblings(siblingsJsons);
		return nodesJson;
	}

	public void toFile(final String file) throws IOException {
		Path p = Paths.get(file);
		Files.deleteIfExists(p);
		File f = p.toFile();
		f.createNewFile();

		JsonMapper s = JsonMapper.create();
		String json = s.toStringFrom(toJson());
		Files.writeString(p, json, StandardCharsets.UTF_8);
	}

	private void jsonCollector(final NodeId rootNodeId, final Map nodeMap,
			final List siblingsJsons) {
		String rootId = rootNodeId.toParseableString();
		Collection nodes = siblings.get(rootId);
		BrowseSiblingsJson s = new BrowseSiblingsJson();
		s.setId(rootId);
		s.setSiblings(nodes.stream().map(BrowseNode::getId).collect(Collectors.toList()));
		siblingsJsons.add(s);
		nodes.forEach(e -> nodeMap.put(e.getId(), e));
		nodes.forEach(e -> jsonCollector(e.getNodeId(), nodeMap, siblingsJsons));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy