AddressSpace.ts 2.4 KB

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