AddressSpace.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { UANodeSet } from "./UANodeSet";
  2. import { UABaseNode } from "./UABaseNode";
  3. import { NamespaceTable } from "./NameSpaceTable";
  4. import { type IAddressSpace } from "./IAddressSpace";
  5. import type { IMappingEntry } from "@/ServerConfig";
  6. import { ReferenceTypeIds } from "./opcua_node_ids";
  7. export class AddressSpace implements IAddressSpace{
  8. nodeMap: Map<string, UABaseNode>;
  9. nst: NamespaceTable;
  10. nodesets: UANodeSet[];
  11. mapping: Map<string, IMappingEntry>;
  12. constructor(nodesets: UANodeSet[]) {
  13. this.nst=new NamespaceTable();
  14. this.nodeMap=new Map();
  15. this.nodesets=[];
  16. this.mapping=new Map();
  17. for(const nodeset of nodesets) {
  18. this.addNodeset(nodeset);
  19. }
  20. }
  21. getNodeSets(): UANodeSet[]{
  22. return this.nodesets;
  23. }
  24. public findNode(nodeId: string):UABaseNode|undefined {
  25. return this.nodeMap.get(nodeId)
  26. }
  27. public getSubTreeAsList(nodeId: string): UABaseNode[] {
  28. const node=this.findNode(nodeId);
  29. if(!node)
  30. return [];
  31. const nlist:UABaseNode[]=[node];
  32. for(const n of node.getChildren()) {
  33. const list=this.getSubTreeAsList(n.nodeId.toString());
  34. nlist.push(...list);
  35. }
  36. return nlist;
  37. }
  38. getRefTypes():UABaseNode[] {
  39. return this.getSubTreeAsList("i="+ReferenceTypeIds.HierarchicalReferences)||[];
  40. }
  41. public addNodeset(nodeset: UANodeSet) {
  42. nodeset.reIndex(this.nst);
  43. for(const node of nodeset.nodes) {
  44. this.nodeMap.set(node.nodeId.toString(), node);
  45. }
  46. nodeset.resolveReferences(this.nodeMap);
  47. this.nodesets.push(nodeset);
  48. }
  49. static async load(files: string[]) {
  50. const as=new AddressSpace([]);
  51. const promises:Promise<UANodeSet>[] = []
  52. for(const file of files) {
  53. promises.push(UANodeSet.load(file, as));
  54. }
  55. for(const p of promises) {
  56. as.addNodeset(await p);
  57. }
  58. return as;
  59. }
  60. }