AddressSpace.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { UANodeSet } from "./UANodeSet";
  2. import { UABaseNode } from "./UABaseNode";
  3. import { NamespaceTable } from "./NameSpaceTable";
  4. import YAML from 'yaml'
  5. import JSZip from "jszip";
  6. import { ReferenceTypeIds } from "./opcua_node_ids";
  7. import type { NodeId } from "./NodeId";
  8. import {sconfig} from "@/util/ServerConfig"
  9. export class AddressSpace{
  10. nodeMap: Map<string, UABaseNode>;
  11. nst: NamespaceTable;
  12. nodesets: UANodeSet[];
  13. mapping: Map<string, IMappingValue>;
  14. constructor(nodesets: UANodeSet[]) {
  15. this.nst=new NamespaceTable();
  16. this.nodeMap=new Map();
  17. this.nodesets=[];
  18. this.mapping=new Map();
  19. for(const nodeset of nodesets) {
  20. this.addNodeset(nodeset);
  21. }
  22. }
  23. public findNode(nodeId: string):UABaseNode|undefined {
  24. return this.nodeMap.get(nodeId)
  25. }
  26. public getHierarchicalReferenceTypes() {
  27. }
  28. public getSubTreeAsList(nodeId: string): UABaseNode[] {
  29. const node=this.findNode(nodeId);
  30. if(!node)
  31. throw new Error(`Nodeid ${nodeId} not found`)
  32. const nlist:UABaseNode[]=[node];
  33. for(const n of node.getChildren()) {
  34. const list=this.getSubTreeAsList(n.nodeId.toString());
  35. nlist.push(...list);
  36. }
  37. return nlist;
  38. }
  39. public addNodeset(nodeset: UANodeSet) {
  40. nodeset.reIndex(this.nst);
  41. for(const node of nodeset.nodes) {
  42. this.nodeMap.set(node.nodeId.toString(), node);
  43. }
  44. nodeset.resolveReferences(this.nodeMap);
  45. this.nodesets.push(nodeset);
  46. }
  47. static async load(files: string[]) {
  48. const promises:Promise<UANodeSet>[] = []
  49. for(const file of files) {
  50. promises.push(UANodeSet.load(file));
  51. }
  52. const sets:UANodeSet[]= [];
  53. for(const p of promises) {
  54. sets.push(await p)
  55. }
  56. return new AddressSpace(sets);
  57. }
  58. public exportProject() {
  59. const zip = new JSZip();
  60. const fileNames:string[]=[];
  61. for(const ns of this.nodesets) {
  62. fileNames.push(ns.fileName);
  63. zip.file(ns.fileName, ns.toXML(ns.nameSpaceTable, this.nst).toString());
  64. }
  65. sconfig.nodesets = fileNames;
  66. zip.file("project.json", JSON.stringify(sconfig));
  67. const mapString=YAML.stringify(this.mapping.values());
  68. zip.file("mapping.yaml", mapString)
  69. return zip.generateAsync({type:'blob'});
  70. }
  71. }
  72. export interface IMappingValue {
  73. path: string;
  74. read: string;
  75. write: string;
  76. }