AddressSpace.ts 2.3 KB

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